-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrunstatus_incident.go
176 lines (145 loc) · 4.63 KB
/
runstatus_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
package egoscale
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
// RunstatusIncident is a runstatus incident
type RunstatusIncident struct {
EndDate *time.Time `json:"end_date,omitempty"`
Events []RunstatusEvent `json:"events,omitempty"`
EventsURL string `json:"events_url,omitempty"`
ID int `json:"id,omitempty"`
PageURL string `json:"page_url,omitempty"` // fake field
PostMortem string `json:"post_mortem,omitempty"`
RealTime bool `json:"real_time,omitempty"`
Services []string `json:"services"`
StartDate *time.Time `json:"start_date,omitempty"`
State string `json:"state"`
Status string `json:"status"`
StatusText string `json:"status_text"`
Title string `json:"title"`
URL string `json:"url,omitempty"`
}
// Match returns true if the other incident has got similarities with itself
func (incident RunstatusIncident) Match(other RunstatusIncident) bool {
if other.Title != "" && incident.Title == other.Title {
return true
}
if other.ID > 0 && incident.ID == other.ID {
return true
}
return false
}
// RunstatusIncidentList is a list of incident
type RunstatusIncidentList struct {
Next string `json:"next"`
Previous string `json:"previous"`
Incidents []RunstatusIncident `json:"results"`
}
// GetRunstatusIncident retrieves the details of a specific incident.
func (client *Client) GetRunstatusIncident(ctx context.Context, incident RunstatusIncident) (*RunstatusIncident, error) {
if incident.URL != "" {
return client.getRunstatusIncident(ctx, incident.URL)
}
if incident.PageURL == "" {
return nil, fmt.Errorf("empty Page URL for %#v", incident)
}
page, err := client.getRunstatusPage(ctx, incident.PageURL)
if err != nil {
return nil, err
}
for i := range page.Incidents {
j := &page.Incidents[i]
if j.Match(incident) {
return j, nil
}
}
return nil, errors.New("incident not found")
}
func (client *Client) getRunstatusIncident(ctx context.Context, incidentURL string) (*RunstatusIncident, error) {
resp, err := client.runstatusRequest(ctx, incidentURL, nil, "GET")
if err != nil {
return nil, err
}
i := new(RunstatusIncident)
if err := json.Unmarshal(resp, i); err != nil {
return nil, err
}
return i, nil
}
// ListRunstatusIncidents lists the incidents for a specific page.
func (client *Client) ListRunstatusIncidents(ctx context.Context, page RunstatusPage) ([]RunstatusIncident, error) {
if page.IncidentsURL == "" {
return nil, fmt.Errorf("empty Incidents URL for %#v", page)
}
results := make([]RunstatusIncident, 0)
var err error
client.PaginateRunstatusIncidents(ctx, page, func(incident *RunstatusIncident, e error) bool {
if e != nil {
err = e
return false
}
results = append(results, *incident)
return true
})
return results, err
}
// PaginateRunstatusIncidents paginate Incidents
func (client *Client) PaginateRunstatusIncidents(ctx context.Context, page RunstatusPage, callback func(*RunstatusIncident, error) bool) {
if page.IncidentsURL == "" {
callback(nil, fmt.Errorf("empty Incidents URL for %#v", page))
return
}
incidentsURL := page.IncidentsURL
for incidentsURL != "" {
resp, err := client.runstatusRequest(ctx, incidentsURL, nil, "GET")
if err != nil {
callback(nil, err)
return
}
var is *RunstatusIncidentList
if err := json.Unmarshal(resp, &is); err != nil {
callback(nil, err)
return
}
for i := range is.Incidents {
if cont := callback(&is.Incidents[i], nil); !cont {
return
}
}
incidentsURL = is.Next
}
}
// CreateRunstatusIncident create runstatus incident
func (client *Client) CreateRunstatusIncident(ctx context.Context, incident RunstatusIncident) (*RunstatusIncident, error) {
if incident.PageURL == "" {
return nil, fmt.Errorf("empty Page URL for %#v", incident)
}
page, err := client.getRunstatusPage(ctx, incident.PageURL)
if err != nil {
return nil, err
}
if page.IncidentsURL == "" {
return nil, fmt.Errorf("empty Incidents URL for %#v", page)
}
resp, err := client.runstatusRequest(ctx, page.IncidentsURL, incident, "POST")
if err != nil {
return nil, err
}
i := &RunstatusIncident{}
if err := json.Unmarshal(resp, &i); err != nil {
return nil, err
}
return i, nil
}
// DeleteRunstatusIncident delete runstatus incident
func (client *Client) DeleteRunstatusIncident(ctx context.Context, incident RunstatusIncident) error {
if incident.URL == "" {
return fmt.Errorf("empty URL for %#v", incident)
}
_, err := client.runstatusRequest(ctx, incident.URL, nil, "DELETE")
return err
}