-
Notifications
You must be signed in to change notification settings - Fork 8
/
mcr.go
506 lines (431 loc) · 15.7 KB
/
mcr.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
package megaport
import (
"context"
"encoding/json"
"fmt"
"io"
"slices"
"strconv"
"time"
)
// MCRService is an interface for interfacing with the MCR endpoints
// of the Megaport API.
type MCRService interface {
// BuyMCR buys an MCR from the Megaport MCR API.
BuyMCR(ctx context.Context, req *BuyMCRRequest) (*BuyMCRResponse, error)
// ValidateMCROrder validates an MCR order in the Megaport Products API.
ValidateMCROrder(ctx context.Context, req *BuyMCRRequest) error
// GetMCR gets details about a single MCR from the Megaport MCR API.
GetMCR(ctx context.Context, mcrId string) (*MCR, error)
// CreatePrefixFilterList creates a Prefix Filter List on an MCR from the Megaport MCR API.
CreatePrefixFilterList(ctx context.Context, req *CreateMCRPrefixFilterListRequest) (*CreateMCRPrefixFilterListResponse, error)
// ListMCRPrefixFilterLists returns prefix filter lists for the specified MCR2 from the Megaport MCR API.
ListMCRPrefixFilterLists(ctx context.Context, mcrId string) ([]*PrefixFilterList, error)
// GetMCRPrefixFilterList returns a single prefix filter list by ID for the specified MCR2 from the Megaport MCR API.
GetMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int) (*MCRPrefixFilterList, error)
// ModifyMCRPrefixFilterList modifies a prefix filter list on an MCR in the Megaport MCR API.
ModifyMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int, prefixFilterList *MCRPrefixFilterList) (*ModifyMCRPrefixFilterListResponse, error)
// DeleteMCRPrefixFilterList deletes a prefix filter list on an MCR from the Megaport MCR API.
DeleteMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int) (*DeleteMCRPrefixFilterListResponse, error)
// ModifyMCR modifies an MCR in the Megaport MCR API.
ModifyMCR(ctx context.Context, req *ModifyMCRRequest) (*ModifyMCRResponse, error)
// DeleteMCR deletes an MCR in the Megaport MCR API.
DeleteMCR(ctx context.Context, req *DeleteMCRRequest) (*DeleteMCRResponse, error)
// RestoreMCR restores a deleted MCR in the Megaport MCR API.
RestoreMCR(ctx context.Context, mcrId string) (*RestoreMCRResponse, error)
// ListMCRResourceTags returns the resource tags for an MCR in the Megaport MCR API.
ListMCRResourceTags(ctx context.Context, mcrID string) (map[string]string, error)
// UpdateMCRResourceTags updates the resource tags for an MCR in the Megaport MCR API.
UpdateMCRResourceTags(ctx context.Context, mcrID string, tags map[string]string) error
// DEPRECATED - Use ListMCRPrefixFilterLists instead
GetMCRPrefixFilterLists(ctx context.Context, mcrId string) ([]*PrefixFilterList, error)
}
// MCRServiceOp handles communication with MCR methods of the Megaport API.
type MCRServiceOp struct {
Client *Client
}
// NewMCRService creates a new instance of the MCR Service.
func NewMCRService(c *Client) *MCRServiceOp {
return &MCRServiceOp{
Client: c,
}
}
// BuyMCRRequest represents a request to buy an MCR
type BuyMCRRequest struct {
LocationID int
Name string
DiversityZone string
Term int
PortSpeed int
MCRAsn int
CostCentre string
PromoCode string
ResourceTags map[string]string `json:"resourceTags,omitempty"`
WaitForProvision bool // Wait until the MCR provisions before returning
WaitForTime time.Duration // How long to wait for the MCR to provision if WaitForProvision is true (default is 5 minutes)
}
// BuyMCRResponse represents a response from buying an MCR
type BuyMCRResponse struct {
TechnicalServiceUID string
}
// CreateMCRPrefixFilterListRequest represents a request to create a prefix filter list on an MCR
type CreateMCRPrefixFilterListRequest struct {
MCRID string
PrefixFilterList MCRPrefixFilterList
}
// CreateMCRPrefixFilterListResponse represents a response from creating a prefix filter list on an MCR
type CreateMCRPrefixFilterListResponse struct {
IsCreated bool
PrefixFilterListID int // The ID of the created prefix filter list
}
// ModifyMCRRequest represents a request to modify an MCR
type ModifyMCRRequest struct {
MCRID string
Name string
CostCentre string
MarketplaceVisibility *bool
ContractTermMonths *int
WaitForUpdate bool // Wait until the MCR updates before returning
WaitForTime time.Duration // How long to wait for the MCR to update if WaitForUpdate is true (default is 5 minutes)
}
// ModifyMCRResponse represents a response from modifying an MCR
type ModifyMCRResponse struct {
IsUpdated bool
}
// DeleteMCRRequest represents a request to delete an MCR
type DeleteMCRRequest struct {
MCRID string
DeleteNow bool
}
// DeleteMCRResponse represents a response from deleting an MCR
type DeleteMCRResponse struct {
IsDeleting bool
}
// RestoreMCRequest represents a request to restore a deleted MCR
type RestoreMCRResponse struct {
IsRestored bool
}
// ModifyMCRPrefixFilterListRequest represents a request to modify a prefix filter list on an MCR
type ModifyMCRPrefixFilterListResponse struct {
IsUpdated bool
}
// DeleteMCRPrefixFilterListResponse represents a response from deleting a prefix filter list on an MCR
type DeleteMCRPrefixFilterListResponse struct {
IsDeleted bool
}
// BuyMCR purchases an MCR from the Megaport MCR API.
func (svc *MCRServiceOp) BuyMCR(ctx context.Context, req *BuyMCRRequest) (*BuyMCRResponse, error) {
err := validateBuyMCRRequest(req)
if err != nil {
return nil, err
}
mcrOrders := createMCROrder(req)
body, resErr := svc.Client.ProductService.ExecuteOrder(ctx, mcrOrders)
if resErr != nil {
return nil, resErr
}
orderInfo := MCROrderResponse{}
unmarshalErr := json.Unmarshal(*body, &orderInfo)
if unmarshalErr != nil {
return nil, unmarshalErr
}
toReturn := &BuyMCRResponse{
TechnicalServiceUID: orderInfo.Data[0].TechnicalServiceUID,
}
// wait until the MCR is provisioned before returning if requested 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 MCR %s to provision", toReturn.TechnicalServiceUID)
case <-ctx.Done():
return nil, fmt.Errorf("context expired waiting for MCR %s to provision", toReturn.TechnicalServiceUID)
case <-ticker.C:
mcrDetails, err := svc.GetMCR(ctx, toReturn.TechnicalServiceUID)
if err != nil {
return nil, err
}
if slices.Contains(SERVICE_STATE_READY, mcrDetails.ProvisioningStatus) {
return toReturn, nil
}
}
}
} else {
// return the service UID right away if the user doesn't want to wait for provision
return toReturn, nil
}
}
// validateBuyMCRRequest validates the BuyMCRRequest for a valid term and port speed.
func validateBuyMCRRequest(order *BuyMCRRequest) error {
if order.Term != 1 && order.Term != 12 && order.Term != 24 && order.Term != 36 {
return ErrInvalidTerm
}
if order.PortSpeed != 1000 && order.PortSpeed != 2500 && order.PortSpeed != 5000 && order.PortSpeed != 10000 {
return ErrMCRInvalidPortSpeed
}
return nil
}
func createMCROrder(req *BuyMCRRequest) []MCROrder {
order := MCROrder{
LocationID: req.LocationID,
Name: req.Name,
Term: req.Term,
Type: "MCR2",
PortSpeed: req.PortSpeed,
PromoCode: req.PromoCode,
ResourceTags: toProductResourceTags(req.ResourceTags),
Config: MCROrderConfig{},
}
if req.CostCentre != "" {
order.CostCentre = req.CostCentre
}
order.Config.ASN = req.MCRAsn
if req.DiversityZone != "" {
order.Config.DiversityZone = req.DiversityZone
}
return []MCROrder{order}
}
func (svc *MCRServiceOp) ValidateMCROrder(ctx context.Context, req *BuyMCRRequest) error {
err := validateBuyMCRRequest(req)
if err != nil {
return err
}
mcrOrders := createMCROrder(req)
return svc.Client.ProductService.ValidateProductOrder(ctx, mcrOrders)
}
// GetMCR returns the details of a single MCR in the Megaport MCR API.
func (svc *MCRServiceOp) GetMCR(ctx context.Context, mcrId string) (*MCR, error) {
url := "/v2/product/" + mcrId
clientReq, err := svc.Client.NewRequest(ctx, "GET", 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, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
mcrRes := &MCRResponse{}
unmarshalErr := json.Unmarshal(body, mcrRes)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return mcrRes.Data, nil
}
// CreatePrefixFilterList creates a Prefix Filter List on an MCR from the Megaport MCR API.
func (svc *MCRServiceOp) CreatePrefixFilterList(ctx context.Context, req *CreateMCRPrefixFilterListRequest) (*CreateMCRPrefixFilterListResponse, error) {
url := "/v2/product/mcr2/" + req.MCRID + "/prefixList"
clientReq, err := svc.Client.NewRequest(ctx, "POST", url, req.PrefixFilterList)
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
}
createRes := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, createRes)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return &CreateMCRPrefixFilterListResponse{
IsCreated: true,
PrefixFilterListID: createRes.Data.ID,
}, nil
}
// DEPRECATED - Use ListMCRPrefixFilterLists instead
func (svc *MCRServiceOp) GetMCRPrefixFilterLists(ctx context.Context, mcrId string) ([]*PrefixFilterList, error) {
res, err := svc.ListMCRPrefixFilterLists(ctx, mcrId)
if err != nil {
return nil, err
}
return res, nil
}
// GetMCRPrefixFilterLists returns prefix filter lists for the specified MCR2 from the Megaport MCR API.
func (svc *MCRServiceOp) ListMCRPrefixFilterLists(ctx context.Context, mcrId string) ([]*PrefixFilterList, error) {
url := "/v2/product/mcr2/" + mcrId + "/prefixLists?"
req, err := svc.Client.NewRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
prefixFilterList := &ListMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, prefixFilterList)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return prefixFilterList.Data, nil
}
// GetMCRPrefixFilterList returns a single prefix filter list by ID for the specified MCR2 from the Megaport MCR API.
func (svc *MCRServiceOp) GetMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int) (*MCRPrefixFilterList, error) {
listID := strconv.Itoa(prefixFilterListID)
url := "/v2/product/mcr2/" + mcrID + "/prefixList/" + listID
req, err := svc.Client.NewRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
response, err := svc.Client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
apiPrefixFilterList := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, apiPrefixFilterList)
if unmarshalErr != nil {
return nil, unmarshalErr
}
prefixFilterList, err := apiPrefixFilterList.Data.ToMCRPrefixFilterList()
if err != nil {
return nil, err
}
return prefixFilterList, nil
}
// ModifyMCR modifies an MCR in the Megaport MCR API.
func (svc *MCRServiceOp) ModifyMCR(ctx context.Context, req *ModifyMCRRequest) (*ModifyMCRResponse, error) {
if len(req.CostCentre) > 255 {
return nil, ErrCostCentreTooLong
}
modifyReq := &ModifyProductRequest{
ProductID: req.MCRID,
ProductType: PRODUCT_MCR,
Name: req.Name,
CostCentre: req.CostCentre,
MarketplaceVisibility: req.MarketplaceVisibility,
}
if req.ContractTermMonths != nil {
modifyReq.ContractTermMonths = *req.ContractTermMonths
}
_, err := svc.Client.ProductService.ModifyProduct(ctx, modifyReq)
if err != nil {
return nil, err
}
toReturn := &ModifyMCRResponse{
IsUpdated: true,
}
// wait until the MCR 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 MCR %s to update", req.MCRID)
case <-ctx.Done():
return nil, fmt.Errorf("context expired waiting for MCR %s to update", req.MCRID)
case <-ticker.C:
mcrDetails, err := svc.GetMCR(ctx, req.MCRID)
if err != nil {
return nil, err
}
if slices.Contains(SERVICE_STATE_READY, mcrDetails.ProvisioningStatus) {
return toReturn, nil
}
}
}
} else {
// return the response right away if the user doesn't want to wait for update
return toReturn, nil
}
}
// DeleteMCRPrefixFilterList deletes a prefix filter list on an MCR from the Megaport MCR API.
func (svc *MCRServiceOp) DeleteMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int) (*DeleteMCRPrefixFilterListResponse, error) {
url := fmt.Sprintf("/v2/product/mcr2/%s/prefixList/%d", mcrID, prefixFilterListID)
clientReq, err := svc.Client.NewRequest(ctx, "DELETE", url, nil)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
return &DeleteMCRPrefixFilterListResponse{
IsDeleted: true,
}, nil
}
// ModifyMCRPrefixFilterList modifies a prefix filter list on an MCR in the Megaport MCR API.
func (svc *MCRServiceOp) ModifyMCRPrefixFilterList(ctx context.Context, mcrID string, prefixFilterListID int, prefixFilterList *MCRPrefixFilterList) (*ModifyMCRPrefixFilterListResponse, error) {
url := fmt.Sprintf("/v2/product/mcr2/%s/prefixList/%d", mcrID, prefixFilterListID)
clientReq, err := svc.Client.NewRequest(ctx, "PUT", url, prefixFilterList)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
return &ModifyMCRPrefixFilterListResponse{
IsUpdated: true,
}, nil
}
// DeleteMCR deletes an MCR in the Megaport MCR API.
func (svc *MCRServiceOp) DeleteMCR(ctx context.Context, req *DeleteMCRRequest) (*DeleteMCRResponse, error) {
_, err := svc.Client.ProductService.DeleteProduct(ctx, &DeleteProductRequest{
ProductID: req.MCRID,
DeleteNow: req.DeleteNow,
})
if err != nil {
return nil, err
}
return &DeleteMCRResponse{
IsDeleting: true,
}, nil
}
// Restore restores a deleted MCR in the Megaport MCR API.
func (svc *MCRServiceOp) RestoreMCR(ctx context.Context, mcrId string) (*RestoreMCRResponse, error) {
_, err := svc.Client.ProductService.RestoreProduct(ctx, mcrId)
if err != nil {
return nil, err
}
return &RestoreMCRResponse{
IsRestored: true,
}, nil
}
// ListMCRResourceTags returns the resource tags for an MCR in the Megaport MCR API.
func (svc *MCRServiceOp) ListMCRResourceTags(ctx context.Context, mcrID string) (map[string]string, error) {
tags, err := svc.Client.ProductService.ListProductResourceTags(ctx, mcrID)
if err != nil {
return nil, err
}
return fromProductResourceTags(tags), nil
}
// UpdateMCRResourceTags updates the resource tags for an MCR in the Megaport MCR API.
func (svc *MCRServiceOp) UpdateMCRResourceTags(ctx context.Context, mcrID string, tags map[string]string) error {
return svc.Client.ProductService.UpdateProductResourceTags(ctx, mcrID, &UpdateProductResourceTagsRequest{
ResourceTags: toProductResourceTags(tags),
})
}