-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathmicro_droplets.go
More file actions
358 lines (301 loc) · 12.9 KB
/
Copy pathmicro_droplets.go
File metadata and controls
358 lines (301 loc) · 12.9 KB
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
349
350
351
352
353
354
355
356
357
358
package godo
import (
"context"
"fmt"
"net/http"
)
const microDropletBasePath = "v2/microdroplets/instances"
// MicroDropletState represents the lifecycle state of a MicroDroplet.
type MicroDropletState string
// Possible lifecycle states for a MicroDroplet.
const (
MicroDropletStateUnknown = MicroDropletState("unknown")
MicroDropletStateCreating = MicroDropletState("creating")
MicroDropletStateRunning = MicroDropletState("running")
MicroDropletStatePausing = MicroDropletState("pausing")
MicroDropletStatePaused = MicroDropletState("paused")
MicroDropletStateResuming = MicroDropletState("resuming")
MicroDropletStateTerminating = MicroDropletState("terminating")
MicroDropletStateTerminated = MicroDropletState("terminated")
MicroDropletStateFailed = MicroDropletState("failed")
)
// MicroDropletNetworking represents the networking mode of a MicroDroplet.
type MicroDropletNetworking string
// Possible networking modes for a MicroDroplet.
const (
MicroDropletNetworkingUnknown = MicroDropletNetworking("unknown")
MicroDropletNetworkingPublic = MicroDropletNetworking("public")
MicroDropletNetworkingVPC = MicroDropletNetworking("vpc")
)
// MicroDropletHTTPProtocol represents the HTTP protocol option for a MicroDroplet.
type MicroDropletHTTPProtocol string
// Possible HTTP protocol values for a MicroDroplet.
const (
MicroDropletHTTPProtocolHTTP = MicroDropletHTTPProtocol("http")
MicroDropletHTTPProtocolHTTP2 = MicroDropletHTTPProtocol("http2")
)
// MicroDropletCheckpointStatus represents the status of a MicroDroplet checkpoint.
type MicroDropletCheckpointStatus string
// Possible states for a MicroDroplet checkpoint.
const (
MicroDropletCheckpointStatusUnknown = MicroDropletCheckpointStatus("CHECKPOINT_UNKNOWN")
MicroDropletCheckpointStatusCreating = MicroDropletCheckpointStatus("CHECKPOINT_CREATING")
MicroDropletCheckpointStatusAvailable = MicroDropletCheckpointStatus("CHECKPOINT_AVAILABLE")
MicroDropletCheckpointStatusFailed = MicroDropletCheckpointStatus("CHECKPOINT_FAILED")
MicroDropletCheckpointStatusDeleted = MicroDropletCheckpointStatus("CHECKPOINT_DELETED")
)
// MicroDropletsService is an interface for interfacing with the MicroDroplet
// endpoints of the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/MicroDroplets
type MicroDropletsService interface {
List(ctx context.Context, opt *ListOptions) ([]MicroDroplet, *Response, error)
ListByRegion(ctx context.Context, region string, opt *ListOptions) ([]MicroDroplet, *Response, error)
ListByName(ctx context.Context, name string, opt *ListOptions) ([]MicroDroplet, *Response, error)
Get(ctx context.Context, id string) (*MicroDroplet, *Response, error)
Create(ctx context.Context, createRequest *MicroDropletCreateRequest) (*MicroDroplet, *Response, error)
Pause(ctx context.Context, id string) (*MicroDroplet, *Response, error)
Resume(ctx context.Context, id string) (*MicroDroplet, *Response, error)
Delete(ctx context.Context, id string) (*Response, error)
ListCheckpoints(ctx context.Context, id string, opt *ListOptions) ([]MicroDropletCheckpoint, *Response, error)
}
// MicroDropletsServiceOp handles communication with the MicroDroplet related
// methods of the DigitalOcean API.
type MicroDropletsServiceOp struct {
client *Client
}
var _ MicroDropletsService = &MicroDropletsServiceOp{}
// MicroDroplet represents a DigitalOcean MicroDroplet.
type MicroDroplet struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Region string `json:"region,omitempty"`
State MicroDropletState `json:"state,omitempty"`
Size string `json:"size,omitempty"`
Networking MicroDropletNetworking `json:"networking,omitempty"`
Image string `json:"image,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
AutoPause *AutoPauseConfig `json:"auto_pause,omitempty"`
AutoResume *bool `json:"auto_resume,omitempty"`
Created string `json:"created_at,omitempty"`
}
// AutoPauseConfig configures MicroDroplet auto-pause behavior. IdleTimeout is
// a Go duration string (e.g. "5m", "30s") describing how long the MicroDroplet
// must be idle before it is paused.
type AutoPauseConfig struct {
Enabled *bool `json:"enabled,omitempty"`
IdleTimeout string `json:"idle_timeout,omitempty"`
}
// MicroDropletCheckpoint represents a checkpoint of a MicroDroplet
// (persisted memory + disk state), captured automatically when the
// MicroDroplet is paused.
type MicroDropletCheckpoint struct {
ID string `json:"id,omitempty"`
MicroDropletID string `json:"micro_droplet_id,omitempty"`
Status MicroDropletCheckpointStatus `json:"status,omitempty"`
Name string `json:"name,omitempty"`
MemoryBytes uint64 `json:"memory_bytes,omitempty"`
DiskBytes uint64 `json:"disk_bytes,omitempty"`
Created string `json:"created_at,omitempty"`
}
// MicroDropletCreateRequest represents a request to create a MicroDroplet.
type MicroDropletCreateRequest struct {
Name string `json:"name"`
Region string `json:"region"`
Size string `json:"size"`
Image string `json:"image"`
Networking MicroDropletNetworking `json:"networking,omitempty"`
VPCUUID string `json:"vpc_uuid,omitempty"`
AutoPause *AutoPauseConfig `json:"auto_pause,omitempty"`
AutoResume *bool `json:"auto_resume,omitempty"`
HTTPPort uint32 `json:"http_port,omitempty"`
HTTPProtocol MicroDropletHTTPProtocol `json:"http_protocol,omitempty"`
Environment map[string]string `json:"environment,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// String returns a human-readable description of a MicroDroplet.
func (m MicroDroplet) String() string {
return Stringify(m)
}
// URN returns the MicroDroplet ID in a valid DO API URN form.
func (m MicroDroplet) URN() string {
return ToURN("MicroDroplet", m.ID)
}
// String returns a human-readable description of a MicroDropletCheckpoint.
func (c MicroDropletCheckpoint) String() string {
return Stringify(c)
}
// String returns a human-readable description of a MicroDropletCreateRequest.
func (r MicroDropletCreateRequest) String() string {
return Stringify(r)
}
type microDropletRoot struct {
MicroDroplet *MicroDroplet `json:"micro_droplet"`
}
type microDropletsRoot struct {
MicroDroplets []MicroDroplet `json:"micro_droplets"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type microDropletCheckpointsRoot struct {
Checkpoints []MicroDropletCheckpoint `json:"checkpoints"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// listMicroDropletOptions holds MicroDroplet-specific list filters that are
// not part of the shared ListOptions.
type listMicroDropletOptions struct {
Region string `url:"region,omitempty"`
Name string `url:"name,omitempty"`
}
// List lists all MicroDroplets, with optional pagination.
func (s *MicroDropletsServiceOp) List(ctx context.Context, opt *ListOptions) ([]MicroDroplet, *Response, error) {
return s.list(ctx, opt, nil)
}
// ListByRegion lists MicroDroplets filtered by region slug, with optional pagination.
func (s *MicroDropletsServiceOp) ListByRegion(ctx context.Context, region string, opt *ListOptions) ([]MicroDroplet, *Response, error) {
if region == "" {
return nil, nil, NewArgError("region", "cannot be empty")
}
return s.list(ctx, opt, &listMicroDropletOptions{Region: region})
}
// ListByName lists MicroDroplets filtered by exact name match, with optional pagination.
func (s *MicroDropletsServiceOp) ListByName(ctx context.Context, name string, opt *ListOptions) ([]MicroDroplet, *Response, error) {
if name == "" {
return nil, nil, NewArgError("name", "cannot be empty")
}
return s.list(ctx, opt, &listMicroDropletOptions{Name: name})
}
func (s *MicroDropletsServiceOp) list(ctx context.Context, opt *ListOptions, listOpt *listMicroDropletOptions) ([]MicroDroplet, *Response, error) {
path := microDropletBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
path, err = addOptions(path, listOpt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.MicroDroplets, resp, nil
}
// Get retrieves a MicroDroplet by its ID.
func (s *MicroDropletsServiceOp) Get(ctx context.Context, id string) (*MicroDroplet, *Response, error) {
if id == "" {
return nil, nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", microDropletBasePath, id)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.MicroDroplet, resp, nil
}
// Create provisions a new MicroDroplet with the provided configuration.
func (s *MicroDropletsServiceOp) Create(ctx context.Context, createRequest *MicroDropletCreateRequest) (*MicroDroplet, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := s.client.NewRequest(ctx, http.MethodPost, microDropletBasePath, createRequest)
if err != nil {
return nil, nil, err
}
root := new(microDropletRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.MicroDroplet, resp, nil
}
// Pause synchronously transitions a RUNNING MicroDroplet to PAUSED. It blocks
// until the platform has durably paused the MicroDroplet and returns the
// updated resource. The call is idempotent: pausing a MicroDroplet that is
// already PAUSED returns the current MicroDroplet with no side effects.
func (s *MicroDropletsServiceOp) Pause(ctx context.Context, id string) (*MicroDroplet, *Response, error) {
return s.doTransition(ctx, id, "pause")
}
// Resume synchronously transitions a PAUSED MicroDroplet to RUNNING. It blocks
// until the platform has durably resumed the MicroDroplet and returns the
// updated resource. The call is idempotent: resuming a MicroDroplet that is
// already RUNNING returns the current MicroDroplet with no side effects.
func (s *MicroDropletsServiceOp) Resume(ctx context.Context, id string) (*MicroDroplet, *Response, error) {
return s.doTransition(ctx, id, "resume")
}
// doTransition posts an empty body to a MicroDroplet transition sub-resource
// (e.g. /pause, /resume) and decodes the returned MicroDroplet.
func (s *MicroDropletsServiceOp) doTransition(ctx context.Context, id, action string) (*MicroDroplet, *Response, error) {
if id == "" {
return nil, nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s/%s", microDropletBasePath, id, action)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.MicroDroplet, resp, nil
}
// Delete removes a MicroDroplet by its ID. The DigitalOcean API returns a 204
// on success and does not include a response body.
func (s *MicroDropletsServiceOp) Delete(ctx context.Context, id string) (*Response, error) {
if id == "" {
return nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", microDropletBasePath, id)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// ListCheckpoints lists checkpoints that belong to a MicroDroplet.
// Checkpoints are captured automatically by DigitalOcean when a MicroDroplet
// is paused; each one preserves the memory and disk state required to resume.
func (s *MicroDropletsServiceOp) ListCheckpoints(ctx context.Context, id string, opt *ListOptions) ([]MicroDropletCheckpoint, *Response, error) {
if id == "" {
return nil, nil, NewArgError("id", "cannot be empty")
}
path := fmt.Sprintf("%s/%s/checkpoints", microDropletBasePath, id)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(microDropletCheckpointsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Checkpoints, resp, nil
}