-
Notifications
You must be signed in to change notification settings - Fork 14
/
runstatus_maintenance.go
209 lines (174 loc) · 5.54 KB
/
runstatus_maintenance.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
package egoscale
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"path"
"strconv"
"time"
)
// RunstatusMaintenance is a runstatus maintenance
type RunstatusMaintenance struct {
Created *time.Time `json:"created,omitempty"`
Description string `json:"description,omitempty"`
EndDate *time.Time `json:"end_date"`
Events []RunstatusEvent `json:"events,omitempty"`
EventsURL string `json:"events_url,omitempty"`
ID int `json:"id,omitempty"` // missing field
PageURL string `json:"page_url,omitempty"` // fake field
RealTime bool `json:"real_time,omitempty"`
Services []string `json:"services"`
StartDate *time.Time `json:"start_date"`
Status string `json:"status"`
Title string `json:"title"`
URL string `json:"url,omitempty"`
}
// Match returns true if the other maintenance has got similarities with itself
func (maintenance RunstatusMaintenance) Match(other RunstatusMaintenance) bool {
if other.Title != "" && maintenance.Title == other.Title {
return true
}
if other.ID > 0 && maintenance.ID == other.ID {
return true
}
return false
}
// FakeID fills up the ID field as it's currently missing
func (maintenance *RunstatusMaintenance) FakeID() error {
if maintenance.ID > 0 {
return nil
}
if maintenance.URL == "" {
return fmt.Errorf("empty URL for %#v", maintenance)
}
u, err := url.Parse(maintenance.URL)
if err != nil {
return err
}
s := path.Base(u.Path)
id, err := strconv.Atoi(s)
if err != nil {
return err
}
maintenance.ID = id
return nil
}
// RunstatusMaintenanceList is a list of incident
type RunstatusMaintenanceList struct {
Next string `json:"next"`
Previous string `json:"previous"`
Maintenances []RunstatusMaintenance `json:"results"`
}
// GetRunstatusMaintenance retrieves the details of a specific maintenance.
func (client *Client) GetRunstatusMaintenance(ctx context.Context, maintenance RunstatusMaintenance) (*RunstatusMaintenance, error) {
if maintenance.URL != "" {
return client.getRunstatusMaintenance(ctx, maintenance.URL)
}
if maintenance.PageURL == "" {
return nil, fmt.Errorf("empty Page URL for %#v", maintenance)
}
page, err := client.getRunstatusPage(ctx, maintenance.PageURL)
if err != nil {
return nil, err
}
for i := range page.Maintenances {
m := &page.Maintenances[i]
if m.Match(maintenance) {
if err := m.FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", m, err)
}
return m, nil
}
}
return nil, errors.New("maintenance not found")
}
func (client *Client) getRunstatusMaintenance(ctx context.Context, maintenanceURL string) (*RunstatusMaintenance, error) {
resp, err := client.runstatusRequest(ctx, maintenanceURL, nil, "GET")
if err != nil {
return nil, err
}
m := new(RunstatusMaintenance)
if err := json.Unmarshal(resp, m); err != nil {
return nil, err
}
return m, nil
}
// ListRunstatusMaintenances returns the list of maintenances for the page.
func (client *Client) ListRunstatusMaintenances(ctx context.Context, page RunstatusPage) ([]RunstatusMaintenance, error) {
if page.MaintenancesURL == "" {
return nil, fmt.Errorf("empty Maintenances URL for %#v", page)
}
results := make([]RunstatusMaintenance, 0)
var err error
client.PaginateRunstatusMaintenances(ctx, page, func(maintenance *RunstatusMaintenance, e error) bool {
if e != nil {
err = e
return false
}
results = append(results, *maintenance)
return true
})
return results, err
}
// PaginateRunstatusMaintenances paginate Maintenances
func (client *Client) PaginateRunstatusMaintenances(ctx context.Context, page RunstatusPage, callback func(*RunstatusMaintenance, error) bool) { // nolint: dupl
if page.MaintenancesURL == "" {
callback(nil, fmt.Errorf("empty Maintenances URL for %#v", page))
return
}
maintenancesURL := page.MaintenancesURL
for maintenancesURL != "" {
resp, err := client.runstatusRequest(ctx, maintenancesURL, nil, "GET")
if err != nil {
callback(nil, err)
return
}
var ms *RunstatusMaintenanceList
if err := json.Unmarshal(resp, &ms); err != nil {
callback(nil, err)
return
}
for i := range ms.Maintenances {
if err := ms.Maintenances[i].FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", ms.Maintenances[i], err)
}
if cont := callback(&ms.Maintenances[i], nil); !cont {
return
}
}
maintenancesURL = ms.Next
}
}
// CreateRunstatusMaintenance create runstatus Maintenance
func (client *Client) CreateRunstatusMaintenance(ctx context.Context, maintenance RunstatusMaintenance) (*RunstatusMaintenance, error) {
if maintenance.PageURL == "" {
return nil, fmt.Errorf("empty Page URL for %#v", maintenance)
}
page, err := client.getRunstatusPage(ctx, maintenance.PageURL)
if err != nil {
return nil, err
}
resp, err := client.runstatusRequest(ctx, page.MaintenancesURL, maintenance, "POST")
if err != nil {
return nil, err
}
m := &RunstatusMaintenance{}
if err := json.Unmarshal(resp, &m); err != nil {
return nil, err
}
if err := m.FakeID(); err != nil {
log.Printf("bad fake ID for %#v, %s", m, err)
}
return m, nil
}
// DeleteRunstatusMaintenance delete runstatus Maintenance
func (client *Client) DeleteRunstatusMaintenance(ctx context.Context, maintenance RunstatusMaintenance) error {
if maintenance.URL == "" {
return fmt.Errorf("empty URL for %#v", maintenance)
}
_, err := client.runstatusRequest(ctx, maintenance.URL, nil, "DELETE")
return err
}