Skip to content

Commit

Permalink
kv list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodingenthusiast committed Nov 21, 2023
1 parent c49c567 commit 522ad1e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
queryRe = `(\?(?P<query>[[:word:]\-\_\=\&]+))?`
nodeNameRe = `(?P<name>[[:word:]\.\-\_]+)`
nearRe = `(~(?P<near>[[:word:]\.\-\_]+))?`
prefixRe = `/?(?P<prefix>[^@]+)`
prefixRe = `/?(?P<prefix>[^@\?]+)`
tagRe = `((?P<tag>[[:word:]=:\.\-\_]+)\.)?`
regionRe = `(@(?P<region>[[:word:]\.\-\_]+))?`
nvPathRe = `/?(?P<path>[^@]+)`
Expand Down
2 changes: 0 additions & 2 deletions dependency/kv_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/url"
"regexp"

"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions dependency/kv_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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.
Expand All @@ -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
}

Expand All @@ -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{
Expand Down
61 changes: 61 additions & 0 deletions dependency/kv_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
15 changes: 12 additions & 3 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ blocking, use [`keyOrDefault`](#keyordefault) or [`keyExists`](#keyexists).
The `<QUERY>` attribute is optional; if omitted, the `default` Consul namespace, `default` partition will be queried. `<QUERY>` can be used to set the Consul [namespace](https://developer.hashicorp.com/consul/api-docs/health#ns-2) or partition. `<QUERY>` 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 `<DATACENTER>` attribute is optional; if omitted, the local datacenter is
Expand Down Expand Up @@ -348,7 +348,15 @@ instead.
Query [Consul][consul] for all top-level kv pairs at the given key path.

```golang
{{ ls "<PATH>@<DATACENTER>" }}
{{ ls "<PATH>?<QUERY>@<DATACENTER>" }}
```

The `<QUERY>` attribute is optional; if omitted, the `default` Consul namespace, `default` partition will be queried. `<QUERY>` can be used to set the Consul [namespace](https://developer.hashicorp.com/consul/api-docs/health#ns-2) or partition. `<QUERY>` accepts a url query-parameter format, e.g.:

```golang
{{ range ls "service/redis?ns=namespace-name&partition=partition-name" }}
{{ .Key }}:{{ .Value }}
{{ end }}
```

The `<DATACENTER>` attribute is optional; if omitted, the local datacenter is
Expand All @@ -358,7 +366,8 @@ For example:

```golang
{{ range ls "service/redis" }}
{{ .Key }}:{{ .Value }}{{ end }}
{{ .Key }}:{{ .Value }}
{{ end }}
```

renders
Expand Down

0 comments on commit 522ad1e

Please sign in to comment.