forked from civo/civogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.go
351 lines (302 loc) · 11.3 KB
/
instance.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
349
350
351
package civogo
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/civo/civogo/utils"
)
// Instance represents a virtual server within Civo's infrastructure
type Instance struct {
ID string `json:"id,omitempty"`
OpenstackServerID string `json:"openstack_server_id,omitempty"`
Hostname string `json:"hostname,omitempty"`
ReverseDNS string `json:"reverse_dns,omitempty"`
Size string `json:"size,omitempty"`
Region string `json:"region,omitempty"`
NetworkID string `json:"network_id,omitempty"`
PrivateIP string `json:"private_ip,omitempty"`
PublicIP string `json:"public_ip,omitempty"`
PseudoIP string `json:"pseudo_ip,omitempty"`
TemplateID string `json:"template_id,omitempty"`
SourceType string `json:"source_type,omitempty"`
SourceID string `json:"source_id,omitempty"`
SnapshotID string `json:"snapshot_id,omitempty"`
InitialUser string `json:"initial_user,omitempty"`
InitialPassword string `json:"initial_password,omitempty"`
SSHKey string `json:"ssh_key,omitempty"`
Status string `json:"status,omitempty"`
Notes string `json:"notes,omitempty"`
FirewallID string `json:"firewall_id,omitempty"`
Tags []string `json:"tags,omitempty"`
CivostatsdToken string `json:"civostatsd_token,omitempty"`
CivostatsdStats string `json:"civostatsd_stats,omitempty"`
CivostatsdStatsPerMinute []string `json:"civostatsd_stats_per_minute,omitempty"`
CivostatsdStatsPerHour []string `json:"civostatsd_stats_per_hour,omitempty"`
OpenstackImageID string `json:"openstack_image_id,omitempty"`
RescuePassword string `json:"rescue_password,omitempty"`
VolumeBacked bool `json:"volume_backed,omitempty"`
CPUCores int `json:"cpu_cores,omitempty"`
RAMMegabytes int `json:"ram_mb,omitempty"`
DiskGigabytes int `json:"disk_gb,omitempty"`
Script string `json:"script,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
//"cpu_cores":1,"ram_mb":2048,"disk_gb":25
// InstanceConsole represents a link to a webconsole for an instances
type InstanceConsole struct {
URL string `json:"url"`
}
// PaginatedInstanceList returns a paginated list of Instance object
type PaginatedInstanceList struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Pages int `json:"pages"`
Items []Instance `json:"items"`
}
// InstanceConfig describes the parameters for a new instance
// none of the fields are mandatory and will be automatically
// set with default values
type InstanceConfig struct {
Count int `json:"count"`
Hostname string `json:"hostname"`
ReverseDNS string `json:"reverse_dns"`
Size string `json:"size"`
Region string `json:"region"`
PublicIPRequired string `json:"public_ip"`
NetworkID string `json:"network_id"`
TemplateID string `json:"template_id"`
SourceType string `json:"source_type"`
SourceID string `json:"source_id"`
SnapshotID string `json:"snapshot_id"`
InitialUser string `json:"initial_user"`
SSHKeyID string `json:"ssh_key_id"`
Script string `json:"script"`
Tags []string `json:"-"`
TagsList string `json:"tags"`
}
// ListInstances returns a page of Instances owned by the calling API account
func (c *Client) ListInstances(page int, perPage int) (*PaginatedInstanceList, error) {
url := "/v2/instances"
if page != 0 && perPage != 0 {
url = url + fmt.Sprintf("?page=%d&per_page=%d", page, perPage)
}
resp, err := c.SendGetRequest(url)
if err != nil {
return nil, decodeERROR(err)
}
PaginatedInstances := PaginatedInstanceList{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&PaginatedInstances)
return &PaginatedInstances, err
}
// ListAllInstances returns all (well, upto 99,999,999 instances) Instances owned by the calling API account
func (c *Client) ListAllInstances() ([]Instance, error) {
instances, err := c.ListInstances(1, 99999999)
if err != nil {
return []Instance{}, decodeERROR(err)
}
return instances.Items, nil
}
// FindInstance finds a instance by either part of the ID or part of the hostname
func (c *Client) FindInstance(search string) (*Instance, error) {
instances, err := c.ListAllInstances()
if err != nil {
return nil, decodeERROR(err)
}
exactMatch := false
partialMatchesCount := 0
result := Instance{}
for _, value := range instances {
if value.Hostname == search || value.ID == search {
exactMatch = true
result = value
} else if strings.Contains(value.Hostname, search) || strings.Contains(value.ID, search) {
if !exactMatch {
result = value
partialMatchesCount++
}
}
}
if exactMatch || partialMatchesCount == 1 {
return &result, nil
} else if partialMatchesCount > 1 {
err := fmt.Errorf("unable to find %s because there were multiple matches", search)
return nil, MultipleMatchesError.wrap(err)
} else {
err := fmt.Errorf("unable to find %s, zero matches", search)
return nil, ZeroMatchesError.wrap(err)
}
}
// GetInstance returns a single Instance by its full ID
func (c *Client) GetInstance(id string) (*Instance, error) {
resp, err := c.SendGetRequest("/v2/instances/" + id)
if err != nil {
return nil, decodeERROR(err)
}
instance := Instance{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&instance)
return &instance, err
}
// NewInstanceConfig returns an initialized config for a new instance
func (c *Client) NewInstanceConfig() (*InstanceConfig, error) {
network, err := c.GetDefaultNetwork()
if err != nil {
return nil, decodeERROR(err)
}
template, err := c.GetTemplateByCode("ubuntu-18.04")
if err != nil {
return nil, decodeERROR(err)
}
return &InstanceConfig{
Count: 1,
Hostname: utils.RandomName(),
ReverseDNS: "",
Size: "g2.xsmall",
Region: c.Region,
PublicIPRequired: "true",
NetworkID: network.ID,
TemplateID: template.ID,
SnapshotID: "",
InitialUser: "civo",
SSHKeyID: "",
Script: "",
Tags: []string{""},
}, nil
}
// CreateInstance creates a new instance in the account
func (c *Client) CreateInstance(config *InstanceConfig) (*Instance, error) {
config.TagsList = strings.Join(config.Tags, " ")
body, err := c.SendPostRequest("/v2/instances", config)
if err != nil {
return nil, decodeERROR(err)
}
var instance Instance
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&instance); err != nil {
return nil, err
}
return &instance, nil
}
// SetInstanceTags sets the tags for the specified instance
func (c *Client) SetInstanceTags(i *Instance, tags string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/tags", i.ID), map[string]string{
"tags": tags,
"region": c.Region,
})
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// UpdateInstance updates an Instance's hostname, reverse DNS or notes
func (c *Client) UpdateInstance(i *Instance) (*SimpleResponse, error) {
params := map[string]string{
"hostname": i.Hostname,
"reverse_dns": i.ReverseDNS,
"notes": i.Notes,
"region": c.Region,
}
if i.Notes == "" {
params["notes_delete"] = "true"
}
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s", i.ID), params)
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// DeleteInstance deletes an instance and frees its resources
func (c *Client) DeleteInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest("/v2/instances/" + id)
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// RebootInstance reboots an instance (short version of HardRebootInstance)
func (c *Client) RebootInstance(id string) (*SimpleResponse, error) {
return c.HardRebootInstance(id)
}
// HardRebootInstance harshly reboots an instance (like shutting the power off and booting it again)
func (c *Client) HardRebootInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendPostRequest(fmt.Sprintf("/v2/instances/%s/hard_reboots", id), "")
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// SoftRebootInstance requests the VM to shut down nicely
func (c *Client) SoftRebootInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendPostRequest(fmt.Sprintf("/v2/instances/%s/soft_reboots", id), "")
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// StopInstance shuts the power down to the instance
func (c *Client) StopInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/stop", id), "")
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// StartInstance starts the instance booting from the shutdown state
func (c *Client) StartInstance(id string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/start", id), "")
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// GetInstanceConsoleURL gets the web URL for an instance's console
func (c *Client) GetInstanceConsoleURL(id string) (string, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/instances/%s/console", id))
if err != nil {
return "", decodeERROR(err)
}
console := InstanceConsole{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(&console)
return console.URL, err
}
// UpgradeInstance resizes the instance up to the new specification
// it's not possible to resize the instance to a smaller size
func (c *Client) UpgradeInstance(id, newSize string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/resize", id), map[string]string{
"size": newSize,
"region": c.Region,
})
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// MovePublicIPToInstance moves a public IP to the specified instance
func (c *Client) MovePublicIPToInstance(id, ipAddress string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/ip/%s", id, ipAddress), "")
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}
// SetInstanceFirewall changes the current firewall for an instance
func (c *Client) SetInstanceFirewall(id, firewallID string) (*SimpleResponse, error) {
resp, err := c.SendPutRequest(fmt.Sprintf("/v2/instances/%s/firewall", id), map[string]string{
"firewall_id": firewallID,
"region": c.Region,
})
if err != nil {
return nil, decodeERROR(err)
}
response, err := c.DecodeSimpleResponse(resp)
return response, err
}