forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroplets.go
176 lines (141 loc) · 4.09 KB
/
droplets.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
package godo
import "fmt"
const dropletBasePath = "v2/droplets"
// DropletsService handles communication with the droplet related methods of the
// DigitalOcean API.
type DropletsService struct {
client *Client
}
// Droplet represents a DigitalOcean Droplet
type Droplet struct {
ID int `json:"id,float64,omitempty"`
Name string `json:"name,omitempty"`
Memory int `json:"memory,omitempty"`
Vcpus int `json:"vcpus,omitempty"`
Disk int `json:"disk,omitempty"`
Region *Region `json:"region,omitempty"`
Image *Image `json:"image,omitempty"`
Size *Size `json:"size,omitempty"`
BackupIDs []int `json:"backup_ids,omitempty"`
SnapshotIDs []int `json:"snapshot_ids,omitempty"`
Locked bool `json:"locked,bool,omitempty"`
Status string `json:"status,omitempty"`
Networks *Networks `json:"networks,omitempty"`
ActionIDs []int `json:"action_ids,omitempty"`
}
// Convert Droplet to a string
func (d Droplet) String() string {
return Stringify(d)
}
// DropletRoot represents a Droplet root
type DropletRoot struct {
Droplet *Droplet `json:"droplet"`
Links *Links `json:"links,omitempty"`
}
type dropletsRoot struct {
Droplets []Droplet `json:"droplets"`
}
// DropletCreateRequest represents a request to create a droplet.
type DropletCreateRequest struct {
Name string `json:"name"`
Region string `json:"region"`
Size string `json:"size"`
Image string `json:"image"`
SSHKeys []interface{} `json:"ssh_keys"`
}
func (d DropletCreateRequest) String() string {
return Stringify(d)
}
// Networks represents the droplet's networks
type Networks struct {
V4 []Network `json:"v4,omitempty"`
V6 []Network `json:"v6,omitempty"`
}
// Network represents a DigitalOcean Network
type Network struct {
IPAddress string `json:"ip_address,omitempty"`
Netmask string `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
}
func (n Network) String() string {
return Stringify(n)
}
// Links are extra links for a droplet
type Links struct {
Actions []Link `json:"actions,omitempty"`
}
// Action extracts Link
func (l *Links) Action(action string) *Link {
for _, a := range l.Actions {
if a.Rel == action {
return &a
}
}
return nil
}
// Link represents a link
type Link struct {
ID int `json:"id,omitempty"`
Rel string `json:"rel,omitempty"`
HREF string `json:"href,omitempty"`
}
// List all droplets
func (s *DropletsService) List() ([]Droplet, *Response, error) {
path := dropletBasePath
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
droplets := new(dropletsRoot)
resp, err := s.client.Do(req, droplets)
if err != nil {
return nil, resp, err
}
return droplets.Droplets, resp, err
}
// Get individual droplet
func (s *DropletsService) Get(dropletID int) (*DropletRoot, *Response, error) {
path := fmt.Sprintf("%s/%d", dropletBasePath, dropletID)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}
root := new(DropletRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// Create droplet
func (s *DropletsService) Create(createRequest *DropletCreateRequest) (*DropletRoot, *Response, error) {
path := dropletBasePath
req, err := s.client.NewRequest("POST", path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(DropletRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// Delete droplet
func (s *DropletsService) Delete(dropletID int) (*Response, error) {
path := fmt.Sprintf("%s/%d", dropletBasePath, dropletID)
req, err := s.client.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
return resp, err
}
func (s *DropletsService) dropletActionStatus(uri string) (string, error) {
action, _, err := s.client.DropletActions.GetByURI(uri)
if err != nil {
return "", err
}
return action.Status, nil
}