-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
402 lines (385 loc) · 11 KB
/
main.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
package main
import (
"fmt"
"os"
"sort"
common "github.com/apiheat/akamai-cli-common/v4"
edgegrid "github.com/apiheat/go-edgegrid/v6/edgegrid"
service "github.com/apiheat/go-edgegrid/v6/service/netlistv2"
"github.com/urfave/cli/v2"
)
var (
apiClient *service.Netlistv2
appVer, appName string
)
func main() {
app := common.CreateNewApp(appName, "A CLI to interact with Akamai network lists", appVer)
app.Flags = common.CreateFlags()
app.Before = func(c *cli.Context) error {
var creds *edgegrid.Credentials
if c.String("config") != common.HomeDir() {
var err error
creds, err = edgegrid.NewCredentials().FromFile(c.String("config")).Section(c.String("section"))
if err != nil {
return err
}
} else {
creds = edgegrid.NewCredentials().AutoLoad(c.String("section"))
}
if creds == nil {
return fmt.Errorf("Cannot load credentials")
}
config := edgegrid.NewConfig().
WithCredentials(creds).
WithLogVerbosity(c.String("debug")).
WithAccountSwitchKey(c.String("ask"))
if c.String("debug") == "debug" {
config = config.WithRequestDebug(true)
}
// Provide struct details needed for apiClient init
apiClient = service.New(config)
return nil
}
app.Commands = []*cli.Command{
&cli.Command{
Name: "get",
Usage: "List network lists objects",
Subcommands: []*cli.Command{
&cli.Command{
Name: "all",
Usage: "Gets all network list in the account",
UsageText: fmt.Sprintf("%s get all [command options]", appName),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "extended",
Usage: "returns more verbose data such as creation date and activation status",
},
&cli.BoolFlag{
Name: "includeElements",
Usage: "includes the full list of IP or GEO elements",
},
&cli.StringFlag{
Name: "listType",
Value: "ANY",
Usage: "filters by the network list type [ IP | GEO | ANY ]",
},
},
Action: cmdlistNetLists,
},
&cli.Command{
Name: "by-id",
Usage: "Gets a network list by unique-id",
UsageText: fmt.Sprintf("%s get by-id --id UNIQUE-ID [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
Required: true,
},
&cli.BoolFlag{
Name: "extended",
Usage: "returns more verbose data such as creation date and activation status",
},
&cli.BoolFlag{
Name: "includeElements",
Usage: "includes the full list of IP or GEO elements",
},
},
Action: cmdlistNetListID,
},
&cli.Command{
Name: "by-name",
Usage: "Gets a network list by name",
UsageText: fmt.Sprintf("%s get by-name --name NAME [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "list name",
Required: true,
},
&cli.BoolFlag{
Name: "extended",
Usage: "returns more verbose data such as creation date and activation status",
},
&cli.BoolFlag{
Name: "includeElements",
Usage: "includes the full list of IP or GEO elements",
},
&cli.StringFlag{
Name: "listType",
Value: "IP",
Usage: "filters by the network list type [ IP | GEO ]",
},
},
Action: cmdlistNetListName,
},
&cli.Command{
Name: "by-syncpoint",
Usage: "Gets a network list by specific syncPoint",
UsageText: fmt.Sprintf("%s get by-syncpoint --id UNIQUE-ID --syncPoint VALUE [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
Required: true,
},
&cli.IntFlag{
Name: "syncpoint",
Usage: "Specific syncPoint of the list",
Required: true,
},
&cli.BoolFlag{
Name: "extended",
Usage: "returns more verbose data such as creation date and activation status",
},
},
Action: cmdlistNetListSyncPoint,
},
},
},
&cli.Command{
Name: "search",
Usage: "Finds all network lists that match specific expression ( either name or network element )",
UsageText: fmt.Sprintf("%s search --searchPattern SEARCH-ELEMENT [command options]", appName),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "extended",
Usage: "returns more verbose data such as creation date and activation status",
},
&cli.StringFlag{
Name: "searchPattern",
Usage: "includes network lists that match search pattern",
Required: true,
},
&cli.StringFlag{
Name: "listType",
Value: "ANY",
Usage: "filters by the network list type [ IP | GEO | ANY ]",
},
},
Action: cmdSearchNetLists,
},
&cli.Command{
Name: "sync",
Usage: "Synchronizes items from source list into destination list ( without activation )",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Usage: "Shows action to be performed",
Value: false,
},
},
Subcommands: []*cli.Command{
&cli.Command{
Name: "aka", //TODO: Name of this command might be changed *** BETA ***
Usage: "Synchronizes items from source list into destination list in Akamai",
UsageText: fmt.Sprintf("%s sync-items --id-src SOURCE-LIST-ID --id-dst TARGET-LIST-ID [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id-src",
Usage: "Source list ID to take items from",
Required: true,
},
&cli.StringFlag{
Name: "id-dst",
Usage: "Target list ID to which items should be added",
Required: true,
},
&cli.BoolFlag{
Name: "force",
Usage: "Enables removal of addresses from Akamai network",
},
},
Action: cmdSyncNetListID,
},
&cli.Command{
Name: "local", //TODO: Name of this command might be changed *** BETA ***
Usage: "Synchronizes items from local file into destination list in Akamai",
UsageText: fmt.Sprintf("%s sync-items --from-file PATH-TO-FILE --id-dst TARGET-LIST-ID [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from-file",
Usage: "Source list ID to take items from",
Required: true,
},
&cli.StringFlag{
Name: "id-dst",
Usage: "Target list ID to which items should be added",
Required: true,
},
&cli.BoolFlag{
Name: "force",
Usage: "Enables removal of addresses from Akamai network",
},
},
Action: cmdsyncNetListWithFile,
},
},
},
&cli.Command{
Name: "items",
Usage: "Manages items in network lists",
Subcommands: []*cli.Command{
&cli.Command{
Name: "add",
Usage: "Adds network list element to provided network list",
UsageText: fmt.Sprintf("%s items add --id UNIQUE-ID --items ITEM1,ITEM2,ITEM3", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
},
&cli.StringFlag{
Name: "items",
Usage: "items to be included",
},
&cli.StringFlag{
Name: "from-file",
Usage: "items to be included from file",
},
},
Action: cmdAddItemsToNetlist,
},
&cli.Command{
Name: "remove",
Usage: "Removes network list element from provided network list",
UsageText: fmt.Sprintf("%s items remove --id UNIQUE-ID --element ELEMENT", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
Required: true,
},
&cli.StringFlag{
Name: "element",
Usage: "element to be removed",
Required: true,
},
},
Action: cmdRemoveItemFromNetlist,
},
},
},
&cli.Command{
Name: "create",
Usage: "Creates new network list",
UsageText: fmt.Sprintf("%s create --name NETWORK-LIST-NAME [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Value: "",
Usage: "name for the new list",
Required: true,
},
&cli.StringFlag{
Name: "description",
Value: "created via akamai-cli-networklist",
Usage: "description for the new list",
},
&cli.StringFlag{
Name: "type",
Value: "IP",
Usage: "defines type of list for creation (IP/GEO)",
},
},
Action: cmdCreateNetList,
},
&cli.Command{
Name: "activate",
Usage: "Manages network list activation/status",
Subcommands: []*cli.Command{
&cli.Command{
Name: "list",
Usage: "Activates network list on given network",
UsageText: fmt.Sprintf("%s activate list --id UNIQUE-ID [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
Required: true,
},
&cli.StringFlag{
Name: "comments",
Value: "activated via akamai-cli",
Usage: "comments",
},
&cli.StringSliceFlag{
Name: "notificationRecipients",
Usage: "recipients of notification",
},
&cli.BoolFlag{
Name: "fast",
Usage: "n/a",
},
&cli.BoolFlag{
Name: "prd",
Usage: "activate on production",
},
},
Action: cmdActivateNetList,
},
&cli.Command{
Name: "status",
Usage: "Displays activation status for given network list",
UsageText: fmt.Sprintf("%s activate status --id UNIQUE-ID [command options]", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id",
Required: true,
},
&cli.BoolFlag{
Name: "prd",
Usage: "activate on production",
},
},
Action: cmdActivateNetListStatus,
},
},
},
&cli.Command{
Name: "delete",
Usage: "Deletes network list ( ** REQUIRES LIST TO BE DEACTIVATED ON BOTH NETWORKS ** )",
UsageText: fmt.Sprintf("%s delete --id UNIQUE-ID", appName),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "list unique-id to remove",
Required: true,
},
},
Action: cmdRemoveNetlist,
},
&cli.Command{
Name: "notification",
Usage: "Manages network list subscription notifications ( SUBSCRIBE by default ) ",
UsageText: fmt.Sprintf("%s notification status --id UNIQUE-ID --notificationRecipients RECIPIENTS [command options]", appName),
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "networkListsIDs",
Usage: "recipients of notification",
Required: true,
},
&cli.StringSliceFlag{
Name: "notificationRecipients",
Usage: "recipients of notification",
Required: true,
},
&cli.BoolFlag{
Name: "unsubscribe",
Usage: "Unsubscribe from notifications",
},
},
Action: cmdNotificationManagement,
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Action = func(c *cli.Context) error {
return nil
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
}
}