diff --git a/dependency/dependency.go b/dependency/dependency.go index d48c94983..e96cda3c4 100644 --- a/dependency/dependency.go +++ b/dependency/dependency.go @@ -23,7 +23,7 @@ const ( queryRe = `(\?(?P[[:word:]\-\_\=\&]+))?` nodeNameRe = `(?P[[:word:]\.\-\_]+)` nearRe = `(~(?P[[:word:]\.\-\_]+))?` - prefixRe = `/?(?P[^@]+)` + prefixRe = `/?(?P[^@\?]+)` tagRe = `((?P[[:word:]=:\.\-\_]+)\.)?` regionRe = `(@(?P[[:word:]\.\-\_]+))?` nvPathRe = `/?(?P[^@]+)` diff --git a/dependency/kv_get.go b/dependency/kv_get.go index 3e951d93d..938ef8ba3 100644 --- a/dependency/kv_get.go +++ b/dependency/kv_get.go @@ -9,7 +9,6 @@ import ( "net/url" "regexp" - "github.com/davecgh/go-spew/spew" "github.com/pkg/errors" ) @@ -39,7 +38,6 @@ func NewKVGetQuery(s string) (*KVGetQuery, error) { } m := regexpMatch(KVGetQueryRe, s) - spew.Dump(m["key"]) queryParams, err := GetConsulQueryOpts(m, "kv.get") if err != nil { return nil, err diff --git a/dependency/kv_list.go b/dependency/kv_list.go index 8c19a63c6..91de6efbc 100644 --- a/dependency/kv_list.go +++ b/dependency/kv_list.go @@ -19,7 +19,7 @@ var ( _ Dependency = (*KVListQuery)(nil) // KVListQueryRe is the regular expression to use. - KVListQueryRe = regexp.MustCompile(`\A` + prefixRe + dcRe + `\z`) + KVListQueryRe = regexp.MustCompile(`\A` + prefixRe + queryRe + dcRe + `\z`) ) func init() { @@ -44,8 +44,10 @@ type KeyPair struct { type KVListQuery struct { stopCh chan struct{} - dc string - prefix string + dc string + prefix string + namespace string + partition string } // NewKVListQuery parses a string into a dependency. @@ -55,10 +57,17 @@ func NewKVListQuery(s string) (*KVListQuery, error) { } m := regexpMatch(KVListQueryRe, s) + queryParams, err := GetConsulQueryOpts(m, "kv.list") + if err != nil { + return nil, err + } + return &KVListQuery{ - stopCh: make(chan struct{}, 1), - dc: m["dc"], - prefix: m["prefix"], + stopCh: make(chan struct{}, 1), + dc: m["dc"], + prefix: m["prefix"], + namespace: queryParams.Get(QueryNamespace), + partition: queryParams.Get(QueryPartition), }, nil } @@ -71,7 +80,9 @@ func (d *KVListQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{} } opts = opts.Merge(&QueryOptions{ - Datacenter: d.dc, + Datacenter: d.dc, + ConsulPartition: d.partition, + ConsulNamespace: d.namespace, }) log.Printf("[TRACE] %s: GET %s", d, &url.URL{ diff --git a/dependency/kv_list_test.go b/dependency/kv_list_test.go index 50be5dd1f..d6201496c 100644 --- a/dependency/kv_list_test.go +++ b/dependency/kv_list_test.go @@ -30,6 +30,18 @@ func TestNewKVListQuery(t *testing.T) { nil, true, }, + { + "query_only", + "?ns=foo", + nil, + true, + }, + { + "invalid query param (unsupported key)", + "prefix?unsupported=foo", + nil, + true, + }, { "prefix", "prefix", @@ -47,6 +59,55 @@ func TestNewKVListQuery(t *testing.T) { }, false, }, + { + "partition", + "prefix?partition=foo", + &KVListQuery{ + prefix: "prefix", + partition: "foo", + }, + false, + }, + { + "namespace", + "prefix?ns=foo", + &KVListQuery{ + prefix: "prefix", + namespace: "foo", + }, + false, + }, + { + "namespace_and_partition", + "prefix?ns=foo&partition=bar", + &KVListQuery{ + prefix: "prefix", + namespace: "foo", + partition: "bar", + }, + false, + }, + { + "namespace_and_partition_and_dc", + "prefix?ns=foo&partition=bar@dc1", + &KVListQuery{ + prefix: "prefix", + namespace: "foo", + partition: "bar", + dc: "dc1", + }, + false, + }, + { + "empty_query", + "prefix?ns=&partition=", + &KVListQuery{ + prefix: "prefix", + namespace: "", + partition: "", + }, + false, + }, { "dots", "prefix.with.dots", diff --git a/docs/templating-language.md b/docs/templating-language.md index a371b9b9e..ed2ab251b 100644 --- a/docs/templating-language.md +++ b/docs/templating-language.md @@ -267,7 +267,7 @@ blocking, use [`keyOrDefault`](#keyordefault) or [`keyExists`](#keyexists). The `` attribute is optional; if omitted, the `default` Consul namespace, `default` partition will be queried. `` can be used to set the Consul [namespace](https://developer.hashicorp.com/consul/api-docs/health#ns-2) or partition. `` accepts a url query-parameter format, e.g.: ```golang -{{ service "key?ns=namespace-name&partition=partition-name" }} +{{ key "key?ns=namespace-name&partition=partition-name" }} ``` The `` attribute is optional; if omitted, the local datacenter is @@ -348,7 +348,15 @@ instead. Query [Consul][consul] for all top-level kv pairs at the given key path. ```golang -{{ ls "@" }} +{{ ls "?@" }} +``` + +The `` attribute is optional; if omitted, the `default` Consul namespace, `default` partition will be queried. `` can be used to set the Consul [namespace](https://developer.hashicorp.com/consul/api-docs/health#ns-2) or partition. `` accepts a url query-parameter format, e.g.: + +```golang +{{ range ls "service/redis?ns=namespace-name&partition=partition-name" }} + {{ .Key }}:{{ .Value }} +{{ end }} ``` The `` attribute is optional; if omitted, the local datacenter is @@ -358,7 +366,8 @@ For example: ```golang {{ range ls "service/redis" }} -{{ .Key }}:{{ .Value }}{{ end }} + {{ .Key }}:{{ .Value }} +{{ end }} ``` renders