-
Notifications
You must be signed in to change notification settings - Fork 12
/
autoscaling.go
348 lines (281 loc) · 8.88 KB
/
autoscaling.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
const (
statusActive = "Active"
statusError = "Error"
policyTypeDeletion = "deletion"
policyTypeAutoScaling = "autoscaling"
)
const (
autoscalingGroupResourcePath = "/groups"
eventsResourcePath = "/events"
launchConfigurationsResourcePath = "/launch_configs"
nodesResourcePath = "nodes"
policiesResourcePath = "policies"
quotasResourcePath = "/quotas"
schedulesResourcePath = "cron_triggers"
suggestionResourcePath = "/suggestion"
tasksResourcePath = "/task"
usingResourcePath = "/using_resource"
webhooksResourcePath = "webhooks"
)
var (
actionTypeSupportedWebhooks = []string{
"CLUSTER SCALE IN",
"CLUSTER SCALE OUT",
}
networkPlan = []string{
"free_datatransfer",
"free_bandwidth",
}
)
var _ AutoScalingService = (*autoscalingService)(nil)
type autoscalingService struct {
client *Client
}
/*
AutoScalingService is interface wrap others resource's interfaces. Includes:
1. AutoScalingGroups: Provides function interact with an autoscaling group such as:
Create, Update, Delete
2. Events: Provides function to list events of an autoscaling group
3. LaunchConfigurations: Provides function to interact with launch configurations
4. Nodes: Provides function to interact with members of autoscaling group
5. Policies: Provides function to interact with autoscaling policies of autoscaling group
6. Schedules: Provides function to interact with schedule for auto scaling
7. Tasks: Provides function to get information of task
8. Webhooks: Provides fucntion to list webhook triggers scale of autoscaling group
*/
type AutoScalingService interface {
AutoScalingGroups() *autoScalingGroup
Common() *common
Events() *event
LaunchConfigurations() *launchConfiguration
Nodes() *node
Policies() *policy
Schedules() *schedule
Tasks() *task
Webhooks() *webhook
}
func (as *autoscalingService) AutoScalingGroups() *autoScalingGroup {
return &autoScalingGroup{client: as.client}
}
func (as *autoscalingService) LaunchConfigurations() *launchConfiguration {
return &launchConfiguration{client: as.client}
}
func (as *autoscalingService) Webhooks() *webhook {
return &webhook{client: as.client}
}
func (as *autoscalingService) Events() *event {
return &event{client: as.client}
}
func (as *autoscalingService) Nodes() *node {
return &node{client: as.client}
}
func (as *autoscalingService) Policies() *policy {
return &policy{client: as.client}
}
func (as *autoscalingService) Schedules() *schedule {
return &schedule{client: as.client}
}
func (as *autoscalingService) Tasks() *task {
return &task{client: as.client}
}
func (as *autoscalingService) Common() *common {
return &common{client: as.client}
}
type autoScalingGroup struct {
client *Client
}
type launchConfiguration struct {
client *Client
}
type webhook struct {
client *Client
}
type event struct {
client *Client
}
type policy struct {
client *Client
}
type node struct {
client *Client
}
type schedule struct {
client *Client
}
type task struct {
client *Client
}
type common struct {
client *Client
}
// taskResult - Struct of data was returned by workers
type taskResult struct {
Action string `json:"action,omitempty"`
Data interface{} `json:"data,omitempty"`
Message string `json:"message,omitempty"`
Progress int `json:"progress,omitempty"`
Success bool `json:"success,omitempty"`
}
// ASTask is information of doing task
type ASTask struct {
Ready bool `json:"ready"`
Result taskResult `json:"result"`
}
// usingResource - list snapshot, ssh key using to create launch configurations
type usingResource struct {
SSHKeys []string `json:"ssh_keys"`
Snapshots []string `json:"snapshots"`
}
// usingResource - list snapshot, ssh key using to create launch configurations
type autoscalingQuotas struct {
Availability map[string]int `json:"can_create,omitempty"`
Limited map[string]int `json:"limited,omitempty"`
Valid bool `json:"valid"`
}
// Auto Scaling Group path
func (asg *autoScalingGroup) resourcePath() string {
return autoscalingGroupResourcePath
}
func (asg *autoScalingGroup) itemPath(id string) string {
return strings.Join([]string{autoscalingGroupResourcePath, id}, "/")
}
// Launch Configurations path
func (lc *launchConfiguration) resourcePath() string {
return launchConfigurationsResourcePath
}
func (lc *launchConfiguration) itemPath(id string) string {
return strings.Join([]string{launchConfigurationsResourcePath, id}, "/")
}
// Webhook path
func (wh *webhook) resourcePath(clusterID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, webhooksResourcePath}, "/")
}
// Events path
func (e *event) resourcePath(clusterID string, page, total int) string {
return eventsResourcePath
}
// Policy path
func (p *policy) resourcePath(clusterID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, policiesResourcePath}, "/")
}
func (p *policy) itemPath(clusterID, policyID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, policiesResourcePath, policyID}, "/")
}
// Node path
func (n *node) resourcePath(clusterID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, nodesResourcePath}, "/")
}
// Schedule path
func (s *schedule) resourcePath(clusterID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, schedulesResourcePath}, "/")
}
func (s *schedule) itemPath(clusterID, scheduleID string) string {
return strings.Join([]string{autoscalingGroupResourcePath, clusterID, schedulesResourcePath, scheduleID}, "/")
}
// Task path
func (t *task) resourcePath(taskID string) string {
return strings.Join([]string{tasksResourcePath, taskID, "status"}, "/")
}
// Using Resource path
func (c *common) usingResourcePath() string {
return usingResourcePath
}
func getQuotasResourcePath() string {
return quotasResourcePath
}
func getSuggestionResourcePath() string {
return suggestionResourcePath
}
func (t *task) Get(ctx context.Context, taskID string) (*ASTask, error) {
req, err := t.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, t.resourcePath(taskID), nil)
if err != nil {
return nil, err
}
resp, err := t.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data = &ASTask{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
// Common
func (c *common) AutoScalingUsingResource(ctx context.Context) (*usingResource, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, c.usingResourcePath(), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
data := &usingResource{}
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}
func (c *common) AutoScalingIsValidQuotas(ctx context.Context, clusterID, ProfileID string, desiredCapacity, maxSize int) (bool, error) {
return isValidQuotas(ctx, c.client, clusterID, ProfileID, desiredCapacity, maxSize)
}
func isValidQuotas(ctx context.Context, client *Client, clusterID, ProfileID string, desiredCapacity, maxSize int) (bool, error) {
payload := map[string]interface{}{
"desired_capacity": desiredCapacity,
"max_size": maxSize,
"profile_id": ProfileID,
}
if clusterID != "" {
payload["cluster_id"] = clusterID
}
req, err := client.NewRequest(ctx, http.MethodPost, autoScalingServiceName, getQuotasResourcePath(), &payload)
if err != nil {
return false, err
}
resp, err := client.Do(ctx, req)
if err != nil {
return false, err
}
var data struct {
Quotas autoscalingQuotas `json:"message"`
}
// data := &map[string]interface{}{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return false, err
}
return data.Quotas.Valid, nil
}
func (c *common) AutoScalingGetSuggestion(ctx context.Context, ProfileID string, desiredCapacity, maxSize int) (interface{}, error) {
return getSuggestion(ctx, c.client, ProfileID, desiredCapacity, maxSize)
}
// getSuggestion do get suggestion
func getSuggestion(ctx context.Context, client *Client, ProfileID string, desiredCapacity, maxSize int) (interface{}, error) {
payload := map[string]interface{}{
"desired_capacity": desiredCapacity,
"max_size": maxSize,
"profile_id": ProfileID,
}
req, err := client.NewRequest(ctx, http.MethodPost, autoScalingServiceName, getSuggestionResourcePath(), &payload)
if err != nil {
return nil, err
}
resp, err := client.Do(ctx, req)
if err != nil {
return nil, err
}
data := map[string]interface{}{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}