Add Namespace as an api query/write option (#6551)
This commit is contained in:
parent
4a3496d4bc
commit
a74f7e5177
14
api/api.go
14
api/api.go
|
@ -75,6 +75,10 @@ const (
|
|||
|
||||
// QueryOptions are used to parameterize a query
|
||||
type QueryOptions struct {
|
||||
// Namespace overrides the `default` namespace
|
||||
// Note: Namespaces are available only in Consul Enterprise
|
||||
Namespace string
|
||||
|
||||
// Providing a datacenter overwrites the DC provided
|
||||
// by the Config
|
||||
Datacenter string
|
||||
|
@ -178,6 +182,10 @@ func (o *QueryOptions) WithContext(ctx context.Context) *QueryOptions {
|
|||
|
||||
// WriteOptions are used to parameterize a write
|
||||
type WriteOptions struct {
|
||||
// Namespace overrides the `default` namespace
|
||||
// Note: Namespaces are available only in Consul Enterprise
|
||||
Namespace string
|
||||
|
||||
// Providing a datacenter overwrites the DC provided
|
||||
// by the Config
|
||||
Datacenter string
|
||||
|
@ -624,6 +632,9 @@ func (r *request) setQueryOptions(q *QueryOptions) {
|
|||
if q == nil {
|
||||
return
|
||||
}
|
||||
if q.Namespace != "" {
|
||||
r.params.Set("ns", q.Namespace)
|
||||
}
|
||||
if q.Datacenter != "" {
|
||||
r.params.Set("dc", q.Datacenter)
|
||||
}
|
||||
|
@ -722,6 +733,9 @@ func (r *request) setWriteOptions(q *WriteOptions) {
|
|||
if q == nil {
|
||||
return
|
||||
}
|
||||
if q.Namespace != "" {
|
||||
r.params.Set("ns", q.Namespace)
|
||||
}
|
||||
if q.Datacenter != "" {
|
||||
r.params.Set("dc", q.Datacenter)
|
||||
}
|
||||
|
|
|
@ -695,6 +695,7 @@ func TestAPI_SetQueryOptions(t *testing.T) {
|
|||
|
||||
r := c.newRequest("GET", "/v1/kv/foo")
|
||||
q := &QueryOptions{
|
||||
Namespace: "operator",
|
||||
Datacenter: "foo",
|
||||
AllowStale: true,
|
||||
RequireConsistent: true,
|
||||
|
@ -706,6 +707,9 @@ func TestAPI_SetQueryOptions(t *testing.T) {
|
|||
}
|
||||
r.setQueryOptions(q)
|
||||
|
||||
if r.params.Get("ns") != "operator" {
|
||||
t.Fatalf("bad: %v", r.params)
|
||||
}
|
||||
if r.params.Get("dc") != "foo" {
|
||||
t.Fatalf("bad: %v", r.params)
|
||||
}
|
||||
|
@ -752,11 +756,14 @@ func TestAPI_SetWriteOptions(t *testing.T) {
|
|||
|
||||
r := c.newRequest("GET", "/v1/kv/foo")
|
||||
q := &WriteOptions{
|
||||
Namespace: "operator",
|
||||
Datacenter: "foo",
|
||||
Token: "23456",
|
||||
}
|
||||
r.setWriteOptions(q)
|
||||
|
||||
if r.params.Get("ns") != "operator" {
|
||||
t.Fatalf("bad: %v", r.params)
|
||||
}
|
||||
if r.params.Get("dc") != "foo" {
|
||||
t.Fatalf("bad: %v", r.params)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue