-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathautoscaling_schedule.go
140 lines (117 loc) · 4.04 KB
/
autoscaling_schedule.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
package gobizfly
import (
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
)
// AutoScalingSize - size of auto scaling group
type AutoScalingSize struct {
DesiredCapacity int `json:"desired_capacity"`
MaxSize int `json:"max_size"`
MinSize int `json:"min_size"`
}
// AutoScalingScheduleValid - represents for a validation time of cron triggers
type autoScalingScheduleValid struct {
From string `json:"_from"`
To *string `json:"_to,omitempty"`
}
// AutoScalingScheduleInputs - represents for a input of cron triggers
type autoScalingScheduleInputs struct {
CronPattern string `json:"cron_pattern"`
Inputs AutoScalingSize `json:"inputs"`
}
// AutoScalingScheduleSizing - represents for phase time of cron triggers
type autoScalingScheduleSizing struct {
From autoScalingScheduleInputs `json:"_from"`
To autoScalingScheduleInputs `json:"_to,omitempty"`
Type string `json:"_type"`
}
// AutoScalingScheduleCreateRequest - payload use create a scheduler (cron trigger)
type autoScalingScheduleCreateRequest struct {
Name string `json:"name"`
Sizing autoScalingScheduleSizing `json:"sizing"`
Valid autoScalingScheduleValid `json:"valid"`
}
// AutoScalingSchedule - cron triggers to do time-based scale
type AutoScalingSchedule struct {
ClusterID string `json:"cluster_id"`
Created string `json:"created_at"`
ID string `json:"_id"`
Name string `json:"name"`
Sizing autoScalingScheduleSizing `json:"sizing"`
Status string `json:"status"`
TaskID string `json:"task_id"`
Valid autoScalingScheduleValid `json:"valid"`
NextExecutionTime string `json:"next_execution_time"`
}
// List - list all cron triggers of a cluster
func (s *schedule) List(ctx context.Context, clusterID string) ([]*AutoScalingSchedule, error) {
if clusterID == "" {
return nil, errors.New("Auto Scaling Group ID is required")
}
req, err := s.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, s.resourcePath(clusterID), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
AutoScalingSchdeules []*AutoScalingSchedule `json:"cron_triggers"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.AutoScalingSchdeules, nil
}
// Get - get a cron trigger of a cluster
func (s *schedule) Get(ctx context.Context, clusterID, scheduleID string) (*AutoScalingSchedule, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, autoScalingServiceName, s.itemPath(clusterID, scheduleID), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data = &AutoScalingSchedule{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
// Delete - delete a cron trigger of a cluster
func (s *schedule) Delete(ctx context.Context, clusterID, scheduleID string) error {
req, err := s.client.NewRequest(ctx, http.MethodDelete, autoScalingServiceName, s.itemPath(clusterID, scheduleID), nil)
if err != nil {
return err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}
// Create - create a cron trigger of a cluster
func (s *schedule) Create(ctx context.Context, clusterID string, asscr *autoScalingScheduleCreateRequest) (*TaskResponses, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, autoScalingServiceName, s.resourcePath(clusterID), &asscr)
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req)
if err != nil {
return nil, err
}
data := &TaskResponses{}
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}