forked from opsgenie/opsgenie-lamp
/
lamp.go
586 lines (561 loc) · 14.9 KB
/
lamp.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2015 OpsGenie. All rights reserved.
// Use of this source code is governed by a Apache Software
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
gcli "github.com/codegangsta/cli"
"github.com/opsgenie/opsgenie-lamp/command"
)
const lampVersion string = "1.0.0"
var commonFlags = []gcli.Flag{
gcli.BoolFlag{
Name: "v",
Usage: "Execute commands in verbose mode",
},
gcli.StringFlag{
Name: "apiKey",
Usage: "API key used for authenticating API requests. If not given, the api key in the conf file is used",
},
gcli.StringFlag{
Name: "user",
Usage: "Owner of the execution",
},
gcli.StringFlag{
Name: "config",
Usage: "Configuration file path",
},
}
func createAlertCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "message",
Usage: "Alert text limited to 130 characters",
},
gcli.StringFlag{
Name: "recipients",
Usage: "The user names of individual users or names of groups",
},
gcli.StringFlag{
Name: "teams",
Usage: "A comma seperated list of teams",
},
gcli.StringFlag{
Name: "alias",
Usage: "A user defined identifier for the alert and there can be only one alert with open status with the same alias.",
},
gcli.StringFlag{
Name: "actions",
Usage: "A comma separated list of actions that can be executed",
},
gcli.StringFlag{
Name: "source",
Usage: "Field to specify source of alert. By default, it will be assigned to IP address of incoming request",
},
gcli.StringFlag{
Name: "tags",
Usage: "A comma separated list of labels attached to the alert",
},
gcli.StringFlag{
Name: "description",
Usage: "Alert text in long form. Unlike the message field, not limited to 130 characters",
},
gcli.StringFlag{
Name: "entity",
Usage: "The entity the alert is related to",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringSliceFlag{
Name: "D",
Usage: "Additional alert properties.\n\tSyntax: -D key=value",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "createAlert",
Flags: flags,
Usage: "Creates an alert at OpsGenie",
Action: command.CreateAlertAction,
}
return cmd
}
func getAlertCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be retrieved. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be retrieved. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "output-format",
Value: "json",
Usage: "Prints the output in json or yaml formats",
},
gcli.BoolFlag{
Name: "pretty",
Usage: "For more readable JSON output",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "getAlert",
Flags: flags,
Usage: "Gets an alert content from OpsGenie",
Action: command.GetAlertAction,
}
return cmd
}
func attachFileCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that the file will be attached. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that the file will be attached. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "attachment",
Usage: "Absolute or relative path to the file",
},
gcli.StringFlag{
Name: "indexFile",
Usage: "",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "attachFile",
Flags: flags,
Usage: "Attaches files to an alert",
Action: command.AttachFileAction,
}
return cmd
}
func acknowledgeCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be acknowledged. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be acknowledged. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "acknowledge",
Flags: flags,
Usage: "Acknowledges an alert at OpsGenie",
Action: command.AcknowledgeAction,
}
return cmd
}
func renotifyCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that recipient will be renotified for. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that recipient will be renotified for. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "recipients",
Usage: "The user names of individual users or names of groups that will be renotified for alert",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "renotify",
Flags: flags,
Usage: "Renotifies recipients at OpsGenie.",
Action: command.RenotifyAction,
}
return cmd
}
func takeOwnershipCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be owned. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be owned. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "takeOwnership",
Flags: flags,
Usage: "Takes the ownership of an alert at OpsGenie.",
Action: command.TakeOwnershipAction,
}
return cmd
}
func assignOwnerCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be owned. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be owned. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "owner",
Usage: "The users who will be the owner of the alert after the execution",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "assign",
Flags: flags,
Usage: "Assigns the ownership of an alert to the specified user.",
Action: command.AssignOwnerAction,
}
return cmd
}
func addTeamCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that the new team will be added. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that the new team will be added. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "team",
Usage: "The team that will be added to the alert",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "addTeam",
Flags: flags,
Usage: "Adds a new team to an alert.",
Action: command.AddTeamAction,
}
return cmd
}
func addRecipientCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that the new recipient will be added. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that the new recipient will be added. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "recipient",
Usage: "The recipient that will be added to the alert",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "addRecipient",
Flags: flags,
Usage: "Adds a new recipient to an alert.",
Action: command.AddRecipientAction,
}
return cmd
}
func addNoteCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be retrieved. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be retrieved. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "note",
Usage: "Note text",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "addNote",
Flags: flags,
Usage: "Adds a user comment for an alert.",
Action: command.AddNoteAction,
}
return cmd
}
func addTagsCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that the new tags will be added. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that the new tags will be added. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "tags",
Usage: "A comma separated list of labels attached to the alert.",
},
gcli.StringFlag{
Name: "note",
Usage: "Additional alert note",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "addTags",
Flags: flags,
Usage: "Adds tags to an alert.",
Action: command.AddTagsAction,
}
return cmd
}
func executeActionCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that the action will be executed on. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that the action will be executed on. Either id or alias must be provided. Alias option can only be used open alerts",
},
gcli.StringFlag{
Name: "action",
Usage: "Action to execute",
},
gcli.StringFlag{
Name: "note",
Usage: "Note text",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "executeAction",
Flags: flags,
Usage: "Executes alert actions at OpsGenie",
Action: command.ExecuteActionAction,
}
return cmd
}
func closeAlertCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId,id",
Usage: "Id of the alert that will be closed. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "alias",
Usage: "Alias of the alert that will be closed. Either id or alias must be provided",
},
gcli.StringFlag{
Name: "notify",
Usage: "Comma separated list of user and groups which will be notified. Also special values \"all\", \"recipients\" and \"owner\" is accepted",
},
gcli.StringFlag{
Name: "note",
Usage: "Note text",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "closeAlert",
Flags: flags,
Usage: "Closes an alert at OpsGenie",
Action: command.CloseAlertAction,
}
return cmd
}
func deleteAlertCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "alertId, id",
Usage: "Id of the alert that will be deleted",
},
gcli.StringFlag{
Name: "source",
Usage: "Source of the action",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "deleteAlert",
Flags: flags,
Usage: "Deletes an alert at OpsGenie.",
Action: command.DeleteAlertAction,
}
return cmd
}
func heartbeatCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "name",
Usage: "Name of the heartbeat on OpsGenie",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "heartbeat",
Flags: flags,
Usage: "Sends heartbeat to OpsGenie",
Action: command.HeartbeatAction,
}
return cmd
}
func enableCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "id",
Usage: "Id of the integration/policy that will be enabled. Either id or name must be provided",
},
gcli.StringFlag{
Name: "name",
Usage: "Name of the integration/policy that will be enabled. Either id or name must be provided",
},
gcli.StringFlag{
Name: "type",
Usage: "integration or policy",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "enable",
Flags: flags,
Usage: "Enables OpsGenie Integration and Policy.",
Action: command.EnableAction,
}
return cmd
}
func disableCommand() gcli.Command {
commandFlags := []gcli.Flag{
gcli.StringFlag{
Name: "id",
Usage: "Id of the integration/policy that will be disabled. Either id or name must be provided",
},
gcli.StringFlag{
Name: "name",
Usage: "Name of the integration/policy that will be disabled. Either id or name must be provided",
},
gcli.StringFlag{
Name: "type",
Usage: "integration or policy",
},
}
flags := append(commonFlags, commandFlags...)
cmd := gcli.Command{Name: "disable",
Flags: flags,
Usage: "Disables OpsGenie Integration and Policy.",
Action: command.DisableAction,
}
return cmd
}
func initCommands(app *gcli.App) {
app.Commands = []gcli.Command{
createAlertCommand(),
getAlertCommand(),
attachFileCommand(),
acknowledgeCommand(),
renotifyCommand(),
takeOwnershipCommand(),
assignOwnerCommand(),
addTeamCommand(),
addRecipientCommand(),
addTagsCommand(),
addNoteCommand(),
executeActionCommand(),
closeAlertCommand(),
deleteAlertCommand(),
heartbeatCommand(),
enableCommand(),
disableCommand(),
}
}
func main() {
app := gcli.NewApp()
app.Name = "lamp"
app.Version = lampVersion
app.Usage = "Command line interface for OpsGenie"
app.Author = "OpsGenie"
app.Action = func(c *gcli.Context) {
fmt.Printf("Run 'lamp help' for the options\n")
}
initCommands(app)
err := app.Run(os.Args)
if err != nil {
fmt.Printf("Error occured while executing command: %s\n", err.Error())
}
}