-
Notifications
You must be signed in to change notification settings - Fork 8
/
product.go
306 lines (251 loc) · 10.3 KB
/
product.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
package megaport
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
)
// ProductService is an interface for interfacing with the Product endpoints of the Megaport API.
type ProductService interface {
// ExecuteOrder is responsible for executing an order for a product in the Megaport Products API.
ExecuteOrder(ctx context.Context, requestBody interface{}) (*[]byte, error)
// ModifyProduct modifies a product in the Megaport Products API. The available fields to modify are Name, Cost Centre, and Marketplace Visibility.
ModifyProduct(ctx context.Context, req *ModifyProductRequest) (*ModifyProductResponse, error)
// DeleteProduct is responsible for either scheduling a product for deletion "CANCEL" or deleting a product immediately "CANCEL_NOW" in the Megaport Products API.
DeleteProduct(ctx context.Context, req *DeleteProductRequest) (*DeleteProductResponse, error)
// RestoreProduct is responsible for restoring a product in the Megaport Products API. The product must be in a "CANCELLED" state to be restored.
RestoreProduct(ctx context.Context, productId string) (*RestoreProductResponse, error)
// ManageProductLock is responsible for locking or unlocking a product in the Megaport Products API.
ManageProductLock(ctx context.Context, req *ManageProductLockRequest) (*ManageProductLockResponse, error)
// ValidateProductOrder is responsible for validating an order for a product in the Megaport Products API.
ValidateProductOrder(ctx context.Context, requestBody interface{}) error
// ListProductResourceTags is responsible for retrieving the resource tags for a product in the Megaport Products API.
ListProductResourceTags(ctx context.Context, productID string) ([]ResourceTag, error)
// UpdateProductResourceTags is responsible for updating the resource tags for a product in the Megaport Products API.
UpdateProductResourceTags(ctx context.Context, productUID string, tagsReq *UpdateProductResourceTagsRequest) error
}
// ProductServiceOp handles communication with Product methods of the Megaport API.
type ProductServiceOp struct {
Client *Client
}
// NewProductService creates a new instance of the Product Service.
func NewProductService(c *Client) *ProductServiceOp {
return &ProductServiceOp{
Client: c,
}
}
// ModifyProductRequest represents a request to modify a product in the Megaport Products API.
type ModifyProductRequest struct {
ProductID string
ProductType string
Name string `json:"name,omitempty"`
CostCentre string `json:"costCentre,omitempty"`
MarketplaceVisibility *bool `json:"marketplaceVisibility,omitempty"`
ContractTermMonths int `json:"term,omitempty"`
}
// ModifyProductResponse represents a response from the Megaport Products API after modifying a product.
type ModifyProductResponse struct {
IsUpdated bool
}
// DeleteProductRequest represents a request to delete a product in the Megaport Products API.
type DeleteProductRequest struct {
ProductID string
DeleteNow bool
}
// DeleteProductResponse represents a response from the Megaport Products API after deleting a product.
type DeleteProductResponse struct{}
// RestoreProductRequest represents a request to restore a product in the Megaport Products API.
type RestoreProductRequest struct {
ProductID string
}
// RestoreProductResponse represents a response from the Megaport Products API after restoring a product.
type RestoreProductResponse struct{}
// ManageProductLockRequest represents a request to lock or unlock a product in the Megaport Products API.
type ManageProductLockRequest struct {
ProductID string
ShouldLock bool
}
// ManageProductLockResponse represents a response from the Megaport Products API after locking or unlocking a product.
type ManageProductLockResponse struct{}
// ParsedProductsResponse represents a response from the Megaport Products API prior to parsing the response.
type ParsedProductsResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data []interface{} `json:"data"`
}
// ResourceTagsResponse represents a response from the Megaport Products API after retrieving the resource tags for a product.
type ResourceTagsResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data *ResourceTagsResponseData `json:"data"`
}
type ResourceTagsResponseData struct {
ResourceTags []ResourceTag `json:"resourceTags"`
}
type UpdateProductResourceTagsRequest struct {
ResourceTags []ResourceTag `json:"resourceTags"`
}
type ResourceTag struct {
Key string `json:"key"`
Value string `json:"value"`
}
// ExecuteOrder is responsible for executing an order for a product in the Megaport Products API.
func (svc *ProductServiceOp) ExecuteOrder(ctx context.Context, requestBody interface{}) (*[]byte, error) {
path := "/v3/networkdesign/buy"
url := svc.Client.BaseURL.JoinPath(path).String()
req, err := svc.Client.NewRequest(ctx, http.MethodPost, url, requestBody)
if err != nil {
return nil, err
}
response, resErr := svc.Client.Do(ctx, req, nil)
if resErr != nil {
return nil, resErr
}
if response != nil {
svc.Client.Logger.DebugContext(ctx, "Executing product order", slog.String("url", url), slog.Int("status_code", response.StatusCode))
defer response.Body.Close()
}
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
return &body, nil
}
// ModifyProduct modifies a product in the Megaport Products API. The available fields to modify are Name, Cost Centre, and Marketplace Visibility.
func (svc *ProductServiceOp) ModifyProduct(ctx context.Context, req *ModifyProductRequest) (*ModifyProductResponse, error) {
if req.ProductType == PRODUCT_MEGAPORT || req.ProductType == PRODUCT_MCR || req.ProductType == PRODUCT_MVE {
path := fmt.Sprintf("/v2/product/%s/%s", req.ProductType, req.ProductID)
url := svc.Client.BaseURL.JoinPath(path).String()
req, err := svc.Client.NewRequest(ctx, http.MethodPut, url, req)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, req, nil)
if err != nil {
return nil, err
}
return &ModifyProductResponse{IsUpdated: true}, nil
} else {
return nil, ErrWrongProductModify
}
}
// DeleteProduct is responsible for either scheduling a product for deletion "CANCEL" or deleting a product immediately "CANCEL_NOW" in the Megaport Products API.
func (svc *ProductServiceOp) DeleteProduct(ctx context.Context, req *DeleteProductRequest) (*DeleteProductResponse, error) {
var action string
if req.DeleteNow {
action = "CANCEL_NOW"
} else {
action = "CANCEL"
}
path := "/v3/product/" + req.ProductID + "/action/" + action
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodPost, url, nil)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
return &DeleteProductResponse{}, nil
}
// RestoreProduct is responsible for restoring a product in the Megaport Products API. The product must be in a "CANCELLED" state to be restored.
func (svc *ProductServiceOp) RestoreProduct(ctx context.Context, productId string) (*RestoreProductResponse, error) {
path := "/v3/product/" + productId + "/action/UN_CANCEL"
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodPost, url, nil)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
return &RestoreProductResponse{}, nil
}
// ManageProductLock is responsible for locking or unlocking a product in the Megaport Products API.
func (svc *ProductServiceOp) ManageProductLock(ctx context.Context, req *ManageProductLockRequest) (*ManageProductLockResponse, error) {
verb := "POST"
if !req.ShouldLock {
verb = "DELETE"
}
path := fmt.Sprintf("/v2/product/%s/lock", req.ProductID)
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, verb, url, nil)
if err != nil {
return nil, err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return nil, err
}
return &ManageProductLockResponse{}, nil
}
// ValidateProductOrder is responsible for validating an order for a product in the Megaport Products API.
func (svc *ProductServiceOp) ValidateProductOrder(ctx context.Context, requestBody interface{}) error {
path := "/v3/networkdesign/validate"
url := svc.Client.BaseURL.JoinPath(path).String()
req, err := svc.Client.NewRequest(ctx, http.MethodPost, url, requestBody)
if err != nil {
return err
}
_, resErr := svc.Client.Do(ctx, req, nil)
if resErr != nil {
return resErr
}
return nil
}
// ListProductResourceTags is responsible for retrieving the resource tags for a product in the Megaport Products API.
func (svc *ProductServiceOp) ListProductResourceTags(ctx context.Context, productUID string) ([]ResourceTag, error) {
path := fmt.Sprintf("/v2/product/%s/tags", productUID)
url := svc.Client.BaseURL.JoinPath(path).String()
req, err := svc.Client.NewRequest(ctx, http.MethodGet, 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()
tagsResponse := &ResourceTagsResponse{}
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
err = json.Unmarshal(body, tagsResponse)
if err != nil {
return nil, err
}
return tagsResponse.Data.ResourceTags, nil
}
// UpdateProductResourceTags is responsible for updating the resource tags for a product in the Megaport Products API.
func (svc *ProductServiceOp) UpdateProductResourceTags(ctx context.Context, productUID string, tagsReq *UpdateProductResourceTagsRequest) error {
path := fmt.Sprintf("/v2/product/%s/tags", productUID)
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodPut, url, tagsReq)
if err != nil {
return err
}
_, err = svc.Client.Do(ctx, clientReq, nil)
if err != nil {
return err
}
return nil
}
func toProductResourceTags(in map[string]string) []ResourceTag {
tags := make([]ResourceTag, 0, len(in))
for key, value := range in {
tags = append(tags, ResourceTag{Key: key, Value: value})
}
return tags
}
func fromProductResourceTags(in []ResourceTag) map[string]string {
tags := make(map[string]string, len(in))
for _, tag := range in {
tags[tag.Key] = tag.Value
}
return tags
}