forked from Netflix/chaosmonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grp.go
229 lines (194 loc) · 5.01 KB
/
grp.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
// Copyright 2016 Netflix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package grp holds the InstanceGroup interface
package grp
import (
"bytes"
"encoding/json"
)
// New generates an InstanceGroup.
// region, stack, and cluster may be empty strings, in which case
// the group is cross-region, cross-stack, or cross-cluster
// Note that stack and cluster are mutually exclusive, can specify one
// but not both
func New(app, account, region, stack, cluster string) InstanceGroup {
return group{
app: app,
account: account,
region: region,
stack: stack,
cluster: cluster,
}
}
// InstanceGroup represents a group of instances
type InstanceGroup interface {
// App returns the name of the app
App() string
// Account returns the name of the app
Account() string
// Region returns (region name, region present)
// If the group is cross-region, the boolean will be false
Region() (name string, ok bool)
// Stack returns (region name, region present)
// If the group is cross-stack, the boolean will be false
Stack() (name string, ok bool)
// Cluster returns (cluster name, cluster present)
// If the group is cross-cluster, the boolean will be false
Cluster() (name string, ok bool)
}
// Equal returns true if g1 and g2 represent the same group of instances
func Equal(g1, g2 InstanceGroup) bool {
if g1 == g2 {
return true
}
if g1.App() != g2.App() {
return false
}
if g1.Account() != g2.Account() {
return false
}
r1, ok1 := g1.Region()
r2, ok2 := g2.Region()
if ok1 != ok2 {
return false
}
if ok1 && (r1 != r2) {
return false
}
s1, ok1 := g1.Stack()
s2, ok2 := g2.Stack()
if ok1 != ok2 {
return false
}
if ok1 && (s1 != s2) {
return false
}
c1, ok1 := g1.Cluster()
c2, ok2 := g2.Cluster()
if ok1 != ok2 {
return false
}
if ok1 && (c1 != c2) {
return false
}
return true
}
// String outputs a string representation of InstanceGroup suitable for logging
func String(group InstanceGroup) string {
var buffer bytes.Buffer
writeString := func(s string) {
_, _ = buffer.WriteString(s)
}
writeString("app=")
writeString(group.App())
writeString(" account=")
writeString(group.Account())
region, ok := group.Region()
if ok {
writeString(" region=")
writeString(region)
}
stack, ok := group.Stack()
if ok {
writeString(" stack=")
writeString(stack)
}
cluster, ok := group.Cluster()
if ok {
writeString(" cluster=")
writeString(cluster)
}
return buffer.String()
}
type group struct {
app, account, region, stack, cluster string
}
func (g group) MarshalJSON() ([]byte, error) {
var s = struct {
App string `json:"app"`
Account string `json:"account"`
Region string `json:"region,omitempty"`
Stack string `json:"stack,omitempty"`
Cluster string `json:"cluster,omitempty"`
}{
App: g.app,
Account: g.account,
Region: g.region,
Stack: g.stack,
Cluster: g.cluster,
}
return json.Marshal(s)
}
// App implements InstanceGroup.App
func (g group) App() string {
return g.app
}
// Account implements InstanceGroup.Account
func (g group) Account() string {
return g.account
}
// Region implements InstanceGroup.Region
func (g group) Region() (string, bool) {
if g.region == "" {
return "", false
}
return g.region, true
}
// Stack implements InstanceGroup.Stack
func (g group) Stack() (string, bool) {
if g.stack == "" {
return "", false
}
return g.stack, true
}
// Cluster implements InstanceGroup.Cluster
func (g group) Cluster() (string, bool) {
if g.cluster == "" {
return "", false
}
return g.cluster, true
}
// AnyRegion is true if the group matches any region
func AnyRegion(g InstanceGroup) bool {
_, specific := g.Region()
return !specific
}
// AnyStack is true if the group matches any region
func AnyStack(g InstanceGroup) bool {
_, specific := g.Stack()
return !specific
}
// AnyCluster is true if the group matches any region
func AnyCluster(g InstanceGroup) bool {
_, specific := g.Cluster()
return !specific
}
// Contains returns true if the asg/instance with
// matching app, account, region, stack and cluster
// are elements of this instance group
func Contains(g InstanceGroup, app, account, region, stack, cluster string) bool {
return app == g.App() &&
account == g.Account() &&
(AnyRegion(g) || region == must(g.Region())) &&
(AnyStack(g) || stack == must(g.Stack())) &&
(AnyCluster(g) || cluster == must(g.Cluster()))
}
// must returns val if ok is true
// panics otherwise
func must(val string, specific bool) string {
if !specific {
panic("specific was unexpectedly false")
}
return val
}