-
Notifications
You must be signed in to change notification settings - Fork 1
/
incident.go
458 lines (385 loc) · 12.2 KB
/
incident.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
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Incident definition https://api.ilert.com/api-docs/#tag/Incidents
type Incident struct {
ID int64 `json:"id"`
Summary string `json:"summary"`
Status string `json:"status"`
Message string `json:"message"`
SendNotification bool `json:"sendNotification"`
CreatedAt string `json:"createdAt"` // Date time string in ISO format
UpdatedAt string `json:"updatedAt"` // Date time string in ISO format
AffectedServices []AffectedServices `json:"affectedServices"`
ResolvedOn string `json:"resolvedOn,omitempty"` // Date time string in ISO format
Subscribed bool `json:"subscribed,omitempty"`
AffectedTeams []TeamShort `json:"affectedTeams,omitempty"`
}
// AffectedServices defines affected services
type AffectedServices struct {
Impact string `json:"impact"`
Service Service `json:"service"`
}
// Subscriber defines a subscriber
type Subscriber struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
// UIMenuItem defines the ui menu item
type UIMenuItem struct {
ID int64 `json:"id"`
Label string `json:"label"`
}
// Affected defines affected entities on incident
type Affected struct {
StatusPagesInfo UIMenuItem `json:"statusPagesInfo"`
PrivateStatusPages int64 `json:"privateStatusPages"`
PublicStatusPages int64 `json:"publicStatusPages"`
PrivateSubscribers int64 `json:"privateSubscribers"`
PublicSubscribers int64 `json:"publicSubscribers"`
}
// IncidentInclude defines incident includes
var IncidentInclude = struct {
Subscribed string
AffectedTeams string
History string
}{
Subscribed: "subscribed",
AffectedTeams: "affectedTeams",
History: "history",
}
// IncidentIncludeAll defines incident includes list
var IncidentIncludeAll = []string{
IncidentInclude.Subscribed,
IncidentInclude.AffectedTeams,
IncidentInclude.History,
}
// IncidentType defines incident type
var IncidentType = struct {
User string
Team string
}{
User: "USER",
Team: "TEAM",
}
// IncidentTypeAll defines incident type list
var IncidentTypeAll = []string{
IncidentType.User,
IncidentType.Team,
}
// IncidentStatus defines incident status
var IncidentStatus = struct {
Investigating string
Identified string
Monitoring string
Resolved string
}{
Investigating: "INVESTIGATING",
Identified: "IDENTIFIED",
Monitoring: "MONITORING",
Resolved: "RESOLVED",
}
// IncidentStatusAll defines incident status list
var IncidentStatusAll = []string{
IncidentStatus.Investigating,
IncidentStatus.Identified,
IncidentStatus.Monitoring,
IncidentStatus.Resolved,
}
// CreateIncidentInput represents the input of a CreateIncident operation.
type CreateIncidentInput struct {
_ struct{}
Incident *Incident
}
// CreateIncidentOutput represents the output of a CreateIncident operation.
type CreateIncidentOutput struct {
_ struct{}
Incident *Incident
}
// CreateIncident creates a new incident. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents/post
func (c *Client) CreateIncident(input *CreateIncidentInput) (*CreateIncidentOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Incident == nil {
return nil, errors.New("incident input is required")
}
resp, err := c.httpClient.R().SetBody(input.Incident).Post(apiRoutes.incidents)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
incident := &Incident{}
err = json.Unmarshal(resp.Body(), incident)
if err != nil {
return nil, err
}
return &CreateIncidentOutput{Incident: incident}, nil
}
// GetIncidentsInput represents the input of a GetIncidents operation.
type GetIncidentsInput 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: 10, Maximum: 25 or 100 without include
MaxResults *int
// describes optional properties that should be included in the response
Include []*string
// state of the incident
States []*string
// service IDs of the incident's affected services
Services []*int64
// Date time string in ISO format
From *string
// Date time string in ISO format
Until *string
}
// GetIncidentsOutput represents the output of a GetIncidents operation.
type GetIncidentsOutput struct {
_ struct{}
Incidents []*Incident
}
// GetIncidents lists existing incidents. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents/get
func (c *Client) GetIncidents(input *GetIncidentsInput) (*GetIncidentsOutput, error) {
if input == nil {
input = &GetIncidentsInput{}
}
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", "10")
}
for _, include := range input.Include {
q.Add("include", *include)
}
for _, state := range input.States {
q.Add("state", *state)
}
for _, services := range input.Services {
q.Add("services", strconv.FormatInt(*services, 10))
}
if input.From != nil {
q.Add("from", *input.From)
}
if input.Until != nil {
q.Add("until", *input.Until)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.incidents, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
incidents := make([]*Incident, 0)
err = json.Unmarshal(resp.Body(), &incidents)
if err != nil {
return nil, err
}
return &GetIncidentsOutput{Incidents: incidents}, nil
}
// GetIncidentInput represents the input of a GetIncident operation.
type GetIncidentInput struct {
_ struct{}
IncidentID *int64
// describes optional properties that should be included in the response
Include []*string
}
// GetIncidentOutput represents the output of a GetIncident operation.
type GetIncidentOutput struct {
_ struct{}
Incident *Incident
ETag *string
}
// GetIncident gets an incident by id. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents~1{id}/get
func (c *Client) GetIncident(input *GetIncidentInput) (*GetIncidentOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.IncidentID == nil {
return nil, errors.New("incident id is required")
}
q := url.Values{}
for _, include := range input.Include {
q.Add("include", *include)
}
var url = fmt.Sprintf("%s/%d?%s", apiRoutes.incidents, *input.IncidentID, q.Encode())
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
incident := &Incident{}
err = json.Unmarshal(resp.Body(), incident)
if err != nil {
return nil, err
}
output := &GetIncidentOutput{Incident: incident}
etag := resp.Header().Get("ETag")
if etag != "" {
output.ETag = String(etag)
}
return output, nil
}
// GetIncidentSubscribersInput represents the input of a GetIncidentSubscribers operation.
type GetIncidentSubscribersInput struct {
_ struct{}
IncidentID *int64
}
// GetIncidentSubscribersOutput represents the output of a GetIncidentSubscribers operation.
type GetIncidentSubscribersOutput struct {
_ struct{}
Subscribers []*Subscriber
}
// GetIncidentSubscribers gets subscribers of an incident by id. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents~1{id}~1private-subscribers/get
func (c *Client) GetIncidentSubscribers(input *GetIncidentSubscribersInput) (*GetIncidentSubscribersOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.IncidentID == nil {
return nil, errors.New("incident id is required")
}
var url = fmt.Sprintf("%s/%d/private-subscribers", apiRoutes.incidents, *input.IncidentID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
subscribers := make([]*Subscriber, 0)
err = json.Unmarshal(resp.Body(), &subscribers)
if err != nil {
return nil, err
}
return &GetIncidentSubscribersOutput{Subscribers: subscribers}, nil
}
// GetIncidentAffectedInput represents the input of a GetIncidentAffected operation.
type GetIncidentAffectedInput struct {
_ struct{}
Incident *Incident
}
// GetIncidentAffectedOutput represents the output of a GetIncidentAffected operation.
type GetIncidentAffectedOutput struct {
_ struct{}
Affected *Affected
}
// GetIncidentAffected forecasts the affected subscribers and status pages. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents~1publish-info/post
func (c *Client) GetIncidentAffected(input *GetIncidentAffectedInput) (*GetIncidentAffectedOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Incident == nil {
return nil, errors.New("incident is required")
}
url := fmt.Sprintf("%s/publish-info", apiRoutes.incidents)
resp, err := c.httpClient.R().SetBody(input.Incident).Post(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
affected := &Affected{}
err = json.Unmarshal(resp.Body(), affected)
if err != nil {
return nil, err
}
return &GetIncidentAffectedOutput{Affected: affected}, nil
}
// AddIncidentSubscribersInput represents the input of a AddIncidentSubscribers operation.
type AddIncidentSubscribersInput struct {
_ struct{}
IncidentID *int64
Subscribers *[]Subscriber
}
// AddIncidentSubscribersOutput represents the output of a AddIncidentSubscribers operation.
type AddIncidentSubscribersOutput struct {
_ struct{}
}
// AddIncidentSubscribers adds a new subscriber to an incident. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents~1{id}~1private-subscribers/post
func (c *Client) AddIncidentSubscribers(input *AddIncidentSubscribersInput) (*AddIncidentSubscribersOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.IncidentID == nil {
return nil, errors.New("incident id is required")
}
if input.Subscribers == nil {
return nil, errors.New("subscriber input is required")
}
url := fmt.Sprintf("%s/%d/private-subscribers", apiRoutes.incidents, *input.IncidentID)
resp, err := c.httpClient.R().SetBody(input.Subscribers).Post(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 202); apiErr != nil {
return nil, apiErr
}
subscribers := make([]*Subscriber, 0)
err = json.Unmarshal(resp.Body(), &subscribers)
if err != nil {
return nil, err
}
return &AddIncidentSubscribersOutput{}, nil
}
// UpdateIncidentInput represents the input of a UpdateIncident operation.
type UpdateIncidentInput struct {
_ struct{}
IncidentID *int64
Incident *Incident
ETag *string
}
// UpdateIncidentOutput represents the output of a UpdateIncident operation.
type UpdateIncidentOutput struct {
_ struct{}
Incident *Incident
}
// UpdateIncident updates the specific incident. https://api.ilert.com/api-docs/#tag/Incidents/paths/~1incidents~1{id}/put
func (c *Client) UpdateIncident(input *UpdateIncidentInput) (*UpdateIncidentOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.IncidentID == nil {
return nil, errors.New("incident id is required")
}
if input.Incident == nil {
return nil, errors.New("incident input is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.incidents, *input.IncidentID)
req := c.httpClient.R()
if input.ETag != nil && *input.ETag != "" {
req.SetHeader("If-Match", *input.ETag)
}
resp, err := req.SetBody(input.Incident).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
incident := &Incident{}
err = json.Unmarshal(resp.Body(), incident)
if err != nil {
return nil, err
}
return &UpdateIncidentOutput{Incident: incident}, nil
}