-
Notifications
You must be signed in to change notification settings - Fork 780
/
kv_keys.go
124 lines (102 loc) · 2.68 KB
/
kv_keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dependency
import (
"fmt"
"log"
"net/url"
"regexp"
"strings"
"github.com/pkg/errors"
)
var (
// Ensure implements
_ Dependency = (*KVKeysQuery)(nil)
// KVKeysQueryRe is the regular expression to use.
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
namespace string
partition string
}
// NewKVKeysQuery parses a string into a dependency.
func NewKVKeysQuery(s string) (*KVKeysQuery, error) {
if s != "" && !KVKeysQueryRe.MatchString(s) {
return nil, fmt.Errorf("kv.keys: invalid format: %q", s)
}
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"],
namespace: queryParams.Get(QueryNamespace),
partition: queryParams.Get(QueryPartition),
}, nil
}
// Fetch queries the Consul API defined by the given client.
func (d *KVKeysQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-d.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
ConsulPartition: d.partition,
ConsulNamespace: d.namespace,
})
log.Printf("[TRACE] %s: GET %s", d, &url.URL{
Path: "/v1/kv/" + d.prefix,
RawQuery: opts.String(),
})
list, qm, err := clients.Consul().KV().Keys(d.prefix, "", opts.ToConsulOpts())
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
keys := make([]string, len(list))
for i, v := range list {
v = strings.TrimPrefix(v, d.prefix)
v = strings.TrimLeft(v, "/")
keys[i] = v
}
log.Printf("[TRACE] %s: returned %d results", d, len(list))
rm := &ResponseMetadata{
LastIndex: qm.LastIndex,
LastContact: qm.LastContact,
}
return keys, rm, nil
}
// CanShare returns a boolean if this dependency is shareable.
func (d *KVKeysQuery) CanShare() bool {
return true
}
// String returns the human-friendly version of this dependency.
func (d *KVKeysQuery) String() string {
prefix := d.prefix
if d.dc != "" {
prefix = prefix + "@" + d.dc
}
if d.partition != "" {
prefix = prefix + "@partition=" + d.partition
}
if d.namespace != "" {
prefix = prefix + "@ns=" + d.namespace
}
return fmt.Sprintf("kv.keys(%s)", prefix)
}
// Stop halts the dependency's fetch function.
func (d *KVKeysQuery) Stop() {
close(d.stopCh)
}
// Type returns the type of this dependency.
func (d *KVKeysQuery) Type() Type {
return TypeConsul
}