-
Notifications
You must be signed in to change notification settings - Fork 784
/
dependency.go
189 lines (154 loc) · 3.85 KB
/
dependency.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package dependency
import (
"net/url"
"regexp"
"sort"
"strconv"
"time"
consulapi "github.com/hashicorp/consul/api"
)
const (
dcRe = `(@(?P<dc>[[:word:]\.\-\_]+))?`
keyRe = `/?(?P<key>[^@]+)`
filterRe = `(\|(?P<filter>[[:word:]\,]+))?`
serviceNameRe = `(?P<name>[[:word:]\-\_]+)`
nodeNameRe = `(?P<name>[[:word:]\.\-\_]+)`
nearRe = `(~(?P<near>[[:word:]\.\-\_]+))?`
prefixRe = `/?(?P<prefix>[^@]+)`
tagRe = `((?P<tag>[[:word:]=:\.\-\_]+)\.)?`
)
type Type int
const (
TypeConsul Type = iota
TypeVault
TypeLocal
)
// Dependency is an interface for a dependency that Consul Template is capable
// of watching.
type Dependency interface {
Fetch(*ClientSet, *QueryOptions) (interface{}, *ResponseMetadata, error)
CanShare() bool
String() string
Stop()
Type() Type
}
// ServiceTags is a slice of tags assigned to a Service
type ServiceTags []string
// QueryOptions is a list of options to send with the query. These options are
// client-agnostic, and the dependency determines which, if any, of the options
// to use.
type QueryOptions struct {
AllowStale bool
Datacenter string
Near string
RequireConsistent bool
VaultGrace time.Duration
WaitIndex uint64
WaitTime time.Duration
}
func (q *QueryOptions) Merge(o *QueryOptions) *QueryOptions {
var r QueryOptions
if q == nil {
if o == nil {
return &QueryOptions{}
}
r = *o
return &r
}
r = *q
if o == nil {
return &r
}
if o.AllowStale != false {
r.AllowStale = o.AllowStale
}
if o.Datacenter != "" {
r.Datacenter = o.Datacenter
}
if o.Near != "" {
r.Near = o.Near
}
if o.RequireConsistent != false {
r.RequireConsistent = o.RequireConsistent
}
if o.WaitIndex != 0 {
r.WaitIndex = o.WaitIndex
}
if o.WaitTime != 0 {
r.WaitTime = o.WaitTime
}
return &r
}
func (q *QueryOptions) ToConsulOpts() *consulapi.QueryOptions {
return &consulapi.QueryOptions{
AllowStale: q.AllowStale,
Datacenter: q.Datacenter,
Near: q.Near,
RequireConsistent: q.RequireConsistent,
WaitIndex: q.WaitIndex,
WaitTime: q.WaitTime,
}
}
func (q *QueryOptions) String() string {
u := &url.Values{}
if q.AllowStale {
u.Add("stale", strconv.FormatBool(q.AllowStale))
}
if q.Datacenter != "" {
u.Add("dc", q.Datacenter)
}
if q.Near != "" {
u.Add("near", q.Near)
}
if q.RequireConsistent {
u.Add("consistent", strconv.FormatBool(q.RequireConsistent))
}
if q.WaitIndex != 0 {
u.Add("index", strconv.FormatUint(q.WaitIndex, 10))
}
if q.WaitTime != 0 {
u.Add("wait", q.WaitTime.String())
}
return u.Encode()
}
// ResponseMetadata is a struct that contains metadata about the response. This
// is returned from a Fetch function call.
type ResponseMetadata struct {
LastIndex uint64
LastContact time.Duration
Block bool
}
// deepCopyAndSortTags deep copies the tags in the given string slice and then
// sorts and returns the copied result.
func deepCopyAndSortTags(tags []string) []string {
newTags := make([]string, 0, len(tags))
for _, tag := range tags {
newTags = append(newTags, tag)
}
sort.Strings(newTags)
return newTags
}
// respWithMetadata is a short wrapper to return the given interface with fake
// response metadata for non-Consul dependencies.
func respWithMetadata(i interface{}) (interface{}, *ResponseMetadata, error) {
return i, &ResponseMetadata{
LastContact: 0,
LastIndex: uint64(time.Now().Unix()),
}, nil
}
// regexpMatch matches the given regexp and extracts the match groups into a
// named map.
func regexpMatch(re *regexp.Regexp, q string) map[string]string {
names := re.SubexpNames()
match := re.FindAllStringSubmatch(q, -1)
if len(match) == 0 {
return map[string]string{}
}
m := map[string]string{}
for i, n := range match[0] {
if names[i] != "" {
m[names[i]] = n
}
}
return m
}