forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bare_metal_server.go
540 lines (448 loc) · 18 KB
/
bare_metal_server.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const bmPath = "/v2/bare-metals"
// BareMetalServerService is the interface to interact with the Bare Metal endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/baremetal
type BareMetalServerService interface {
Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, *http.Response, error)
Get(ctx context.Context, serverID string) (*BareMetalServer, *http.Response, error)
Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, *http.Response, error)
Delete(ctx context.Context, serverID string) error
List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, *http.Response, error)
GetBandwidth(ctx context.Context, serverID string) (*Bandwidth, *http.Response, error)
GetUserData(ctx context.Context, serverID string) (*UserData, *http.Response, error)
GetVNCUrl(ctx context.Context, serverID string) (*VNCUrl, *http.Response, error)
ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, *http.Response, error)
ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, *http.Response, error)
Halt(ctx context.Context, serverID string) error
Reboot(ctx context.Context, serverID string) error
Start(ctx context.Context, serverID string) error
Reinstall(ctx context.Context, serverID string) (*BareMetalServer, *http.Response, error)
MassStart(ctx context.Context, serverList []string) error
MassHalt(ctx context.Context, serverList []string) error
MassReboot(ctx context.Context, serverList []string) error
GetUpgrades(ctx context.Context, serverID string) (*Upgrades, *http.Response, error)
ListVPCInfo(ctx context.Context, serverID string) ([]VPCInfo, *http.Response, error)
AttachVPC(ctx context.Context, serverID, vpcID string) error
DetachVPC(ctx context.Context, serverID, vpcID string) error
ListVPC2Info(ctx context.Context, serverID string) ([]VPC2Info, *http.Response, error)
AttachVPC2(ctx context.Context, serverID string, vpc2Req *AttachVPC2Req) error
DetachVPC2(ctx context.Context, serverID, vpcID string) error
}
// BareMetalServerServiceHandler handles interaction with the Bare Metal methods for the Vultr API
type BareMetalServerServiceHandler struct {
client *Client
}
// BareMetalServer represents a Bare Metal server on Vultr
type BareMetalServer struct {
ID string `json:"id"`
Os string `json:"os"`
RAM string `json:"ram"`
Disk string `json:"disk"`
MainIP string `json:"main_ip"`
CPUCount int `json:"cpu_count"`
Region string `json:"region"`
DefaultPassword string `json:"default_password"`
DateCreated string `json:"date_created"`
Status string `json:"status"`
NetmaskV4 string `json:"netmask_v4"`
GatewayV4 string `json:"gateway_v4"`
Plan string `json:"plan"`
V6Network string `json:"v6_network"`
V6MainIP string `json:"v6_main_ip"`
V6NetworkSize int `json:"v6_network_size"`
MacAddress int `json:"mac_address"`
Label string `json:"label"`
OsID int `json:"os_id"`
AppID int `json:"app_id"`
ImageID string `json:"image_id"`
Features []string `json:"features"`
Tags []string `json:"tags"`
UserScheme string `json:"user_scheme"`
}
// BareMetalCreate represents the optional parameters that can be set when creating a Bare Metal server
type BareMetalCreate struct {
Region string `json:"region"`
Plan string `json:"plan"`
OsID int `json:"os_id,omitempty"`
StartupScriptID string `json:"script_id,omitempty"`
SnapshotID string `json:"snapshot_id,omitempty"`
EnableIPv6 *bool `json:"enable_ipv6,omitempty"`
Label string `json:"label,omitempty"`
SSHKeyIDs []string `json:"sshkey_id,omitempty"`
AppID int `json:"app_id,omitempty"`
ImageID string `json:"image_id,omitempty"`
UserData string `json:"user_data,omitempty"`
ActivationEmail *bool `json:"activation_email,omitempty"`
Hostname string `json:"hostname,omitempty"`
MdiskMode string `json:"mdisk_mode,omitempty"`
ReservedIPv4 string `json:"reserved_ipv4,omitempty"`
PersistentPxe *bool `json:"persistent_pxe,omitempty"`
Tags []string `json:"tags,omitempty"`
UserScheme string `json:"user_scheme,omitempty"`
AttachVPC2 []string `json:"attach_vpc2,omitempty"`
DetachVPC2 []string `json:"detach_vpc2,omitempty"`
EnableVPC2 *bool `json:"enable_vpc2,omitempty"`
AppVariables map[string]string `json:"app_variables,omitempty"`
}
// BareMetalUpdate represents the optional parameters that can be set when updating a Bare Metal server
type BareMetalUpdate struct {
OsID int `json:"os_id,omitempty"`
EnableIPv6 *bool `json:"enable_ipv6,omitempty"`
Label string `json:"label,omitempty"`
AppID int `json:"app_id,omitempty"`
ImageID string `json:"image_id,omitempty"`
UserData string `json:"user_data,omitempty"`
MdiskMode string `json:"mdisk_mode,omitempty"`
Tags []string `json:"tags,omitempty"`
UserScheme string `json:"user_scheme,omitempty"`
AttachVPC2 []string `json:"attach_vpc2,omitempty"`
DetachVPC2 []string `json:"detach_vpc2,omitempty"`
EnableVPC2 *bool `json:"enable_vpc2,omitempty"`
}
// BareMetalServerBandwidth represents bandwidth information for a Bare Metal server
type BareMetalServerBandwidth struct {
IncomingBytes int `json:"incoming_bytes"`
OutgoingBytes int `json:"outgoing_bytes"`
}
type bareMetalsBase struct {
BareMetals []BareMetalServer `json:"bare_metals"`
Meta *Meta `json:"meta"`
}
type bareMetalBase struct {
BareMetal *BareMetalServer `json:"bare_metal"`
}
// BMBareMetalBase represents the base struct for a Bare Metal server
type BMBareMetalBase struct {
BareMetalBandwidth map[string]BareMetalServerBandwidth `json:"bandwidth"`
}
type vncBase struct {
VNCUrl *VNCUrl `json:"vnc"`
}
// VNCUrl contains the URL for a given Bare Metals VNC
type VNCUrl struct {
URL string `json:"url"`
}
// Create a new Bare Metal server.
func (b *BareMetalServerServiceHandler) Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, *http.Response, error) {
req, err := b.client.NewRequest(ctx, http.MethodPost, bmPath, bmCreate)
if err != nil {
return nil, nil, err
}
bm := new(bareMetalBase)
resp, err := b.client.DoWithContext(ctx, req, bm)
if err != nil {
return nil, resp, err
}
return bm.BareMetal, resp, nil
}
// Get information for a Bare Metal instance.
func (b *BareMetalServerServiceHandler) Get(ctx context.Context, serverID string) (*BareMetalServer, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
bms := new(bareMetalBase)
resp, err := b.client.DoWithContext(ctx, req, bms)
if err != nil {
return nil, resp, err
}
return bms.BareMetal, resp, nil
}
// Update a Bare Metal server
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPatch, uri, bmReq)
if err != nil {
return nil, nil, err
}
bms := new(bareMetalBase)
resp, err := b.client.DoWithContext(ctx, req, bms)
if err != nil {
return nil, resp, err
}
return bms.BareMetal, resp, nil
}
// Delete a Bare Metal server.
func (b *BareMetalServerServiceHandler) Delete(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// List all Bare Metal instances in your account.
func (b *BareMetalServerServiceHandler) List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, *http.Response, error) { //nolint:dupl,lll
req, err := b.client.NewRequest(ctx, http.MethodGet, bmPath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
bms := new(bareMetalsBase)
resp, err := b.client.DoWithContext(ctx, req, bms)
if err != nil {
return nil, nil, resp, err
}
return bms.BareMetals, bms.Meta, resp, nil
}
// GetBandwidth used by a Bare Metal server.
func (b *BareMetalServerServiceHandler) GetBandwidth(ctx context.Context, serverID string) (*Bandwidth, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/bandwidth", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
bms := new(Bandwidth)
resp, err := b.client.DoWithContext(ctx, req, &bms)
if err != nil {
return nil, resp, err
}
return bms, resp, nil
}
// GetUserData for a Bare Metal server. The userdata returned will be in base64 encoding.
func (b *BareMetalServerServiceHandler) GetUserData(ctx context.Context, serverID string) (*UserData, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/user-data", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
userData := new(userDataBase)
resp, err := b.client.DoWithContext(ctx, req, userData)
if err != nil {
return nil, nil, err
}
return userData.UserData, resp, nil
}
// GetVNCUrl gets the vnc url for a given Bare Metal server.
func (b *BareMetalServerServiceHandler) GetVNCUrl(ctx context.Context, serverID string) (*VNCUrl, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/vnc", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vnc := new(vncBase)
resp, err := b.client.DoWithContext(ctx, req, vnc)
if err != nil {
return nil, resp, err
}
return vnc.VNCUrl, resp, nil
}
// ListIPv4s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, *http.Response, error) { //nolint:lll,dupl
uri := fmt.Sprintf("%s/%s/ipv4", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
ipv4 := new(ipBase)
resp, err := b.client.DoWithContext(ctx, req, ipv4)
if err != nil {
return nil, nil, resp, err
}
return ipv4.IPv4s, ipv4.Meta, resp, nil
}
// ListIPv6s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
// If the Bare Metal server does not have IPv6 enabled, then an empty array is returned.
func (b *BareMetalServerServiceHandler) ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, *http.Response, error) { //nolint:lll,dupl
uri := fmt.Sprintf("%s/%s/ipv6", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
ipv6 := new(ipBase)
resp, err := b.client.DoWithContext(ctx, req, ipv6)
if err != nil {
return nil, nil, resp, err
}
return ipv6.IPv6s, ipv6.Meta, resp, nil
}
// Halt a Bare Metal server.
// This is a hard power off, meaning that the power to the machine is severed.
// The data on the machine will not be modified, and you will still be billed for the machine.
func (b *BareMetalServerServiceHandler) Halt(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/halt", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// Reboot a Bare Metal server. This is a hard reboot, which means that the server is powered off, then back on.
func (b *BareMetalServerServiceHandler) Reboot(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/reboot", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// Start a Bare Metal server.
func (b *BareMetalServerServiceHandler) Start(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/start", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// Reinstall the operating system on a Bare Metal server.
// All data will be permanently lost, but the IP address will remain the same.
func (b *BareMetalServerServiceHandler) Reinstall(ctx context.Context, serverID string) (*BareMetalServer, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/reinstall", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, nil, err
}
bms := new(bareMetalBase)
resp, err := b.client.DoWithContext(ctx, req, bms)
if err != nil {
return nil, resp, err
}
return bms.BareMetal, resp, nil
}
// MassStart will start a list of Bare Metal servers the machine is already running, it will be restarted.
func (b *BareMetalServerServiceHandler) MassStart(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/start", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// MassHalt a list of Bare Metal servers.
func (b *BareMetalServerServiceHandler) MassHalt(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/halt", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// MassReboot a list of Bare Metal servers.
func (b *BareMetalServerServiceHandler) MassReboot(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/reboot", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// GetUpgrades that are available for a Bare Metal server.
func (b *BareMetalServerServiceHandler) GetUpgrades(ctx context.Context, serverID string) (*Upgrades, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/upgrades", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
upgrades := new(upgradeBase)
resp, err := b.client.DoWithContext(ctx, req, upgrades)
if err != nil {
return nil, resp, err
}
return upgrades.Upgrades, resp, nil
}
// ListVPCInfo will list all currently attached VPC IP information for the
// given bare metal server.
func (b *BareMetalServerServiceHandler) ListVPCInfo(ctx context.Context, serverID string) ([]VPCInfo, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/vpcs", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vpcs := new(vpcInfoBase)
resp, err := b.client.DoWithContext(ctx, req, vpcs)
if err != nil {
return nil, resp, err
}
return vpcs.VPCs, resp, nil
}
// AttachVPC serves to attach a VPC to a bare metal server.
func (b *BareMetalServerServiceHandler) AttachVPC(ctx context.Context, serverID, vpcID string) error {
uri := fmt.Sprintf("%s/%s/vpcs/attach", bmPath, serverID)
body := RequestBody{"vpc_id": vpcID}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, body)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// DetachVPC will detach a VPC from a bare metal server.
func (b *BareMetalServerServiceHandler) DetachVPC(ctx context.Context, serverID, vpcID string) error {
uri := fmt.Sprintf("%s/%s/vpc2/detach", bmPath, serverID)
body := RequestBody{"vpc_id": vpcID}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, body)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// ListVPC2Info currently attached to a Bare Metal server.
func (b *BareMetalServerServiceHandler) ListVPC2Info(ctx context.Context, serverID string) ([]VPC2Info, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/vpc2", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
vpcs := new(vpc2InfoBase)
resp, err := b.client.DoWithContext(ctx, req, vpcs)
if err != nil {
return nil, resp, err
}
return vpcs.VPCs, resp, nil
}
// AttachVPC2 to a Bare Metal server.
func (b *BareMetalServerServiceHandler) AttachVPC2(ctx context.Context, serverID string, vpc2Req *AttachVPC2Req) error {
uri := fmt.Sprintf("%s/%s/vpc2/attach", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, vpc2Req)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}
// DetachVPC2 from a Bare Metal server.
func (b *BareMetalServerServiceHandler) DetachVPC2(ctx context.Context, serverID, vpcID string) error {
uri := fmt.Sprintf("%s/%s/vpc2/detach", bmPath, serverID)
body := RequestBody{"vpc_id": vpcID}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, body)
if err != nil {
return err
}
_, err = b.client.DoWithContext(ctx, req, nil)
return err
}