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 4a3496d4bc
commit a74f7e5177
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
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)
}

View File

@ -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)
}