-
Notifications
You must be signed in to change notification settings - Fork 8
/
query.go
299 lines (284 loc) · 6.91 KB
/
query.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package sabakan
import (
"fmt"
"strings"
)
// Query is an URL query
type Query map[string]string
// Match returns true if all non-empty fields matches Machine
func (q Query) Match(m *Machine) (bool, error) {
if serial := q["serial"]; len(serial) > 0 {
match := false
serials := strings.Split(serial, ",")
for _, s := range serials {
if s == m.Spec.Serial {
match = true
break
}
}
if !match {
return false, nil
}
}
if ipv4 := q["ipv4"]; len(ipv4) > 0 {
ipv4s := strings.Split(ipv4, ",")
match := false
for _, ipv4address := range ipv4s {
for _, ip := range m.Spec.IPv4 {
if ip == ipv4address {
match = true
break
}
}
}
if !match {
return false, nil
}
}
if ipv6 := q["ipv6"]; len(ipv6) > 0 {
ipv6s := strings.Split(ipv6, ",")
match := false
for _, ipv6address := range ipv6s {
for _, ip := range m.Spec.IPv6 {
if ip == ipv6address {
match = true
break
}
}
}
if !match {
return false, nil
}
}
if labels := q["labels"]; len(labels) > 0 {
queries := strings.Split(labels, ",")
for _, query := range queries {
kv := strings.SplitN(query, "=", 2)
if len(kv) != 2 {
return false, fmt.Errorf("invalid query in labels: %s", query)
}
queryKey := kv[0]
queryValue := kv[1]
if value, exists := m.Spec.Labels[queryKey]; exists {
if value != queryValue {
return false, nil
}
} else {
return false, nil
}
}
}
if rack := q["rack"]; len(rack) > 0 {
racks := strings.Split(rack, ",")
match := false
for _, r := range racks {
if r == fmt.Sprint(m.Spec.Rack) {
match = true
break
}
}
if !match {
return false, nil
}
}
if role := q["role"]; len(role) > 0 {
roles := strings.Split(role, ",")
match := false
for _, r := range roles {
if r == m.Spec.Role {
match = true
break
}
}
if !match {
return false, nil
}
}
if bmc := q["bmc-type"]; len(bmc) > 0 {
bmcs := strings.Split(bmc, ",")
match := false
for _, b := range bmcs {
if b == m.Spec.BMC.Type {
match = true
break
}
}
if !match {
return false, nil
}
}
if state := q["state"]; len(state) > 0 {
states := strings.Split(state, ",")
match := false
for _, s := range states {
if s == fmt.Sprint(m.Status.State) {
match = true
break
}
}
if !match {
return false, nil
}
}
if withoutSerial := q["without-serial"]; len(withoutSerial) > 0 {
withoutSerials := strings.Split(withoutSerial, ",")
for _, wr := range withoutSerials {
if wr == fmt.Sprint(m.Spec.Serial) {
return false, nil
}
}
}
if withoutIPv4 := q["without-ipv4"]; len(withoutIPv4) > 0 {
withoutIPv4s := strings.Split(withoutIPv4, ",")
for _, wIPv4 := range withoutIPv4s {
for _, ip := range m.Spec.IPv4 {
if ip == wIPv4 {
return false, nil
}
}
}
}
if withoutIPv6 := q["without-ipv6"]; len(withoutIPv6) > 0 {
withoutIPv6s := strings.Split(withoutIPv6, ",")
for _, wIPv6 := range withoutIPv6s {
for _, ip := range m.Spec.IPv6 {
if ip == wIPv6 {
return false, nil
}
}
}
}
if withoutLabels := q["without-labels"]; len(withoutLabels) > 0 {
queries := strings.Split(withoutLabels, ",")
excluded := true
for _, query := range queries {
kv := strings.SplitN(query, "=", 2)
if len(kv) != 2 {
return false, fmt.Errorf("invalid query in without-labels: %s", query)
}
queryKey := kv[0]
queryValue := kv[1]
if value, exists := m.Spec.Labels[queryKey]; exists {
if value != queryValue {
excluded = false
break
}
} else {
excluded = false
break
}
}
if excluded {
return false, nil
}
}
if withoutRack := q["without-rack"]; len(withoutRack) > 0 {
withoutRacks := strings.Split(withoutRack, ",")
for _, wr := range withoutRacks {
if wr == fmt.Sprint(m.Spec.Rack) {
return false, nil
}
}
}
if withoutRole := q["without-role"]; len(withoutRole) > 0 {
withoutRoles := strings.Split(withoutRole, ",")
for _, wr := range withoutRoles {
if wr == fmt.Sprint(m.Spec.Role) {
return false, nil
}
}
}
if withoutBmc := q["without-bmc-type"]; len(withoutBmc) > 0 {
withoutBmcs := strings.Split(withoutBmc, ",")
for _, wb := range withoutBmcs {
if wb == fmt.Sprint(m.Spec.BMC.Type) {
return false, nil
}
}
}
if withoutState := q["without-state"]; len(withoutState) > 0 {
withoutStates := strings.Split(withoutState, ",")
for _, ws := range withoutStates {
if ws == fmt.Sprint(m.Status.State) {
return false, nil
}
}
}
return true, nil
}
// Serial returns value of serial in the query
func (q Query) Serial() string { return q["serial"] }
// Rack returns value of rack in the query
func (q Query) Rack() string { return q["rack"] }
// Role returns value of role in the query
func (q Query) Role() string { return q["role"] }
// IPv4 returns value of ipv4 in the query
func (q Query) IPv4() string { return q["ipv4"] }
// IPv6 returns value of ipv6 in the query
func (q Query) IPv6() string { return q["ipv6"] }
// BMCType returns value of bmc-type in the query
func (q Query) BMCType() string { return q["bmc-type"] }
// State returns value of state the query
func (q Query) State() string { return q["state"] }
// Labels return label's key and value combined with '='
func (q Query) Labels() []string {
queries := strings.Split(q["labels"], ",")
for idx, rawQuery := range queries {
queries[idx] = strings.TrimSpace(rawQuery)
}
return queries
}
// IsEmpty returns true if query is empty or no values are presented
func (q Query) IsEmpty() bool {
for _, v := range q {
if len(v) > 0 {
return false
}
}
return true
}
// RemoveWithout returns query removed --without key
func (q Query) HasOnlyWithout() bool {
for k, v := range q {
if !strings.HasPrefix(k, "without") && len(v) > 0 {
return false
}
}
return true
}
// Valid returns true if query isn't conflicted
func (q Query) Valid() bool {
hasWithoutSerial := q["without-serial"]
if hasSerial := q["serial"]; len(hasSerial) > 0 && len(hasWithoutSerial) > 0 {
return false
}
hasWithoutRack := q["without-rack"]
if hasRack := q["rack"]; len(hasRack) > 0 && len(hasWithoutRack) > 0 {
return false
}
hasWithoutRole := q["without-role"]
if hasRole := q["role"]; len(hasRole) > 0 && len(hasWithoutRole) > 0 {
return false
}
hasWithoutIPv4 := q["without-ipv4"]
if hasIPv4 := q["ipv4"]; len(hasIPv4) > 0 && len(hasWithoutIPv4) > 0 {
return false
}
hasWithoutIPv6 := q["without-ipv6"]
if hasIPv6 := q["ipv6"]; len(hasIPv6) > 0 && len(hasWithoutIPv6) > 0 {
return false
}
hasWithoutBMCType := q["without-bmc-type"]
if hasBMCType := q["bmc-type"]; len(hasBMCType) > 0 && len(hasWithoutBMCType) > 0 {
return false
}
hasWithoutState := q["without-state"]
if hasState := q["state"]; len(hasState) > 0 && len(hasWithoutState) > 0 {
return false
}
hasWithoutLabels := q["without-labels"]
if hasLabels := q["labels"]; len(hasLabels) > 0 && len(hasWithoutLabels) > 0 {
return false
}
return true
}