Skip to content

Commit

Permalink
kv keys endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodingenthusiast committed Nov 21, 2023
1 parent 522ad1e commit 295243e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
25 changes: 18 additions & 7 deletions dependency/kv_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ var (
_ Dependency = (*KVKeysQuery)(nil)

// KVKeysQueryRe is the regular expression to use.
KVKeysQueryRe = regexp.MustCompile(`\A` + prefixRe + dcRe + `\z`)
KVKeysQueryRe = regexp.MustCompile(`\A` + prefixRe + queryRe + dcRe + `\z`)
)

// KVKeysQuery queries the KV store for a single key.
type KVKeysQuery struct {
stopCh chan struct{}

dc string
prefix string
dc string
prefix string
namespace string
partition string
}

// NewKVKeysQuery parses a string into a dependency.
Expand All @@ -36,10 +38,17 @@ func NewKVKeysQuery(s string) (*KVKeysQuery, error) {
}

m := regexpMatch(KVKeysQueryRe, s)
queryParams, err := GetConsulQueryOpts(m, "kv.keys")
if err != nil {
return nil, err
}

return &KVKeysQuery{
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 @@ -52,7 +61,9 @@ func (d *KVKeysQuery) 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_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func TestNewKVKeysQuery(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 TestNewKVKeysQuery(t *testing.T) {
},
false,
},
{
"partition",
"prefix?partition=foo",
&KVKeysQuery{
prefix: "prefix",
partition: "foo",
},
false,
},
{
"namespace",
"prefix?ns=foo",
&KVKeysQuery{
prefix: "prefix",
namespace: "foo",
},
false,
},
{
"namespace_and_partition",
"prefix?ns=foo&partition=bar",
&KVKeysQuery{
prefix: "prefix",
namespace: "foo",
partition: "bar",
},
false,
},
{
"namespace_and_partition_and_dc",
"prefix?ns=foo&partition=bar@dc1",
&KVKeysQuery{
prefix: "prefix",
namespace: "foo",
partition: "bar",
dc: "dc1",
},
false,
},
{
"empty_query",
"prefix?ns=&partition=",
&KVKeysQuery{
prefix: "prefix",
namespace: "",
partition: "",
},
false,
},
{
"dots",
"prefix.with.dots",
Expand Down

0 comments on commit 295243e

Please sign in to comment.