forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
incident.go
344 lines (303 loc) · 12 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
package pagerduty
import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
)
// Acknowledgement is the data structure of an acknowledgement of an incident.
type Acknowledgement struct {
At string `json:"at,omitempty"`
Acknowledger APIObject `json:"acknowledger,omitempty"`
}
// PendingAction is the data structure for any pending actions on an incident.
type PendingAction struct {
Type string `json:"type,omitempty"`
At string `json:"at,omitempty"`
}
// Assignment is the data structure for an assignment of an incident
type Assignment struct {
At string `json:"at,omitempty"`
Assignee APIObject `json:"assignee,omitempty"`
}
// AlertCounts is the data structure holding a summary of the number of alerts by status of an incident.
type AlertCounts struct {
Triggered uint `json:"triggered,omitempty"`
Resolved uint `json:"resolved,omitempty"`
All uint `json:"all,omitempty"`
}
// Priority is the data structure describing the priority of an incident.
type Priority struct {
APIObject
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
// Resolve reason is the data structure describing the reason an incident was resolved
type ResolveReason struct {
Type string `json:"type,omitempty"`
Incident APIObject `json:"incident"`
}
// IncidentBody is the datastructure containing data describing the incident.
type IncidentBody struct {
Type string `json:"type,omitempty"`
Details string `json:"details,omitempty"`
}
// Incident is a normalized, de-duplicated event generated by a PagerDuty integration.
type Incident struct {
APIObject
IncidentNumber uint `json:"incident_number,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
PendingActions []PendingAction `json:"pending_actions,omitempty"`
IncidentKey string `json:"incident_key,omitempty"`
Service APIObject `json:"service,omitempty"`
Assignments []Assignment `json:"assignments,omitempty"`
Acknowledgements []Acknowledgement `json:"acknowledgements,omitempty"`
LastStatusChangeAt string `json:"last_status_change_at,omitempty"`
LastStatusChangeBy APIObject `json:"last_status_change_by,omitempty"`
FirstTriggerLogEntry APIObject `json:"first_trigger_log_entry,omitempty"`
EscalationPolicy APIObject `json:"escalation_policy,omitempty"`
Teams []APIObject `json:"teams,omitempty"`
Priority *Priority `json:"priority,omitempty"`
Urgency string `json:"urgency,omitempty"`
Status string `json:"status,omitempty"`
Id string `json:"id,omitempty"`
ResolveReason ResolveReason `json:"resolve_reason,omitempty"`
AlertCounts AlertCounts `json:"alert_counts,omitempty"`
Body IncidentBody `json:"body,omitempty"`
IsMergeable bool `json:"is_mergeable,omitempty"`
}
// ListIncidentsResponse is the response structure when calling the ListIncident API endpoint.
type ListIncidentsResponse struct {
APIListObject
Incidents []Incident `json:"incidents,omitempty"`
}
// ListIncidentsOptions is the structure used when passing parameters to the ListIncident API endpoint.
type ListIncidentsOptions struct {
APIListObject
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
DateRange string `url:"date_range,omitempty"`
Statuses []string `url:"statuses,omitempty,brackets"`
IncidentKey string `url:"incident_key,omitempty"`
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
UserIDs []string `url:"user_ids,omitempty,brackets"`
Urgencies []string `url:"urgencies,omitempty,brackets"`
TimeZone string `url:"time_zone,omitempty"`
SortBy string `url:"sort_by,omitempty"`
Includes []string `url:"include,omitempty,brackets"`
}
// ListIncidents lists existing incidents.
func (c *Client) ListIncidents(o ListIncidentsOptions) (*ListIncidentsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/incidents?" + v.Encode())
if err != nil {
return nil, err
}
var result ListIncidentsResponse
return &result, c.decodeJSON(resp, &result)
}
// createIncidentResponse is returned from the API when creating a response.
type createIncidentResponse struct {
Incident Incident `json:"incident"`
}
// CreateIncidentOptions is the structure used when POSTing to the CreateIncident API endpoint.
type CreateIncidentOptions struct {
Type string `json:"type"`
Title string `json:"title"`
Service *APIReference `json:"service"`
Priority *APIReference `json:"priority,omitempty"`
IncidentKey string `json:"incident_key,omitempty"`
Body *APIDetails `json:"body,omitempty"`
EscalationPolicy *APIReference `json:"escalation_policy,omitempty"`
}
// CreateIncident creates an incident synchronously without a corresponding event from a monitoring service.
func (c *Client) CreateIncident(from string, o *CreateIncidentOptions) (*Incident, error) {
headers := make(map[string]string)
headers["From"] = from
data := make(map[string]*CreateIncidentOptions)
data["incident"] = o
resp, e := c.post("/incidents", data, &headers)
if e != nil {
return nil, e
}
var ii createIncidentResponse
e = json.NewDecoder(resp.Body).Decode(&ii)
if e != nil {
return nil, e
}
return &ii.Incident, nil
}
// ManageIncidents acknowledges, resolves, escalates, or reassigns one or more incidents.
func (c *Client) ManageIncidents(from string, incidents []Incident) error {
r := make(map[string][]Incident)
headers := make(map[string]string)
headers["From"] = from
r["incidents"] = incidents
_, e := c.put("/incidents", r, &headers)
return e
}
// MergeIncidents a list of source incidents into a specified incident.
func (c *Client) MergeIncidents(from string, id string, incidents []Incident) error {
r := make(map[string][]Incident)
r["source_incidents"] = incidents
headers := make(map[string]string)
headers["From"] = from
_, e := c.put("/incidents/"+id+"/merge", r, &headers)
return e
}
// GetIncident shows detailed information about an incident.
func (c *Client) GetIncident(id string) (*Incident, error) {
resp, err := c.get("/incidents/" + id)
if err != nil {
return nil, err
}
var result map[string]Incident
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
i, ok := result["incident"]
if !ok {
return nil, fmt.Errorf("JSON response does not have incident field")
}
return &i, nil
}
// IncidentNote is a note for the specified incident.
type IncidentNote struct {
ID string `json:"id,omitempty"`
User APIObject `json:"user,omitempty"`
Content string `json:"content,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
// ListIncidentNotes lists existing notes for the specified incident.
func (c *Client) ListIncidentNotes(id string) ([]IncidentNote, error) {
resp, err := c.get("/incidents/" + id + "/notes")
if err != nil {
return nil, err
}
var result map[string][]IncidentNote
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
notes, ok := result["notes"]
if !ok {
return nil, fmt.Errorf("JSON response does not have notes field")
}
return notes, nil
}
// IncidentAlert is a alert for the specified incident.
type IncidentAlert struct {
APIObject
CreatedAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
AlertKey string `json:"alert_key,omitempty"`
Service APIObject `json:"service,omitempty"`
Body map[string]interface{} `json:"body,omitempty"`
Incident APIReference `json:"incident,omitempty"`
Suppressed bool `json:"suppressed,omitempty"`
Severity string `json:"severity,omitempty"`
Integration APIObject `json:"integration,omitempty"`
}
// ListAlertsResponse is the response structure when calling the ListAlert API endpoint.
type ListAlertsResponse struct {
APIListObject
Alerts []IncidentAlert `json:"alerts,omitempty"`
}
// ListIncidentAlerts lists existing alerts for the specified incident.
func (c *Client) ListIncidentAlerts(id string) (*ListAlertsResponse, error) {
resp, err := c.get("/incidents/" + id + "/alerts")
if err != nil {
return nil, err
}
var result ListAlertsResponse
return &result, c.decodeJSON(resp, &result)
}
// CreateIncidentNote creates a new note for the specified incident.
func (c *Client) CreateIncidentNote(id string, note IncidentNote) error {
data := make(map[string]IncidentNote)
headers := make(map[string]string)
headers["From"] = note.User.Summary
data["note"] = note
_, err := c.post("/incidents/"+id+"/notes", data, &headers)
return err
}
// SnoozeIncident sets an incident to not alert for a specified period of time.
func (c *Client) SnoozeIncident(id string, duration uint) error {
data := make(map[string]uint)
data["duration"] = duration
_, err := c.post("/incidents/"+id+"/snooze", data, nil)
return err
}
// ListIncidentLogEntriesResponse is the response structure when calling the ListIncidentLogEntries API endpoint.
type ListIncidentLogEntriesResponse struct {
APIListObject
LogEntries []LogEntry `json:"log_entries,omitempty"`
}
// ListIncidentLogEntriesOptions is the structure used when passing parameters to the ListIncidentLogEntries API endpoint.
type ListIncidentLogEntriesOptions struct {
APIListObject
Includes []string `url:"include,omitempty,brackets"`
IsOverview bool `url:"is_overview,omitempty"`
TimeZone string `url:"time_zone,omitempty"`
}
// ListIncidentLogEntries lists existing log entries for the specified incident.
func (c *Client) ListIncidentLogEntries(id string, o ListIncidentLogEntriesOptions) (*ListIncidentLogEntriesResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/incidents/" + id + "/log_entries?" + v.Encode())
if err != nil {
return nil, err
}
var result ListIncidentLogEntriesResponse
return &result, c.decodeJSON(resp, &result)
}
// Alert is a list of all of the alerts that happened to an incident.
type Alert struct {
APIObject
Service APIObject `json:"service,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
AlertKey string `json:"alert_key,omitempty"`
Incident APIObject `json:"incident,omitempty"`
}
type ListAlertResponse struct {
APIListObject
Alerts []Alert `json:"alerts,omitempty"`
}
// UpdateIncident is not apart of the go-pagerduty library. Therefore, we need to add it manually.
// https://developer.pagerduty.com/api-reference/8a0e1aa2ec666-update-an-incident
// UpdateIncidentResponse is the response structure when calling the UpdateIncident API endpoint.
type UpdateIncidentResponse struct {
Incident Incident `json:"incident,omitempty"`
}
// UpdateIncidentOptions is the structure used when passing parameters to the UpdateIncident API endpoint.
type UpdateIncidentOptions struct {
Type string `json:"type,omitempty"`
Urgency string `json:"urgency,omitempty"`
Status string `json:"status,omitempty"`
Resolution string `json:"resolution,omitempty,brackets"`
EscalationLevel int `json:"escalation_level,omitempty"`
}
// UpdateIncident updates an existing incident.
func (c *Client) UpdateIncident(id string, from string, o UpdateIncidentOptions) (*UpdateIncidentResponse, error) {
data := make(map[string]UpdateIncidentOptions)
data["incident"] = o
headers := make(map[string]string)
headers["From"] = from
resp, err := c.put("/incidents/"+id, data, &headers)
if err != nil {
return nil, err
}
var result UpdateIncidentResponse
e := json.NewDecoder(resp.Body).Decode(&result)
if e != nil {
return nil, e
}
return &result, nil
}