forked from equinix/ne-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_device.go
501 lines (465 loc) · 17.9 KB
/
rest_device.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
package ne
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/equinix/ne-go/internal/api"
"github.com/equinix/rest-go"
)
const (
//DeviceManagementTypeSelf indicates device management mode where customer
//fully manages the device
DeviceManagementTypeSelf = "SELF-CONFIGURED"
//DeviceManagementTypeEquinix indicates device management mode where device
//connectivity and services are managed by Equinix
DeviceManagementTypeEquinix = "EQUINIX-CONFIGURED"
//DeviceLicenseModeSubscription indicates device software license mode where
//Equinix provides software license in a form of subscription
DeviceLicenseModeSubscription = "Sub"
//DeviceLicenseModeBYOL indicates device software license mode where
//customer provides his own, externally procured device license
DeviceLicenseModeBYOL = "BYOL"
)
type restDeviceUpdateRequest struct {
uuid string
deviceFields map[string]interface{}
additionalBandwidth *int
aclTemplateID *string
mgmtAclTemplateUuid *string
c RestClient
}
// CreateDevice creates given Network Edge device and returns its UUID upon successful creation
func (c RestClient) CreateDevice(device Device) (*string, error) {
path := "/ne/v1/devices"
reqBody := createDeviceRequest(device)
respBody := api.DeviceRequestResponse{}
req := c.R().SetBody(&reqBody).SetResult(&respBody)
if err := c.Execute(req, http.MethodPost, path); err != nil {
return nil, err
}
return respBody.UUID, nil
}
// CreateRedundantDevice creates HA device setup from given primary and secondary devices and
// returns their UUIDS upon successful creation
func (c RestClient) CreateRedundantDevice(primary Device, secondary Device) (*string, *string, error) {
path := "/ne/v1/devices"
reqBody := createRedundantDeviceRequest(primary, secondary)
respBody := api.DeviceRequestResponse{}
req := c.R().SetBody(&reqBody).SetResult(&respBody)
if err := c.Execute(req, http.MethodPost, path); err != nil {
return nil, nil, err
}
return respBody.UUID, respBody.SecondaryUUID, nil
}
// GetDevice fetches details of a device with a given UUID
func (c RestClient) GetDevice(uuid string) (*Device, error) {
path := "/ne/v1/devices/" + url.PathEscape(uuid)
result := api.Device{}
request := c.R().SetResult(&result)
if err := c.Execute(request, http.MethodGet, path); err != nil {
return nil, err
}
return mapDeviceAPIToDomain(result), nil
}
// GetDevices retrieves list of devices (along with their details) with given list of statuses
func (c RestClient) GetDevices(statuses []string) ([]Device, error) {
path := "/ne/v1/devices"
content, err := c.GetOffsetPaginated(path, &api.DevicesResponse{},
rest.DefaultOffsetPagingConfig().
SetAdditionalParams(map[string]string{"status": buildQueryParamValueString(statuses)}))
if err != nil {
return nil, err
}
transformed := make([]Device, len(content))
for i := range content {
transformed[i] = *mapDeviceAPIToDomain(content[i].(api.Device))
}
return transformed, nil
}
// GetDeviceAdditionalBandwidthDetails retrives details of given device's additional bandwidth
func (c RestClient) GetDeviceAdditionalBandwidthDetails(uuid string) (*DeviceAdditionalBandwidthDetails, error) {
path := fmt.Sprintf("/ne/v1/devices/%s/additionalBandwidths", url.PathEscape(uuid))
result := api.DeviceAdditionalBandwidthResponse{}
request := c.R().SetResult(&result)
if err := c.Execute(request, http.MethodGet, path); err != nil {
return nil, err
}
return mapDeviceAdditionalBandwidthAPIToDomain(result), nil
}
// GetDeviceACLDetails retrives device acl template provisioning status
func (c RestClient) GetDeviceACLDetails(uuid string) (*DeviceACLDetails, error) {
path := fmt.Sprintf("/ne/v1/devices/%s/acl", url.PathEscape(uuid))
result := api.DeviceACLResponse{}
request := c.R().SetResult(&result)
if err := c.Execute(request, http.MethodGet, path); err != nil {
return nil, err
}
return mapDeviceACLAPIToDomain(result), nil
}
// NewDeviceUpdateRequest creates new composite update request for a device with a given UUID
func (c RestClient) NewDeviceUpdateRequest(uuid string) DeviceUpdateRequest {
return &restDeviceUpdateRequest{
uuid: uuid,
deviceFields: make(map[string]interface{}),
c: c}
}
// DeleteDevice deletes device with a given UUID
func (c RestClient) DeleteDevice(uuid string) error {
path := "/ne/v1/devices/" + url.PathEscape(uuid)
req := c.R().SetQueryParam("deleteRedundantDevice", "true")
if err := c.Execute(req, http.MethodDelete, path); err != nil {
return err
}
return nil
}
// WithDeviceName sets new device name in a composite device update request
func (req *restDeviceUpdateRequest) WithDeviceName(deviceName string) DeviceUpdateRequest {
req.deviceFields["deviceName"] = deviceName
return req
}
// WithTermLength sets new term length in a composite device update request
func (req *restDeviceUpdateRequest) WithTermLength(termLength int) DeviceUpdateRequest {
req.deviceFields["termLength"] = termLength
return req
}
// WithNotifications sets new notifications in a composite device update request
func (req *restDeviceUpdateRequest) WithNotifications(notifications []string) DeviceUpdateRequest {
req.deviceFields["notifications"] = notifications
return req
}
// WithCore sets new core count in a composite device update request
func (req *restDeviceUpdateRequest) WithCore(core int) DeviceUpdateRequest {
req.deviceFields["core"] = core
return req
}
// WithAdditionalBandwidth sets new additional bandwidth in a composite device update request
func (req *restDeviceUpdateRequest) WithAdditionalBandwidth(additionalBandwidth int) DeviceUpdateRequest {
req.additionalBandwidth = &additionalBandwidth
return req
}
// WithACLTemplate sets new ACL template identifier in a composite device update request
func (req *restDeviceUpdateRequest) WithACLTemplate(templateID string) DeviceUpdateRequest {
req.aclTemplateID = &templateID
return req
}
// WithMgmtAclTemplate sets new MGMT ACL template identifier in a composite device update request
func (req *restDeviceUpdateRequest) WithMgmtAclTemplate(mgmtAclTemplateUuid string) DeviceUpdateRequest {
req.mgmtAclTemplateUuid = &mgmtAclTemplateUuid
return req
}
// Execute attempts to update device according new data set in composite update request.
// This is not atomic operation and if any update will fail, other changes won't be reverted.
// UpdateError will be returned if any of requested data failed to update
func (req *restDeviceUpdateRequest) Execute() error {
updateErr := UpdateError{}
if err := req.c.replaceDeviceFields(req.uuid, req.deviceFields); err != nil {
updateErr.AddChangeError(changeTypeUpdate, "deviceFields", req.deviceFields, err)
}
if req.aclTemplateID != nil || req.mgmtAclTemplateUuid != nil {
if err := req.c.replaceDeviceACLTemplate(req.uuid, req.aclTemplateID, req.mgmtAclTemplateUuid); err != nil {
if req.aclTemplateID != nil {
updateErr.AddChangeError(changeTypeUpdate, "aclTemplateUuid", *req.aclTemplateID, err)
}
if req.mgmtAclTemplateUuid != nil {
updateErr.AddChangeError(changeTypeUpdate, "mgmtAclTemplateUuid", *req.mgmtAclTemplateUuid, err)
}
}
}
if req.additionalBandwidth != nil {
if err := req.c.replaceDeviceAdditionalBandwidth(req.uuid, *req.additionalBandwidth); err != nil {
updateErr.AddChangeError(changeTypeUpdate, "additionalBandwidth", req.additionalBandwidth, err)
}
}
if updateErr.ChangeErrorsCount() > 0 {
return updateErr
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported package methods
//_______________________________________________________________________
func mapDeviceAPIToDomain(apiDevice api.Device) *Device {
device := Device{}
device.UUID = apiDevice.UUID
device.Name = apiDevice.Name
device.TypeCode = apiDevice.DeviceTypeCode
device.Status = apiDevice.Status
device.LicenseStatus = apiDevice.LicenseStatus
device.MetroCode = apiDevice.MetroCode
device.IBX = apiDevice.IBX
device.Region = apiDevice.Region
device.Throughput = apiDevice.Throughput
device.ThroughputUnit = apiDevice.ThroughputUnit
device.HostName = apiDevice.HostName
device.PackageCode = apiDevice.PackageCode
device.Version = apiDevice.Version
if apiDevice.LicenseType != nil {
if *apiDevice.LicenseType == DeviceLicenseModeBYOL {
device.IsBYOL = Bool(true)
} else {
device.IsBYOL = Bool(false)
}
}
device.LicenseToken = apiDevice.LicenseToken
device.LicenseFileID = apiDevice.LicenseFileID
device.ACLTemplateUUID = apiDevice.ACLTemplateUUID
device.MgmtAclTemplateUuid = apiDevice.MgmtAclTemplateUUID
device.SSHIPAddress = apiDevice.SSHIPAddress
device.SSHIPFqdn = apiDevice.SSHIPFqdn
device.AccountNumber = apiDevice.AccountNumber
device.Notifications = apiDevice.Notifications
device.PurchaseOrderNumber = apiDevice.PurchaseOrderNumber
device.RedundancyType = apiDevice.RedundancyType
device.RedundantUUID = apiDevice.RedundantUUID
device.TermLength = apiDevice.TermLength
device.AdditionalBandwidth = apiDevice.AdditionalBandwidth
device.WanInterfaceId = apiDevice.SshInterfaceID
device.OrderReference = apiDevice.OrderReference
device.InterfaceCount = apiDevice.InterfaceCount
if apiDevice.Core != nil {
device.CoreCount = apiDevice.Core.Core
}
if apiDevice.DeviceManagementType != nil {
if *apiDevice.DeviceManagementType == DeviceManagementTypeSelf {
device.IsSelfManaged = Bool(true)
} else {
device.IsSelfManaged = Bool(false)
}
}
device.Connectivity = apiDevice.Connectivity
device.Interfaces = mapDeviceInterfacesAPIToDomain(apiDevice.Interfaces)
device.VendorConfiguration = apiDevice.VendorConfig
device.UserPublicKey = mapDeviceUserPublicKeyAPIToDomain(apiDevice.UserPublicKey)
device.ASN = apiDevice.ASN
device.ZoneCode = apiDevice.ZoneCode
device.ClusterDetails = mapDeviceClusterDetailsAPIToDomain(apiDevice.ClusterDetails)
return &device
}
func mapDeviceInterfacesAPIToDomain(apiInterfaces []api.DeviceInterface) []DeviceInterface {
transformed := make([]DeviceInterface, len(apiInterfaces))
for i := range apiInterfaces {
transformed[i] = DeviceInterface{
ID: apiInterfaces[i].ID,
Name: apiInterfaces[i].Name,
Status: apiInterfaces[i].Status,
OperationalStatus: apiInterfaces[i].OperationalStatus,
MACAddress: apiInterfaces[i].MACAddress,
IPAddress: apiInterfaces[i].IPAddress,
AssignedType: apiInterfaces[i].AssignedType,
Type: apiInterfaces[i].Type,
}
}
return transformed
}
func mapDeviceUserPublicKeyAPIToDomain(apiUserKey *api.DeviceUserPublicKey) *DeviceUserPublicKey {
if apiUserKey == nil {
return nil
}
return &DeviceUserPublicKey{
Username: apiUserKey.Username,
KeyName: apiUserKey.KeyName,
}
}
func mapDeviceUserPublicKeyDomainToAPI(userKey *DeviceUserPublicKey) *api.DeviceUserPublicKeyRequest {
if userKey == nil {
return nil
}
return &api.DeviceUserPublicKeyRequest{
Username: userKey.Username,
KeyName: userKey.KeyName,
}
}
func mapDeviceClusterDetailsAPIToDomain(apiClusterDetails *api.ClusterDetails) *ClusterDetails {
if apiClusterDetails == nil {
return nil
}
clusterDetails := ClusterDetails{}
clusterDetails.ClusterId = apiClusterDetails.ClusterID
clusterDetails.ClusterName = apiClusterDetails.ClusterName
clusterDetails.NumOfNodes = apiClusterDetails.NumOfNodes
apiNodes := apiClusterDetails.Nodes
transformed := make([]ClusterNode, len(apiNodes))
clusterNodeDetails := make([]*ClusterNodeDetail, len(apiNodes))
for i := range apiNodes {
transformed[i] = ClusterNode{
UUID: apiNodes[i].UUID,
Name: apiNodes[i].Name,
Node: apiNodes[i].Node,
AdminPassword: apiNodes[i].AdminPassword,
VendorConfiguration: apiNodes[i].VendorConfiguration,
}
clusterNodeDetails[i] = &ClusterNodeDetail{
UUID: apiNodes[i].UUID,
Name: apiNodes[i].Name,
VendorConfiguration: apiNodes[i].VendorConfiguration,
}
}
clusterDetails.Nodes = transformed
clusterDetails.Node0 = clusterNodeDetails[0]
clusterDetails.Node1 = clusterNodeDetails[1]
return &clusterDetails
}
func mapDeviceClusterDetailsDomainToAPI(clusterDetails *ClusterDetails) *api.ClusterDetailsRequest {
if clusterDetails == nil {
return nil
}
req := api.ClusterDetailsRequest{}
req.ClusterName = clusterDetails.ClusterName
clusterNodeDetailsRequest := make(map[string]api.ClusterNodeDetailRequest)
clusterNodeDetailsRequest["node0"] = *mapDeviceClusterNodeDetailDomainToAPI(clusterDetails.Node0)
clusterNodeDetailsRequest["node1"] = *mapDeviceClusterNodeDetailDomainToAPI(clusterDetails.Node1)
req.ClusterNodeDetails = clusterNodeDetailsRequest
return &req
}
func mapDeviceClusterNodeDetailDomainToAPI(clusterNodeDetail *ClusterNodeDetail) *api.ClusterNodeDetailRequest {
if clusterNodeDetail == nil {
return nil
}
return &api.ClusterNodeDetailRequest{
VendorConfiguration: clusterNodeDetail.VendorConfiguration,
LicenseFileID: clusterNodeDetail.LicenseFileId,
LicenseToken: clusterNodeDetail.LicenseToken,
}
}
func createDeviceRequest(device Device) api.DeviceRequest {
req := api.DeviceRequest{}
req.Throughput = device.Throughput
req.ThroughputUnit = device.ThroughputUnit
req.MetroCode = device.MetroCode
req.DeviceTypeCode = device.TypeCode
if device.TermLength != nil {
termLengthString := strconv.Itoa(*device.TermLength)
req.TermLength = &termLengthString
}
if device.IsBYOL != nil {
if *device.IsBYOL {
req.LicenseMode = String(DeviceLicenseModeBYOL)
} else {
req.LicenseMode = String(DeviceLicenseModeSubscription)
}
}
req.LicenseToken = device.LicenseToken
req.LicenseFileID = device.LicenseFileID
req.CloudInitFileID = device.CloudInitFileID
req.PackageCode = device.PackageCode
req.VirtualDeviceName = device.Name
req.Notifications = device.Notifications
req.HostNamePrefix = device.HostName
req.OrderReference = device.OrderReference
req.PurchaseOrderNumber = device.PurchaseOrderNumber
req.AccountNumber = device.AccountNumber
req.Version = device.Version
req.InterfaceCount = device.InterfaceCount
if device.IsSelfManaged != nil {
if *device.IsSelfManaged {
req.DeviceManagementType = String(DeviceManagementTypeSelf)
} else {
req.DeviceManagementType = String(DeviceManagementTypeEquinix)
}
}
req.Connectivity = device.Connectivity
req.Core = device.CoreCount
req.AdditionalBandwidth = device.AdditionalBandwidth
req.SshInterfaceId = device.WanInterfaceId
req.ACLTemplateUUID = device.ACLTemplateUUID
req.MgmtAclTemplateUUID = device.MgmtAclTemplateUuid
req.VendorConfig = device.VendorConfiguration
req.UserPublicKey = mapDeviceUserPublicKeyDomainToAPI(device.UserPublicKey)
req.ClusterDetails = mapDeviceClusterDetailsDomainToAPI(device.ClusterDetails)
return req
}
func createRedundantDeviceRequest(primary Device, secondary Device) api.DeviceRequest {
req := createDeviceRequest(primary)
secReq := api.SecondaryDeviceRequest{}
secReq.MetroCode = secondary.MetroCode
secReq.LicenseToken = secondary.LicenseToken
secReq.LicenseFileID = secondary.LicenseFileID
secReq.CloudInitFileID = secondary.CloudInitFileID
secReq.VirtualDeviceName = secondary.Name
secReq.Notifications = secondary.Notifications
secReq.HostNamePrefix = secondary.HostName
secReq.AccountNumber = secondary.AccountNumber
secReq.AdditionalBandwidth = secondary.AdditionalBandwidth
secReq.SshInterfaceID = secondary.WanInterfaceId
if secReq.SshInterfaceID == nil {
secReq.SshInterfaceID = req.SshInterfaceId
}
secReq.ACLTemplateUUID = secondary.ACLTemplateUUID
secReq.MgmtAclTemplateUUID = secondary.MgmtAclTemplateUuid
secReq.VendorConfig = secondary.VendorConfiguration
secReq.UserPublicKey = mapDeviceUserPublicKeyDomainToAPI(secondary.UserPublicKey)
req.Secondary = &secReq
return req
}
func (c RestClient) replaceDeviceACLTemplate(uuid string, wanAclTemplateUuid *string, mgmtAclTemplateUuid *string) error {
path := "/ne/v1/devices/" + url.PathEscape(uuid) + "/acl"
reqBody := api.DeviceACLTemplateRequest{
TemplateUUID: wanAclTemplateUuid,
MgmtAclTemplateUUID: mgmtAclTemplateUuid,
}
req := c.R().SetBody(reqBody)
if err := c.Execute(req, http.MethodPatch, path); err != nil {
return err
}
return nil
}
func (c RestClient) replaceDeviceAdditionalBandwidth(uuid string, bandwidth int) error {
path := fmt.Sprintf("/ne/v1/devices/%s/additionalBandwidths", url.PathEscape(uuid))
reqBody := api.DeviceAdditionalBandwidthUpdateRequest{AdditionalBandwidth: &bandwidth}
req := c.R().SetBody(reqBody)
if err := c.Execute(req, http.MethodPut, path); err != nil {
return err
}
return nil
}
func (c RestClient) replaceDeviceFields(uuid string, fields map[string]interface{}) error {
reqBody := api.DeviceUpdateRequest{}
okToSend := false
if v, ok := fields["deviceName"]; ok {
reqBody.VirtualDeviceName = String(v.(string))
okToSend = true
}
if v, ok := fields["termLength"]; ok {
reqBody.TermLength = Int(v.(int))
okToSend = true
}
if v, ok := fields["notifications"]; ok {
reqBody.Notifications = v.([]string)
okToSend = true
}
if v, ok := fields["core"]; ok {
reqBody.Core = Int(v.(int))
okToSend = true
}
if okToSend {
path := "/ne/v1/devices/" + uuid
req := c.R().SetBody(&reqBody)
if err := c.Execute(req, http.MethodPatch, path); err != nil {
return err
}
}
return nil
}
func buildQueryParamValueString(values []string) string {
var sb strings.Builder
for i := range values {
sb.WriteString(url.QueryEscape(values[i]))
if i < len(values)-1 {
sb.WriteString(",")
}
}
return sb.String()
}
func mapDeviceAdditionalBandwidthAPIToDomain(apiDetails api.DeviceAdditionalBandwidthResponse) *DeviceAdditionalBandwidthDetails {
return &DeviceAdditionalBandwidthDetails{
AdditionalBandwidth: apiDetails.AdditionalBandwidth,
Status: apiDetails.Status,
}
}
func mapDeviceACLAPIToDomain(apiDetails api.DeviceACLResponse) *DeviceACLDetails {
return &DeviceACLDetails{
Status: apiDetails.Status,
}
}