-
Notifications
You must be signed in to change notification settings - Fork 1
/
alert.go
691 lines (586 loc) · 19 KB
/
alert.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Alert definition
type Alert struct {
ID int64 `json:"id"`
Summary string `json:"summary"`
Details string `json:"details"`
ReportTime string `json:"reportTime"` // Date time string in ISO format
ResolvedOn string `json:"resolvedOn"` // Date time string in ISO format
Status string `json:"status"`
AlertSource *AlertSource `json:"alertSource,omitempty"`
EscalationPolicy *EscalationPolicy `json:"scalationPolicy,omitempty"`
Priority string `json:"priority"`
AlertKey string `json:"alertKey"`
AssignedTo *User `json:"assignedTo,omitempty"`
NextEscalation string `json:"nextEscalation"` // Date time string in ISO format
CallRoutingNumber *CallRoutingNumber `json:"callRoutingNumber,omitempty"`
AcknowledgedBy *User `json:"acknowledgedBy,omitempty"`
AcknowledgedByType string `json:"acknowledgedByType,omitempty"`
ResolvedBy *User `json:"resolvedBy,omitempty"`
ResolvedByType string `json:"resolvedByType,omitempty"`
Images []AlertImage `json:"images,omitempty"`
Links []AlertLink `json:"links,omitempty"`
CustomDetails map[string]interface{} `json:"customDetails,omitempty"`
}
// AlertImage represents event image
type AlertImage struct {
Src string `json:"src"`
Href string `json:"href"`
Alt string `json:"alt"`
}
// AlertLink represents event link
type AlertLink struct {
Text string `json:"text"`
Href string `json:"href"`
}
// AlertComment definition
type AlertComment struct {
ID string `json:"id"`
Content string `json:"content"`
Creator *User `json:"creator"`
TriggerType string `json:"triggerType"`
ResolveComment bool `json:"resolveComment"`
Created string `json:"created"`
Updated string `json:"updated"`
}
// CallRoutingNumber definition
type CallRoutingNumber struct {
ID int `json:"id"`
Number Phone `json:"number"`
VoiceLanguageLocale string `json:"voiceLanguageLocale"`
AlertSource *AlertSource `json:"alertSource"`
}
// AlertStatuses defines alert statuses
var AlertStatuses = struct {
New string
Pending string
Accepted string
Resolved string
}{
New: "NEW",
Pending: "PENDING",
Accepted: "ACCEPTED",
Resolved: "RESOLVED",
}
// AlertPriorities defines alert priorities
var AlertPriorities = struct {
High string
Low string
}{
High: "HIGH",
Low: "LOW",
}
// AlertPrioritiesAll defines alert priorities list
var AlertPrioritiesAll = []string{
AlertPriorities.High,
AlertPriorities.Low,
}
// AlertResponderTypes defines alert responder types
var AlertResponderTypes = struct {
User string
AlertSource string
}{
User: "USER",
AlertSource: "SOURCE",
}
// AlertResponder definition
type AlertResponder struct {
ID int64 `json:"id"`
Name string `json:"name"`
Group string `json:"group"`
Disabled bool `json:"disabled"`
}
// AlertResponderGroups defines alert responder groups
var AlertResponderGroups = struct {
Suggested string
User string
EscalationPolicy string
OnCallSchedule string
}{
Suggested: "SUGGESTED",
User: "USER",
EscalationPolicy: "ESCALATION_POLICY",
OnCallSchedule: "ON_CALL_SCHEDULE",
}
// AlertLogEntry definition
type AlertLogEntry struct {
ID int64 `json:"id"`
Timestamp string `json:"timestamp"` // Date time string in ISO format
LogEntryType string `json:"logEntryType"`
Text string `json:"text"`
AlertID int64 `json:"alertId"`
}
// AlertLogEntryTypes defines alert log entry types
var AlertLogEntryTypes = struct {
AlertReceivedLogEntry string
AlertSourceResponseLogEntry string
EmailReceivedLogEntry string
AlertAssignedBySystemLogEntry string
AlertAssignedByUserLogEntry string
AlertCreatedByUserLogEntry string
NotificationLogEntry string
UserResponseLogEntry string
}{
AlertReceivedLogEntry: "AlertReceivedLogEntry",
AlertSourceResponseLogEntry: "AlertSourceResponseLogEntry",
EmailReceivedLogEntry: "EmailReceivedLogEntry",
AlertAssignedBySystemLogEntry: "AlertAssignedBySystemLogEntry",
AlertAssignedByUserLogEntry: "AlertAssignedByUserLogEntry",
AlertCreatedByUserLogEntry: "AlertCreatedByUserLogEntry",
NotificationLogEntry: "NotificationLogEntry",
UserResponseLogEntry: "UserResponseLogEntry",
}
var AlertInclude = struct {
EscalationRules string
NextEscalationUser string
}{
EscalationRules: "escalationRules",
NextEscalationUser: "nextEscalationUser",
}
// GetAlertInput represents the input of a GetAlert operation.
type GetAlertInput struct {
_ struct{}
AlertID *int64
// describes optional properties that should be included in the response
Include []*string
}
// GetAlertOutput represents the output of a GetAlert operation.
type GetAlertOutput struct {
_ struct{}
Alert *Alert
}
// GetAlert gets the alert with specified id. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}/get
func (c *Client) GetAlert(input *GetAlertInput) (*GetAlertOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
q := url.Values{}
for _, include := range input.Include {
q.Add("include", *include)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d?%s", apiRoutes.alerts, *input.AlertID, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alert := &Alert{}
err = json.Unmarshal(resp.Body(), alert)
if err != nil {
return nil, err
}
return &GetAlertOutput{Alert: alert}, nil
}
// GetAlertsInput represents the input of a GetAlerts operation.
type GetAlertsInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
// Default: 0
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Default: 50, Maximum: 100
MaxResults *int
// state of the alert
States []*string
// alert source IDs of the alert's alert source
AlertSources []*int64
// user IDs of the user that the alert is assigned to
AssignedToUserIDs []*int64
// usernames of the user that the alert is assigned to
AssignedToUserNames []*string
// Date time string in ISO format
From *string
// Date time string in ISO format
Until *string
}
// GetAlertsOutput represents the output of a GetAlerts operation.
type GetAlertsOutput struct {
_ struct{}
Alerts []*Alert
}
// GetAlerts lists existing alerts. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts/get
func (c *Client) GetAlerts(input *GetAlertsInput) (*GetAlertsOutput, error) {
if input == nil {
input = &GetAlertsInput{}
}
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
} else {
q.Add("start-index", "0")
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
} else {
q.Add("max-results", "50")
}
if input.From != nil {
q.Add("from", *input.From)
}
if input.Until != nil {
q.Add("until", *input.Until)
}
for _, state := range input.States {
q.Add("state", *state)
}
for _, alertSourceID := range input.AlertSources {
q.Add("alert-source", strconv.FormatInt(*alertSourceID, 10))
}
for _, userID := range input.AssignedToUserIDs {
q.Add("assigned-to", strconv.FormatInt(*userID, 10))
}
for _, username := range input.AssignedToUserNames {
q.Add("assigned-to", *username)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.alerts, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alerts := make([]*Alert, 0)
err = json.Unmarshal(resp.Body(), &alerts)
if err != nil {
return nil, err
}
return &GetAlertsOutput{Alerts: alerts}, nil
}
// GetAlertsCountInput represents the input of a GetAlertsCount operation.
type GetAlertsCountInput struct {
_ struct{}
// state of the alert
States []*string
// alert source ids of the alert's alert source
AlertSources []*int64
// user ids of the user that the alert is assigned to
AssignedToUserIDs []*int64
// usernames of the user that the alert is assigned to
AssignedToUserNames []*string
// Date time string in ISO format
From *string
// Date time string in ISO format
Until *string
}
// GetAlertsCountOutput represents the output of a GetAlertsCount operation.
type GetAlertsCountOutput struct {
_ struct{}
Count int
}
// GetAlertsCount gets the alert count. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1count/get
func (c *Client) GetAlertsCount(input *GetAlertsCountInput) (*GetAlertsCountOutput, error) {
if input == nil {
input = &GetAlertsCountInput{}
}
q := url.Values{}
if input.From != nil {
q.Add("from", *input.From)
}
if input.Until != nil {
q.Add("until", *input.From)
}
for _, state := range input.States {
q.Add("state", *state)
}
for _, alertSourceID := range input.AlertSources {
q.Add("alert-source", strconv.FormatInt(*alertSourceID, 10))
}
for _, userID := range input.AssignedToUserIDs {
q.Add("assigned-to", strconv.FormatInt(*userID, 10))
}
for _, username := range input.AssignedToUserNames {
q.Add("assigned-to", *username)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/count?%s", apiRoutes.alerts, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
body := &GenericCountResponse{}
err = json.Unmarshal(resp.Body(), body)
if err != nil {
return nil, err
}
return &GetAlertsCountOutput{Count: body.Count}, nil
}
// GetAlertResponderInput represents the input of a GetAlertResponder operation.
type GetAlertResponderInput struct {
_ struct{}
AlertID *int64
Language *string
}
// GetAlertResponderOutput represents the output of a GetAlertResponder operation.
type GetAlertResponderOutput struct {
_ struct{}
Responders []*AlertResponder
}
// GetAlertResponder gets the responders on the alert with specified id. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}~1suggested-responders/get
func (c *Client) GetAlertResponder(input *GetAlertResponderInput) (*GetAlertResponderOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
q := url.Values{}
if input.Language != nil {
if *input.Language == "en" {
q.Add("lng", "en")
} else if *input.Language == "de" {
q.Add("lng", "de")
}
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/suggested-responders", apiRoutes.alerts, *input.AlertID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alertResponders := make([]*AlertResponder, 0)
err = json.Unmarshal(resp.Body(), &alertResponders)
if err != nil {
return nil, err
}
return &GetAlertResponderOutput{Responders: alertResponders}, nil
}
// AssignAlertInput represents the input of a AssignAlert operation.
type AssignAlertInput struct {
_ struct{}
AlertID *int64
UserID *int64
Username *string
EscalationPolicyID *int64
ScheduleID *int64
}
// AssignAlertOutput represents the output of a AssignAlert operation.
type AssignAlertOutput struct {
_ struct{}
Alert *Alert
}
// AssignAlert assigns an alert with specified id to specified entities. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}~1assign/put
func (c *Client) AssignAlert(input *AssignAlertInput) (*AssignAlertOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
if input.UserID == nil && input.Username == nil && input.EscalationPolicyID == nil && input.ScheduleID == nil {
return nil, errors.New("one of assignments is required")
}
q := url.Values{}
if input.UserID != nil {
q.Add("user-id", strconv.FormatInt(*input.UserID, 10))
}
if input.Username != nil {
q.Add("user-id", *input.Username)
}
if input.EscalationPolicyID != nil {
q.Add("policy-id", strconv.FormatInt(*input.EscalationPolicyID, 10))
}
if input.ScheduleID != nil {
q.Add("schedule-id", strconv.FormatInt(*input.ScheduleID, 10))
}
resp, err := c.httpClient.R().Put(fmt.Sprintf("%s/%d/assign?%s", apiRoutes.alerts, *input.AlertID, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alert := &Alert{}
err = json.Unmarshal(resp.Body(), alert)
if err != nil {
return nil, err
}
return &AssignAlertOutput{Alert: alert}, nil
}
// AcceptAlertInput represents the input of a AcceptAlert operation.
type AcceptAlertInput struct {
_ struct{}
AlertID *int64
}
// AcceptAlertOutput represents the output of a AcceptAlert operation.
type AcceptAlertOutput struct {
_ struct{}
Alert *Alert
}
// AcceptAlert accepts an alert with specified id. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}~1accept/put
func (c *Client) AcceptAlert(input *AcceptAlertInput) (*AcceptAlertOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
resp, err := c.httpClient.R().Put(fmt.Sprintf("%s/%d/accept", apiRoutes.alerts, *input.AlertID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alert := &Alert{}
err = json.Unmarshal(resp.Body(), alert)
if err != nil {
return nil, err
}
return &AcceptAlertOutput{Alert: alert}, nil
}
// ResolveAlertInput represents the input of a ResolveAlert operation.
type ResolveAlertInput struct {
_ struct{}
AlertID *int64
}
// ResolveAlertOutput represents the output of a ResolveAlert operation.
type ResolveAlertOutput struct {
_ struct{}
Alert *Alert
}
// ResolveAlert resolves an alert with specified id. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}~1resolve/put
func (c *Client) ResolveAlert(input *ResolveAlertInput) (*ResolveAlertOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
resp, err := c.httpClient.R().Put(fmt.Sprintf("%s/%d/resolve", apiRoutes.alerts, *input.AlertID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alert := &Alert{}
err = json.Unmarshal(resp.Body(), alert)
if err != nil {
return nil, err
}
return &ResolveAlertOutput{Alert: alert}, nil
}
// GetAlertLogEntriesInput represents the input of a GetAlertLogEntries operation.
type GetAlertLogEntriesInput struct {
_ struct{}
AlertID *int64
Language *string
}
// GetAlertLogEntriesOutput represents the output of a GetAlertLogEntries operation.
type GetAlertLogEntriesOutput struct {
_ struct{}
LogEntries []*AlertLogEntry
}
// GetAlertLogEntries gets log entries for the specified alert. https://api.ilert.com/api-docs/#tag/Alerts/paths/~1alerts~1{id}~1log-entries/get
func (c *Client) GetAlertLogEntries(input *GetAlertLogEntriesInput) (*GetAlertLogEntriesOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.AlertID == nil {
return nil, errors.New("alert id is required")
}
q := url.Values{}
if input.Language != nil {
if *input.Language == "en" {
q.Add("lng", "en")
} else if *input.Language == "de" {
q.Add("lng", "de")
}
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/log-entries", apiRoutes.alerts, *input.AlertID))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
alertLogEntries := make([]*AlertLogEntry, 0)
err = json.Unmarshal(resp.Body(), &alertLogEntries)
if err != nil {
return nil, err
}
return &GetAlertLogEntriesOutput{LogEntries: alertLogEntries}, nil
}
// TODO https://api.ilert.com/api-docs/#tag/Alert-Actions/paths/~1alerts~1{id}~1actions/get
// // GetAlertActionsInput represents the input of a GetAlertsAction operation.
// type GetAlertActionsInput struct {
// _ struct{}
// AlertID *int64
// }
// // GetAlertActionsOutput represents the output of a GetAlertsAction operation.
// type GetAlertActionsOutput struct {
// _ struct{}
// Actions []*AlertAction
// }
// // GetAlertActions gets available actions for specified alert. https://api.ilert.com/api-docs/#tag/Alert-Actions/paths/~1alerts~1{id}~1actions/get
// func (c *Client) GetAlertActions(input *GetAlertActionsInput) (*GetAlertActionsOutput, error) {
// if input == nil {
// return nil, errors.New("input is required")
// }
// if input.AlertID == nil {
// return nil, errors.New("Alert id is required")
// }
// resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%d/actions", apiRoutes.alerts, *input.AlertID))
// if err != nil {
// return nil, err
// }
// if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
// return nil, apiErr
// }
// alertActions := make([]*AlertAction, 0)
// err = json.Unmarshal(resp.Body(), &alertActions)
// if err != nil {
// return nil, err
// }
// return &GetAlertActionsOutput{Actions: alertActions}, nil
// }
// TODO https://api.ilert.com/api-docs/#tag/Alert-Actions/paths/~1alerts~1{id}~1actions/post
// // InvokeAlertActionInput represents the input of a InvokeAlertAction operation.
// type InvokeAlertActionInput struct {
// _ struct{}
// AlertID *int64
// Action *AlertAction
// }
// // InvokeAlertActionOutput represents the output of a InvokeAlertAction operation.
// type InvokeAlertActionOutput struct {
// _ struct{}
// Action *AlertAction
// }
// // InvokeAlertAction invokes a specific alert action. https://api.ilert.com/api-docs/#tag/Alert-Actions/paths/~1alerts~1{id}~1actions/post
// func (c *Client) InvokeAlertAction(input *InvokeAlertActionInput) (*InvokeAlertActionOutput, error) {
// if input == nil {
// return nil, errors.New("input is required")
// }
// if input.AlertID == nil {
// return nil, errors.New("Alert id is required")
// }
// if input.Action == nil {
// return nil, errors.New("action input is required")
// }
// resp, err := c.httpClient.R().SetBody(input.Action).Post(fmt.Sprintf("%s/%d/actions", apiRoutes.alerts, *input.AlertID))
// if err != nil {
// return nil, err
// }
// if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
// return nil, apiErr
// }
// alertAction := &AlertAction{}
// err = json.Unmarshal(resp.Body(), alertAction)
// if err != nil {
// return nil, err
// }
// return &InvokeAlertActionOutput{Action: alertAction}, nil
// }