Add Namespace as an api query/write option (#6551)

This commit is contained in:
Freddy 2019-09-26 10:05:13 -06:00 committed by GitHub
parent 78366c5830
commit 2274f64cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -75,6 +75,10 @@ const (
// QueryOptions are used to parameterize a query // QueryOptions are used to parameterize a query
type QueryOptions struct { 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 // Providing a datacenter overwrites the DC provided
// by the Config // by the Config
Datacenter string Datacenter string
@ -178,6 +182,10 @@ func (o *QueryOptions) WithContext(ctx context.Context) *QueryOptions {
// WriteOptions are used to parameterize a write // WriteOptions are used to parameterize a write
type WriteOptions struct { 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 // Providing a datacenter overwrites the DC provided
// by the Config // by the Config
Datacenter string Datacenter string
@ -624,6 +632,9 @@ func (r *request) setQueryOptions(q *QueryOptions) {
if q == nil { if q == nil {
return return
} }
if q.Namespace != "" {
r.params.Set("ns", q.Namespace)
}
if q.Datacenter != "" { if q.Datacenter != "" {
r.params.Set("dc", q.Datacenter) r.params.Set("dc", q.Datacenter)
} }
@ -722,6 +733,9 @@ func (r *request) setWriteOptions(q *WriteOptions) {
if q == nil { if q == nil {
return return
} }
if q.Namespace != "" {
r.params.Set("ns", q.Namespace)
}
if q.Datacenter != "" { if q.Datacenter != "" {
r.params.Set("dc", q.Datacenter) r.params.Set("dc", q.Datacenter)
} }

View File

@ -695,6 +695,7 @@ func TestAPI_SetQueryOptions(t *testing.T) {
r := c.newRequest("GET", "/v1/kv/foo") r := c.newRequest("GET", "/v1/kv/foo")
q := &QueryOptions{ q := &QueryOptions{
Namespace: "operator",
Datacenter: "foo", Datacenter: "foo",
AllowStale: true, AllowStale: true,
RequireConsistent: true, RequireConsistent: true,
@ -706,6 +707,9 @@ func TestAPI_SetQueryOptions(t *testing.T) {
} }
r.setQueryOptions(q) r.setQueryOptions(q)
if r.params.Get("ns") != "operator" {
t.Fatalf("bad: %v", r.params)
}
if r.params.Get("dc") != "foo" { if r.params.Get("dc") != "foo" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.params)
} }
@ -752,11 +756,14 @@ func TestAPI_SetWriteOptions(t *testing.T) {
r := c.newRequest("GET", "/v1/kv/foo") r := c.newRequest("GET", "/v1/kv/foo")
q := &WriteOptions{ q := &WriteOptions{
Namespace: "operator",
Datacenter: "foo", Datacenter: "foo",
Token: "23456", Token: "23456",
} }
r.setWriteOptions(q) r.setWriteOptions(q)
if r.params.Get("ns") != "operator" {
t.Fatalf("bad: %v", r.params)
}
if r.params.Get("dc") != "foo" { if r.params.Get("dc") != "foo" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.params)
} }