-
Notifications
You must be signed in to change notification settings - Fork 8
/
vxc.go
468 lines (400 loc) · 13.8 KB
/
vxc.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
package megaport
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"slices"
"strings"
"time"
)
// VXCService is an interface for interfacing with the VXC endpoints in the Megaport VXC API.
type VXCService interface {
// BuyVXC buys a VXC from the Megaport VXC API.
BuyVXC(ctx context.Context, req *BuyVXCRequest) (*BuyVXCResponse, error)
// ValidateVXCOrder validates a VXC order in the Megaport Products API.
ValidateVXCOrder(ctx context.Context, req *BuyVXCRequest) error
// GetVXC gets details about a single VXC from the Megaport VXC API.
GetVXC(ctx context.Context, id string) (*VXC, error)
// DeleteVXC deletes a VXC in the Megaport VXC API.
DeleteVXC(ctx context.Context, id string, req *DeleteVXCRequest) error
// UpdateVXC updates a VXC in the Megaport VXC API.
UpdateVXC(ctx context.Context, id string, req *UpdateVXCRequest) (*VXC, error)
// LookupPartnerPorts looks up available partner ports in the Megaport VXC API.
LookupPartnerPorts(ctx context.Context, req *LookupPartnerPortsRequest) (*LookupPartnerPortsResponse, error)
// ListPartnerPorts lists available partner ports in the Megaport VXC API.
ListPartnerPorts(ctx context.Context, req *ListPartnerPortsRequest) (*ListPartnerPortsResponse, error)
// ListVXCResourceTags lists the resource tags for a VXC in the Megaport Products API.
ListVXCResourceTags(ctx context.Context, vxcID string) (map[string]string, error)
// UpdateVXCResourceTags updates the resource tags for a VXC in the Megaport Products API.
UpdateVXCResourceTags(ctx context.Context, vxcID string, tags map[string]string) error
}
// NewVXCService creates a new instance of the VXC Service.
func NewVXCService(c *Client) *VXCServiceOp {
return &VXCServiceOp{
Client: c,
}
}
var _ VXCService = &VXCServiceOp{}
// VXCServiceOp handles communication with the VXC related methods of the Megaport API.
type VXCServiceOp struct {
Client *Client
}
// BuyVXCRequest represents a request to buy a VXC from the Megaport VXC API.
type BuyVXCRequest struct {
PortUID string
VXCName string
RateLimit int
Term int
Shutdown bool
PromoCode string
ServiceKey string
CostCentre string
AEndConfiguration VXCOrderEndpointConfiguration
BEndConfiguration VXCOrderEndpointConfiguration
WaitForProvision bool // Wait until the VXC provisions before returning
WaitForTime time.Duration // How long to wait for the VXC to provision if WaitForProvision is true (default is 5 minutes)
ResourceTags map[string]string `json:"resourceTags,omitempty"`
}
// BuyVXCResponse represents a response from buying a VXC from the Megaport VXC API.
type BuyVXCResponse struct {
TechnicalServiceUID string
}
// DeleteVXCRequest represents a request to delete a VXC in the Megaport VXC API.
type DeleteVXCRequest struct {
DeleteNow bool
}
// DeleteVXCResponse represents a response from deleting a VXC in the Megaport VXC API.
type DeleteVXCResponse struct {
IsDeleting bool
}
// UpdateVXCRequest represents a request to update a VXC in the Megaport VXC API.
type UpdateVXCRequest struct {
AEndVLAN *int
BEndVLAN *int
AEndProductUID *string
BEndProductUID *string
RateLimit *int
Name *string
CostCentre *string
Term *int
Shutdown *bool
AEndInnerVLAN *int
BEndInnerVLAN *int
AEndPartnerConfig VXCPartnerConfiguration
BEndPartnerConfig VXCPartnerConfiguration
WaitForUpdate bool // Wait until the VXC updates before returning
WaitForTime time.Duration // How long to wait for the VXC to update if WaitForUpdate is true (default is 5 minutes)
}
// UpdateVXCResponse represents a response from updating a VXC in the Megaport VXC API.
type UpdateVXCResponse struct {
}
// LookupPartnerPortsRequest represents a request to lookup available partner ports in the Megaport VXC API.
type LookupPartnerPortsRequest struct {
Key string
PortSpeed int
Partner string
ProductID string
}
// LookupPartnerPortsResponse represents a response from looking up available partner ports in the Megaport VXC API.
type LookupPartnerPortsResponse struct {
ProductUID string
}
// ListPartnerPortsRequest represents a request to list available partner ports in the Megaport VXC API.
type ListPartnerPortsRequest struct {
Key string
Partner string
}
type ListPartnerPortsResponse struct {
Data PartnerLookup
}
// BuyVXC buys a VXC from the Megaport VXC API.
func (svc *VXCServiceOp) BuyVXC(ctx context.Context, req *BuyVXCRequest) (*BuyVXCResponse, error) {
if req.Term != 1 && req.Term != 12 && req.Term != 24 && req.Term != 36 {
return nil, ErrInvalidTerm
}
buyOrder := createVXCOrder(req)
responseBody, responseError := svc.Client.ProductService.ExecuteOrder(ctx, buyOrder)
if responseError != nil {
return nil, responseError
}
orderInfo := VXCOrderResponse{}
if err := json.Unmarshal(*responseBody, &orderInfo); err != nil {
return nil, err
}
serviceUID := orderInfo.Data[0].TechnicalServiceUID
// wait until the VXC is provisioned before returning if reqested by the user
if req.WaitForProvision {
toWait := req.WaitForTime
if toWait == 0 {
toWait = 5 * time.Minute
}
ticker := time.NewTicker(30 * time.Second) // check on the provision status every 30 seconds
timer := time.NewTimer(toWait)
defer ticker.Stop()
defer timer.Stop()
for {
select {
case <-timer.C:
return nil, fmt.Errorf("time expired waiting for VXC %s to provision", serviceUID)
case <-ctx.Done():
return nil, fmt.Errorf("context expired waiting for VXC %s to provision", serviceUID)
case <-ticker.C:
vxcDetails, err := svc.GetVXC(ctx, serviceUID)
if err != nil {
return nil, err
}
if slices.Contains(SERVICE_STATE_READY, vxcDetails.ProvisioningStatus) {
return &BuyVXCResponse{
TechnicalServiceUID: serviceUID,
}, nil
}
}
}
} else {
// return the service UID right away if the user doesn't want to wait for provision
return &BuyVXCResponse{
TechnicalServiceUID: serviceUID,
}, nil
}
}
// GetVXC gets details about a single VXC from the Megaport VXC API.
func (svc *VXCServiceOp) GetVXC(ctx context.Context, id string) (*VXC, error) {
path := "/v2/product/" + id
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
return nil, err
}
return &vxcDetails.Data, nil
}
// ListVXCResourceTags lists the resource tags for a VXC in the Megaport Products API.
func (svc *VXCServiceOp) ListVXCResourceTags(ctx context.Context, vxcID string) (map[string]string, error) {
tags, err := svc.Client.ProductService.ListProductResourceTags(ctx, vxcID)
if err != nil {
return nil, err
}
return fromProductResourceTags(tags), nil
}
// UpdateVXCResourceTags updates the resource tags for a VXC in the Megaport Products API.
func (svc *VXCServiceOp) UpdateVXCResourceTags(ctx context.Context, vxcID string, tags map[string]string) error {
return svc.Client.ProductService.UpdateProductResourceTags(ctx, vxcID, &UpdateProductResourceTagsRequest{
ResourceTags: toProductResourceTags(tags),
})
}
func createVXCOrder(req *BuyVXCRequest) []VXCOrder {
return []VXCOrder{{
PortID: req.PortUID,
AssociatedVXCs: []VXCOrderConfiguration{
{
Name: req.VXCName,
RateLimit: req.RateLimit,
Term: req.Term,
Shutdown: req.Shutdown,
PromoCode: req.PromoCode,
ServiceKey: req.ServiceKey,
CostCentre: req.CostCentre,
AEnd: req.AEndConfiguration,
BEnd: req.BEndConfiguration,
ResourceTags: toProductResourceTags(req.ResourceTags),
},
},
}}
}
// ValidateVXCOrder validates a VXC order in the Megaport VXC API.
func (svc *VXCServiceOp) ValidateVXCOrder(ctx context.Context, req *BuyVXCRequest) error {
buyOrder := createVXCOrder(req)
return svc.Client.ProductService.ValidateProductOrder(ctx, buyOrder)
}
// DeleteVXC deletes a VXC in the Megaport VXC API.
func (svc *VXCServiceOp) DeleteVXC(ctx context.Context, id string, req *DeleteVXCRequest) error {
_, err := svc.Client.ProductService.DeleteProduct(ctx, &DeleteProductRequest{
ProductID: id,
DeleteNow: req.DeleteNow,
})
if err != nil {
return err
}
return nil
}
// UpdateVXC updates a VXC in the Megaport VXC API.
func (svc *VXCServiceOp) UpdateVXC(ctx context.Context, id string, req *UpdateVXCRequest) (*VXC, error) {
if req.Term != nil && (*req.Term != 1 && *req.Term != 12 && *req.Term != 24 && *req.Term != 36) {
return nil, ErrInvalidTerm
}
if req.CostCentre != nil && len(*req.CostCentre) > 255 {
return nil, ErrCostCentreTooLong
}
path := fmt.Sprintf("/v3/product/%s/%s", PRODUCT_VXC, id)
url := svc.Client.BaseURL.JoinPath(path).String()
update := &VXCUpdate{
RateLimit: req.RateLimit,
AEndVLAN: req.AEndVLAN,
BEndVLAN: req.BEndVLAN,
Term: req.Term,
Shutdown: req.Shutdown,
}
if req.AEndPartnerConfig != nil {
// Only allow AENdPartnerConfig or VROUTER Partner Config for AEndPartnerConfig in VXC Updates
switch req.AEndPartnerConfig.(type) {
case VXCPartnerConfiguration, *VXCOrderVrouterPartnerConfig, VXCOrderVrouterPartnerConfig:
update.AEndPartnerConfig = req.AEndPartnerConfig
default:
return nil, ErrInvalidVXCAEndPartnerConfig
}
}
if req.BEndPartnerConfig != nil {
// Only allow Vrouter Partner Config for BEndPartnerConfig in VXC Updates
switch req.BEndPartnerConfig.(type) {
case *VXCOrderVrouterPartnerConfig, VXCOrderVrouterPartnerConfig:
update.BEndPartnerConfig = req.BEndPartnerConfig
default:
return nil, ErrInvalidVXCBEndPartnerConfig
}
}
if req.Name != nil {
update.Name = *req.Name
}
if req.AEndProductUID != nil {
update.AEndProductUID = *req.AEndProductUID
}
if req.BEndProductUID != nil {
update.BEndProductUID = *req.BEndProductUID
}
if req.CostCentre != nil {
update.CostCentre = *req.CostCentre
}
if req.AEndInnerVLAN != nil {
update.AEndInnerVLAN = req.AEndInnerVLAN
}
if req.BEndInnerVLAN != nil {
update.BEndInnerVLAN = req.BEndInnerVLAN
}
clientReq, err := svc.Client.NewRequest(ctx, http.MethodPut, url, update)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
return nil, err
}
// wait until the VXC is updated before returning if requested by the user
if req.WaitForUpdate {
toWait := req.WaitForTime
if toWait == 0 {
toWait = 5 * time.Minute
}
ticker := time.NewTicker(30 * time.Second) // check on the update status every 30 seconds
timer := time.NewTimer(toWait)
defer ticker.Stop()
defer timer.Stop()
for {
select {
case <-timer.C:
return nil, fmt.Errorf("time expired waiting for VXC %s to update", id)
case <-ctx.Done():
return nil, fmt.Errorf("context expired waiting for VXC %s to update", id)
case <-ticker.C:
vxc, err := svc.GetVXC(ctx, id)
if err != nil {
return nil, err
}
var isUpdated bool
if vxc.ProvisioningStatus == "LIVE" {
isUpdated = true
}
if isUpdated {
return vxc, nil
}
}
}
} else {
// return the response right away if the user doesn't want to wait for update
return &vxcDetails.Data, nil
}
}
// LookupPartnerPorts looks up available partner ports in the Megaport VXC API.
func (svc *VXCServiceOp) LookupPartnerPorts(ctx context.Context, req *LookupPartnerPortsRequest) (*LookupPartnerPortsResponse, error) {
lookupUrl := "/v2/secure/" + strings.ToLower(req.Partner) + "/" + req.Key
clientReq, err := svc.Client.NewRequest(ctx, http.MethodGet, lookupUrl, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)
if parseErr != nil {
return nil, parseErr
}
toReturn := &LookupPartnerPortsResponse{}
for i := 0; i < len(lookupResponse.Data.Megaports); i++ {
if lookupResponse.Data.Megaports[i].VXC == 0 && lookupResponse.Data.Megaports[i].PortSpeed >= req.PortSpeed { // nil is 0
// We only need the first available one that has enough speed capacity.
if req.ProductID == "" {
toReturn.ProductUID = lookupResponse.Data.Megaports[i].ProductUID
return toReturn, nil
// Try to match Product ID if provided
} else if lookupResponse.Data.Megaports[i].ProductUID == req.ProductID {
toReturn.ProductUID = lookupResponse.Data.Megaports[i].ProductUID
return toReturn, nil
}
}
}
return nil, ErrNoAvailableVxcPorts
}
// LookupPartnerPorts looks up available partner ports in the Megaport VXC API.
func (svc *VXCServiceOp) ListPartnerPorts(ctx context.Context, req *ListPartnerPortsRequest) (*ListPartnerPortsResponse, error) {
lookupUrl := "/v2/secure/" + strings.ToLower(req.Partner) + "/" + req.Key
clientReq, err := svc.Client.NewRequest(ctx, http.MethodGet, lookupUrl, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)
if parseErr != nil {
return nil, parseErr
}
return &ListPartnerPortsResponse{
Data: lookupResponse.Data,
}, nil
}