forked from chiefy/go-statuspage-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincident.go
More file actions
252 lines (231 loc) · 8.52 KB
/
Copy pathincident.go
File metadata and controls
252 lines (231 loc) · 8.52 KB
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
package statuspage
import (
"fmt"
"time"
)
type IncidentUpdate struct {
Body *string `json:"body,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
DisplayAt *time.Time `json:"display_at,omitempty"`
ID *string `json:"id,omitempty"`
IncidentID *string `json:"incident_id,omitempty"`
Status *string `json:"status,omitempty"`
TwitterUpdatedAt *time.Time `json:"twitter_updated_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
WantsTwitterUpdate *bool `json:"wants_twitter-update,omitempty"`
}
type Incident struct {
Backfilled *bool `json:"backfilled,omitempty"`
Components *[]Component `json:"components,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
ID *string `json:"id,omitempty"`
Impact *string `json:"impact,omitempty"`
ImpactOverride *string `json:"impact_override,omitempty"`
IncidentUpdates []*IncidentUpdate `json:"incident_updates,omitempty"`
MonitoringAt *time.Time `json:"monitoring_at,omitempty"`
Name *string `json:"name,omitempty"`
PageID *string `json:"page_id,omitempty"`
PostmortemBody *string `json:"postmortem_body,omitempty"`
PostmortemBodyLastUpdatedAt *time.Time `json:"postmortem_body_last_updated_at,omitempty"`
PostmortemIgnored *bool `json:"postmortem_ignored,omitempty"`
PostmortemNotifiedSubscribers *bool `json:"postmortem_notified_subscribers,omitempty"`
PostmortemNotifiedTwitter *bool `json:"postmortem_notified_twitter,omitempty"`
PostmortemPublishedAt *time.Time `json:"postmorem_published_at,omitempty"`
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
ScheduledAutoInProgress *bool `json:"scheduled_auto_in_progress,omitempty"`
ScheduledAutoCompleted *bool `json:"scheduled_auto_completed,omitempty"`
ScheduledFor *time.Time `json:"scheduled_for"`
ScheduledRemindPrior *bool `json:"scheduled_remind_prior,omitempty"`
ScheduledRemindedAt *time.Time `json:"scheduled_reminded_at,omitempty"`
ScheduledUntil *time.Time `json:"scheduled_until,omitempty"`
Shortlink *string `json:"shortlink,omitempty"`
Status *string `json:"status,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type IncidentResponse []Incident
type NewIncident struct {
Name string
Status string
Message string
WantsTwitterUpdate bool
ImpactOverride string
ComponentIDs []string
}
func (i *NewIncident) String() string {
return encodeParams(map[string]interface{}{
"incident[name]": i.Name,
"incident[status]": i.Status,
"incident[wants_twitter_update]": i.WantsTwitterUpdate,
"incident[message]": i.Message,
"incident[impact_override]": i.ImpactOverride,
"incident[component_ids][]": i.ComponentIDs,
})
}
type ScheduledIncident struct {
Name string
Status string
ScheduledFor time.Time
ScheduledUntil time.Time
WantsTwitterUpdate bool
ScheduledRemindPrior bool
ScheduledAutoInProgress bool
ScheduledAutoCompleted bool
ImpactOverride string
Message string
ComponentIDs []string
}
func (i *ScheduledIncident) String() string {
return encodeParams(map[string]interface{}{
"incident[name]": i.Name,
"incident[status]": i.Status,
"incident[scheduled_for]": i.ScheduledFor,
"incident[scheduled_until]": i.ScheduledUntil,
"incident[wants_twitter_update]": i.WantsTwitterUpdate,
"incident[scheduled_remind_prior]": i.ScheduledRemindPrior,
"incident[scheduled_auto_in_progress]": i.ScheduledAutoInProgress,
"incident[scheduled_auto_completed]": i.ScheduledAutoCompleted,
"incident[message]": i.Message,
"incident[component_ids][]": i.ComponentIDs,
})
}
type HistoricIncident struct {
Name string
Backfilled bool
BackfillDate string
Message string
}
func (i *HistoricIncident) String() string {
return encodeParams(map[string]interface{}{
"incident[name]": i.Name,
"incident[backfilled]": i.Backfilled,
"incident[backfill_date]": i.BackfillDate,
"incident[message]": i.Message,
})
}
type NewIncidentUpdate struct {
Name string
Status string
Message string
WantsTwitterUpdate bool
ImpactOverride string
ComponentIDs []string
}
func (i *NewIncidentUpdate) String() string {
return encodeParams(map[string]interface{}{
"incident[name]": i.Name,
"incident[status]": i.Status,
"incident[message]": i.Message,
"incident[wants_twitter_update]": i.WantsTwitterUpdate,
"incident[impact_override]": i.ImpactOverride,
"incident[component_ids]": i.ComponentIDs,
})
}
// TODO: Paging
func (c *Client) doGetIncidents(path string) ([]Incident, error) {
resp := IncidentResponse{}
err := c.doGet(path, nil, &resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) GetAllIncidents() ([]Incident, error) {
return c.doGetIncidents("incidents.json")
}
func (c *Client) GetOpenIncidents() ([]Incident, error) {
return c.doGetIncidents("incidents/unresolved.json")
}
func (c *Client) GetScheduledIncidents() ([]Incident, error) {
return c.doGetIncidents("incidents/scheduled.json")
}
func (c *Client) CreateIncident(component, name, message, status string) (*Incident, error) {
switch status {
case "investigating", "identified", "monitoring", "resolved":
break
default:
return nil, fmt.Errorf("create error: status not (investigating|identified|monitoring|resolved), got %s", status)
}
cp, err := c.GetComponentByName(component)
if err != nil {
return nil, err
}
i := &NewIncident{
Name: name,
Status: status,
Message: message,
WantsTwitterUpdate: false,
ImpactOverride: "none",
ComponentIDs: []string{*cp.ID},
}
resp := &Incident{}
err = c.doPost("incidents.json", i, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) ScheduleIncident(component, name, message string, start, end time.Time, remind, autoInProgress, autoComplete bool) (*Incident, error) {
cp, err := c.GetComponentByName(component)
if err != nil {
return nil, err
}
i := &ScheduledIncident{
Name: name,
Status: "scheduled",
ScheduledFor: start,
ScheduledUntil: end,
WantsTwitterUpdate: false,
ScheduledRemindPrior: remind,
ScheduledAutoInProgress: autoInProgress,
ScheduledAutoCompleted: autoComplete,
ImpactOverride: "none",
Message: message,
ComponentIDs: []string{*cp.ID},
}
resp := &Incident{}
err = c.doPost("incidents.json", i, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) CreateHistoricIncident(name, message string, date time.Time) (*Incident, error) {
i := &HistoricIncident{
Name: name,
Message: message,
Backfilled: true,
BackfillDate: date.Format("2006-01-02"),
}
resp := &Incident{}
err := c.doPost("incidents.json", i, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// UpdateIncident updates an incident. If Status and/or Message are different,
// a new update will be published for the incident. Each change will add an
// update notification, so updates should be batched.
func (c *Client) UpdateIncident(incident *Incident, name, status, message string) (*Incident, error) {
path := "incidents/" + *incident.ID + ".json"
u := &NewIncidentUpdate{
Name: name,
Status: status,
Message: message,
}
resp := &Incident{}
err := c.doPatch(path, u, resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) DeleteIncident(incident *Incident) (*Incident, error) {
path := "incidents/" + *incident.ID + ".json"
resp := &Incident{}
err := c.doDelete(path, nil, resp)
if err != nil {
return nil, err
}
return resp, nil
}