-
Notifications
You must be signed in to change notification settings - Fork 303
/
droplet_autoscale.go
258 lines (234 loc) · 9.84 KB
/
droplet_autoscale.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
package godo
import (
"context"
"fmt"
"net/http"
"time"
)
const (
dropletAutoscaleBasePath = "/v2/droplets/autoscale"
)
// DropletAutoscaleService defines an interface for managing droplet autoscale pools through DigitalOcean API
type DropletAutoscaleService interface {
Create(context.Context, *DropletAutoscalePoolRequest) (*DropletAutoscalePool, *Response, error)
Get(context.Context, string) (*DropletAutoscalePool, *Response, error)
List(context.Context, *ListOptions) ([]*DropletAutoscalePool, *Response, error)
ListMembers(context.Context, string, *ListOptions) ([]*DropletAutoscaleResource, *Response, error)
ListHistory(context.Context, string, *ListOptions) ([]*DropletAutoscaleHistoryEvent, *Response, error)
Update(context.Context, string, *DropletAutoscalePoolRequest) (*DropletAutoscalePool, *Response, error)
Delete(context.Context, string) (*Response, error)
DeleteDangerous(context.Context, string) (*Response, error)
}
// DropletAutoscalePool represents a DigitalOcean droplet autoscale pool
type DropletAutoscalePool struct {
ID string `json:"id"`
Name string `json:"name"`
Config *DropletAutoscaleConfiguration `json:"config"`
DropletTemplate *DropletAutoscaleResourceTemplate `json:"droplet_template"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CurrentUtilization *DropletAutoscaleResourceUtilization `json:"current_utilization,omitempty"`
Status string `json:"status"`
}
// DropletAutoscaleConfiguration represents a DigitalOcean droplet autoscale pool configuration
type DropletAutoscaleConfiguration struct {
MinInstances uint64 `json:"min_instances,omitempty"`
MaxInstances uint64 `json:"max_instances,omitempty"`
TargetCPUUtilization float64 `json:"target_cpu_utilization,omitempty"`
TargetMemoryUtilization float64 `json:"target_memory_utilization,omitempty"`
CooldownMinutes uint32 `json:"cooldown_minutes,omitempty"`
TargetNumberInstances uint64 `json:"target_number_instances,omitempty"`
}
// DropletAutoscaleResourceTemplate represents a DigitalOcean droplet autoscale pool resource template
type DropletAutoscaleResourceTemplate struct {
Size string `json:"size"`
Region string `json:"region"`
Image string `json:"image"`
Tags []string `json:"tags"`
SSHKeys []string `json:"ssh_keys"`
VpcUUID string `json:"vpc_uuid"`
WithDropletAgent bool `json:"with_droplet_agent"`
ProjectID string `json:"project_id"`
IPV6 bool `json:"ipv6"`
UserData string `json:"user_data"`
}
// DropletAutoscaleResourceUtilization represents a DigitalOcean droplet autoscale pool resource utilization
type DropletAutoscaleResourceUtilization struct {
Memory float64 `json:"memory,omitempty"`
CPU float64 `json:"cpu,omitempty"`
}
// DropletAutoscaleResource represents a DigitalOcean droplet autoscale pool resource
type DropletAutoscaleResource struct {
DropletID uint64 `json:"droplet_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
HealthStatus string `json:"health_status"`
UnhealthyReason string `json:"unhealthy_reason,omitempty"`
Status string `json:"status"`
CurrentUtilization *DropletAutoscaleResourceUtilization `json:"current_utilization,omitempty"`
}
// DropletAutoscaleHistoryEvent represents a DigitalOcean droplet autoscale pool history event
type DropletAutoscaleHistoryEvent struct {
HistoryEventID string `json:"history_event_id"`
CurrentInstanceCount uint64 `json:"current_instance_count"`
DesiredInstanceCount uint64 `json:"desired_instance_count"`
Reason string `json:"reason"`
Status string `json:"status"`
ErrorReason string `json:"error_reason,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// DropletAutoscalePoolRequest represents a DigitalOcean droplet autoscale pool create/update request
type DropletAutoscalePoolRequest struct {
Name string `json:"name"`
Config *DropletAutoscaleConfiguration `json:"config"`
DropletTemplate *DropletAutoscaleResourceTemplate `json:"droplet_template"`
}
type dropletAutoscalePoolRoot struct {
AutoscalePool *DropletAutoscalePool `json:"autoscale_pool"`
}
type dropletAutoscalePoolsRoot struct {
AutoscalePools []*DropletAutoscalePool `json:"autoscale_pools"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type dropletAutoscaleMembersRoot struct {
Droplets []*DropletAutoscaleResource `json:"droplets"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type dropletAutoscaleHistoryEventsRoot struct {
History []*DropletAutoscaleHistoryEvent `json:"history"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// DropletAutoscaleServiceOp handles communication with droplet autoscale-related methods of the DigitalOcean API
type DropletAutoscaleServiceOp struct {
client *Client
}
var _ DropletAutoscaleService = &DropletAutoscaleServiceOp{}
// Create a new droplet autoscale pool
func (d *DropletAutoscaleServiceOp) Create(ctx context.Context, createReq *DropletAutoscalePoolRequest) (*DropletAutoscalePool, *Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodPost, dropletAutoscaleBasePath, createReq)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscalePoolRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
return root.AutoscalePool, resp, nil
}
// Get an existing droplet autoscale pool
func (d *DropletAutoscaleServiceOp) Get(ctx context.Context, id string) (*DropletAutoscalePool, *Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s/%s", dropletAutoscaleBasePath, id), nil)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscalePoolRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
return root.AutoscalePool, resp, err
}
// List all existing droplet autoscale pools
func (d *DropletAutoscaleServiceOp) List(ctx context.Context, opts *ListOptions) ([]*DropletAutoscalePool, *Response, error) {
path, err := addOptions(dropletAutoscaleBasePath, opts)
if err != nil {
return nil, nil, err
}
req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscalePoolsRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
if root.Links != nil {
resp.Links = root.Links
}
if root.Meta != nil {
resp.Meta = root.Meta
}
return root.AutoscalePools, resp, err
}
// ListMembers all members for an existing droplet autoscale pool
func (d *DropletAutoscaleServiceOp) ListMembers(ctx context.Context, id string, opts *ListOptions) ([]*DropletAutoscaleResource, *Response, error) {
path, err := addOptions(fmt.Sprintf("%s/%s/members", dropletAutoscaleBasePath, id), opts)
if err != nil {
return nil, nil, err
}
req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscaleMembersRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
if root.Links != nil {
resp.Links = root.Links
}
if root.Meta != nil {
resp.Meta = root.Meta
}
return root.Droplets, resp, err
}
// ListHistory all history events for an existing droplet autoscale pool
func (d *DropletAutoscaleServiceOp) ListHistory(ctx context.Context, id string, opts *ListOptions) ([]*DropletAutoscaleHistoryEvent, *Response, error) {
path, err := addOptions(fmt.Sprintf("%s/%s/history", dropletAutoscaleBasePath, id), opts)
if err != nil {
return nil, nil, err
}
req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscaleHistoryEventsRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
if root.Links != nil {
resp.Links = root.Links
}
if root.Meta != nil {
resp.Meta = root.Meta
}
return root.History, resp, err
}
// Update an existing autoscale pool
func (d *DropletAutoscaleServiceOp) Update(ctx context.Context, id string, updateReq *DropletAutoscalePoolRequest) (*DropletAutoscalePool, *Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodPut, fmt.Sprintf("%s/%s", dropletAutoscaleBasePath, id), updateReq)
if err != nil {
return nil, nil, err
}
root := new(dropletAutoscalePoolRoot)
resp, err := d.client.Do(ctx, req, root)
if err != nil {
return nil, nil, err
}
return root.AutoscalePool, resp, nil
}
// Delete an existing autoscale pool
func (d *DropletAutoscaleServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s", dropletAutoscaleBasePath, id), nil)
if err != nil {
return nil, err
}
return d.client.Do(ctx, req, nil)
}
// DeleteDangerous deletes an existing autoscale pool with all underlying resources
func (d *DropletAutoscaleServiceOp) DeleteDangerous(ctx context.Context, id string) (*Response, error) {
req, err := d.client.NewRequest(ctx, http.MethodDelete, fmt.Sprintf("%s/%s/dangerous", dropletAutoscaleBasePath, id), nil)
req.Header.Set("X-Dangerous", "true")
if err != nil {
return nil, err
}
return d.client.Do(ctx, req, nil)
}