/
v2-network_list.go
389 lines (318 loc) · 13.5 KB
/
v2-network_list.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
package edgegrid
import (
"encoding/json"
"fmt"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
// NetworkListServicev2 represents exposed services to manage network lists
// Akamai API docs: https://developer.akamai.com/api/luna/network-list
type NetworkListServicev2 struct {
client *Client
}
// NetworkListsv2 represents array of network lists
// Akamai API docs: https://developer.akamai.com/api/luna/network-list
type NetworkListsv2 struct {
NetworkLists []NetworkListv2 `json:"networkLists"`
Links struct {
Create NetworkListLinkv2 `json:"create"`
} `json:"links"`
}
// NetworkListv2 represents the network list structure
// Akamai API docs: https://developer.akamai.com/api/luna/network-list
type NetworkListv2 struct {
NetworkListType string `json:"networkListType,omitempty`
AccessControlGroup string `json:"accessControlGroup,omitempty"`
Name string `json:"name,omitempty"`
ElementCount int `json:"elementCount,omitempty"`
Links struct {
ActivateInProduction NetworkListLinkv2 `json:"activateInProduction,omitempty"`
ActivateInStaging NetworkListLinkv2 `json:"activateInStaging,omitempty"`
AppendItems NetworkListLinkv2 `json:"appendItems,omitempty"`
Retrieve NetworkListLinkv2 `json:"retrieve,omitempty"`
StatusInProduction NetworkListLinkv2 `json:"statusInProduction,omitempty"`
StatusInStaging NetworkListLinkv2 `json:"statusInStaging,omitempty"`
Update NetworkListLinkv2 `json:"update,omitempty"`
} `json:"links"`
List []string `json:"list"`
SyncPoint int `json:"syncPoint,omitempty"`
Type string `json:"type,omitempty"`
UniqueID string `json:"uniqueId,omitempty"`
CreateDate time.Time `json:"createDate,omitempty"`
CreatedBy string `json:"createdBy,omitempty"`
ExpeditedProductionActivationStatus string `json:"expeditedProductionActivationStatus,omitempty"`
ExpeditedStagingActivationStatus string `json:"expeditedStagingActivationStatus,omitempty"`
ProductionActivationStatus string `json:"productionActivationStatus,omitempty"`
StagingActivationStatus string `json:"stagingActivationStatus,omitempty"`
UpdateDate time.Time `json:"updateDate,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
}
// AkamaiNetworkListLinks represents the network list `links` structure
// Akamai API docs: https://developer.akamai.com/api/luna/network-list
type NetworkListLinkv2 struct {
Href string `json:"href"`
Method string `json:"method"`
}
// NetworkListErrorv2 represents the error returned from Akamai
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html#errors
type NetworkListErrorv2 struct {
Detail string `json:"detail"`
Instance string `json:"instance"`
Status int `json:"status"`
Title string `json:"title"`
Type string `json:"type"`
FieldErrors struct {
Entry []struct {
Key string `json:"key"`
Value []string `json:"value"`
} `json:"entry"`
} `json:"fieldErrors"`
}
// NetworkListsOptionsv2 represents struct required to create items for network list
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
type NetworkListsOptionsv2 struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
List []string `json:"list,omitempty"`
}
// ListNetworkListsOptionsv2 represents the available options for listing network lists
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
type ListNetworkListsOptionsv2 struct {
TypeOflist string
Extended bool
IncludeElements bool
Search string
}
// NetworkListActivationOptsv2 represents object used for activating network list in Akamai
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
type NetworkListActivationOptsv2 struct {
Comments string `json:"comments"`
NotificationRecipients []string `json:"notificationRecipients"`
Fast bool `json:"fast"`
}
// NetworkListActivationStatusv2 represents object used for status of network list activation
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
type NetworkListActivationStatusv2 struct {
ActivationID int `json:"activationId"`
ActivationComments string `json:"activationComments"`
ActivationStatus string `json:"activationStatus"`
SyncPoint int `json:"syncPoint"`
UniqueID string `json:"uniqueId"`
Fast bool `json:"fast"`
}
// NetworkListErrorv2 Error() function implementation
func (e *NetworkListErrorv2) Error() string {
return ShowJSONMessage(e)
}
// NetworkListDeleteResponse represents response from deleting a list
type NetworkListDeleteResponse struct {
Status int `json:"status"`
UniqueID string `json:"uniqueId"`
SyncPoint int `json:"syncPoint"`
}
// NetworkListSubscription represents object used for (un)subscribing for notifications
type NetworkListSubscription struct {
Recipients []string `json:"recipients"`
UniqueIds []string `json:"uniqueIds"`
}
// QStrNetworkList includes query params used across network lists
type QStrNetworkList struct {
IncludeElements bool `url:"includeElements,omitempty"`
Extended bool `url:"extended,omitempty"`
Search string `url:"search,omitempty"`
Element string `url:"element,omitempty"`
}
// ListNetworkLists List all configured Network Lists for the authenticated user.
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html#getlists
func (nls *NetworkListServicev2) ListNetworkLists(opts ListNetworkListsOptionsv2) (*[]NetworkListv2, *ClientResponse, error) {
qParams := QStrNetworkList{
Extended: opts.Extended,
IncludeElements: opts.IncludeElements,
Search: opts.Search,
}
path := NetworkListPathV2
var respStruct *NetworkListsv2
log.Debug("[NetworkListServicev2]::Execute request")
resp, err := nls.client.makeAPIRequest(http.MethodGet, path, qParams, &respStruct, nil, nil)
// This error indicates we had problems connecting to Akamai endpoint(s)
if err != nil {
log.Debug("[NetworkListServicev2]::Client request error")
log.Debug(fmt.Sprintf("[NetworkListServicev2]:: %s", err))
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return &respStruct.NetworkLists, resp, netListError
}
return &respStruct.NetworkLists, resp, nil
}
// CreateNetworkList Create a new network list
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html#postlists
func (nls *NetworkListServicev2) CreateNetworkList(opts NetworkListsOptionsv2) (*NetworkListv2, *ClientResponse, error) {
qParams := QStrNetworkList{}
path := NetworkListPathV2
var respStruct *NetworkListv2
resp, err := nls.client.makeAPIRequest(http.MethodPost, path, qParams, &respStruct, opts, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// GetNetworkList Gets a specific network list
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html#getlist
func (nls *NetworkListServicev2) GetNetworkList(ListID string, opts ListNetworkListsOptionsv2) (*NetworkListv2, *ClientResponse, error) {
qParams := QStrNetworkList{
Extended: opts.Extended,
IncludeElements: opts.IncludeElements,
}
path := fmt.Sprintf("%s/%s", NetworkListPathV2, ListID)
var respStruct *NetworkListv2
resp, err := nls.client.makeAPIRequest(http.MethodGet, path, qParams, &respStruct, nil, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// AppendListNetworkList Adds items to network list
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html#postlists
func (nls *NetworkListServicev2) AppendListNetworkList(ListID string, opts NetworkListsOptionsv2) (*NetworkListv2, *ClientResponse, error) {
qParams := QStrNetworkList{}
path := fmt.Sprintf("%s/%s/append", NetworkListPathV2, ListID)
var respStruct *NetworkListv2
resp, err := nls.client.makeAPIRequest(http.MethodPost, path, qParams, &respStruct, opts, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// RemoveNetworkListElement Removes network list element
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
func (nls *NetworkListServicev2) RemoveNetworkListElement(ListID, element string) (*NetworkListv2, *ClientResponse, error) {
qParams := QStrNetworkList{
Element: element,
}
path := fmt.Sprintf("%s/%s/elements", NetworkListPathV2, ListID)
var respStruct *NetworkListv2
resp, err := nls.client.makeAPIRequest(http.MethodDelete, path, qParams, &respStruct, nil, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// ActivateNetworkList Activates network list on specified network ( PRODUCTION or STAGING )
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
func (nls *NetworkListServicev2) ActivateNetworkList(ListID string, targetEnv AkamaiEnvironment, opts NetworkListActivationOptsv2) (*NetworkListActivationStatusv2, *ClientResponse, error) {
qParams := QStrNetworkList{}
path := fmt.Sprintf("%s/%s/environments/%s/activate", NetworkListPathV2, ListID, targetEnv)
var respStruct *NetworkListActivationStatusv2
resp, err := nls.client.makeAPIRequest(http.MethodPost, path, qParams, &respStruct, opts, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// GetNetworkListActStatus Gets activation network list status on specified network ( PRODUCTION or STAGING )
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
func (nls *NetworkListServicev2) GetNetworkListActStatus(ListID string, targetEnv AkamaiEnvironment) (*NetworkListActivationStatusv2, *ClientResponse, error) {
qParams := QStrNetworkList{}
path := fmt.Sprintf("%s/%s/environments/%s/status", NetworkListPathV2, ListID, targetEnv)
var respStruct *NetworkListActivationStatusv2
resp, err := nls.client.makeAPIRequest(http.MethodGet, path, qParams, &respStruct, nil, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// DeleteNetworkList Remove network list element
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
func (nls *NetworkListServicev2) DeleteNetworkList(ListID string) (*NetworkListDeleteResponse, *ClientResponse, error) {
qParams := QStrNetworkList{}
path := fmt.Sprintf("%s/%s", NetworkListPathV2, ListID)
var respStruct *NetworkListDeleteResponse
resp, err := nls.client.makeAPIRequest(http.MethodDelete, path, qParams, &respStruct, nil, nil)
if err != nil {
return nil, resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return respStruct, resp, netListError
}
return respStruct, resp, err
}
// NetworkListNotification Manage network list subscription
// Akamai API docs: https://developer.akamai.com/api/cloud_security/network_lists/v2.html
func (nls *NetworkListServicev2) NetworkListNotification(action AkamaiSubscription, sub NetworkListSubscription) (*ClientResponse, error) {
qParams := QStrNetworkList{}
path := fmt.Sprintf("/network-list/v2/notifications/%s", action)
resp, err := nls.client.makeAPIRequest(http.MethodPost, path, qParams, nil, sub, nil)
if err != nil {
return resp, err
}
if resp.Response.StatusCode >= http.StatusBadRequest {
netListError := &NetworkListErrorv2{}
err := json.Unmarshal([]byte(resp.Body), &netListError)
if err != nil {
log.Fatal(err)
}
return resp, err
}
return resp, err
}