diff --git a/api/api.go b/api/api.go index b624b3cce3..0444e9e75e 100644 --- a/api/api.go +++ b/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) } diff --git a/api/api_test.go b/api/api_test.go index 8de3a0805f..48b568f1b7 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -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) }