-
Notifications
You must be signed in to change notification settings - Fork 302
/
uptime.go
351 lines (293 loc) · 10.7 KB
/
uptime.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
package godo
import (
"context"
"fmt"
"net/http"
"path"
)
const (
uptimeChecksBasePath = "/v2/uptime/checks"
// UptimeAlertGreaterThan is the comparison >
UptimeAlertGreaterThan UptimeAlertComp = "greater_than"
// UptimeAlertLessThan is the comparison <
UptimeAlertLessThan UptimeAlertComp = "less_than"
)
// UptimeChecksService is an interface for creating and managing Uptime checks with the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Uptime
type UptimeChecksService interface {
List(context.Context, *ListOptions) ([]UptimeCheck, *Response, error)
Get(context.Context, string) (*UptimeCheck, *Response, error)
GetState(context.Context, string) (*UptimeCheckState, *Response, error)
Create(context.Context, *CreateUptimeCheckRequest) (*UptimeCheck, *Response, error)
Update(context.Context, string, *UpdateUptimeCheckRequest) (*UptimeCheck, *Response, error)
Delete(context.Context, string) (*Response, error)
GetAlert(context.Context, string, string) (*UptimeAlert, *Response, error)
ListAlerts(context.Context, string, *ListOptions) ([]UptimeAlert, *Response, error)
CreateAlert(context.Context, string, *CreateUptimeAlertRequest) (*UptimeAlert, *Response, error)
UpdateAlert(context.Context, string, string, *UpdateUptimeAlertRequest) (*UptimeAlert, *Response, error)
DeleteAlert(context.Context, string, string) (*Response, error)
}
// UptimeChecksServiceOp handles communication with Uptime Check methods of the DigitalOcean API.
type UptimeChecksServiceOp struct {
client *Client
}
// UptimeCheck represents a DigitalOcean UptimeCheck configuration.
type UptimeCheck struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Target string `json:"target"`
Regions []string `json:"regions"`
Enabled bool `json:"enabled"`
}
// UptimeAlert represents a DigitalOcean Uptime Alert configuration.
type UptimeAlert struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Threshold int `json:"threshold"`
Comparison UptimeAlertComp `json:"comparison"`
Notifications *Notifications `json:"notifications"`
Period string `json:"period"`
}
// Notifications represents a DigitalOcean Notifications configuration.
type Notifications struct {
Email []string `json:"email"`
Slack []SlackDetails `json:"slack"`
}
// UptimeCheckState represents a DigitalOcean Uptime Check's state configuration.
type UptimeCheckState struct {
Regions map[string]UptimeRegion `json:"regions"`
PreviousOutage UptimePreviousOutage `json:"previous_outage"`
}
type UptimeRegion struct {
Status string `json:"status"`
StatusChangedAt string `json:"status_changed_at"`
ThirtyDayUptimePercentage float32 `json:"thirty_day_uptime_percentage"`
}
// UptimePreviousOutage represents a DigitalOcean Uptime Check's previous outage configuration.
type UptimePreviousOutage struct {
Region string `json:"region"`
StartedAt string `json:"started_at"`
EndedAt string `json:"ended_at"`
DurationSeconds int `json:"duration_seconds"`
}
// CreateUptimeCheckRequest represents the request to create a new uptime check.
type CreateUptimeCheckRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Target string `json:"target"`
Regions []string `json:"regions"`
Enabled bool `json:"enabled"`
}
// UpdateUptimeCheckRequest represents the request to update uptime check information.
type UpdateUptimeCheckRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Target string `json:"target"`
Regions []string `json:"regions"`
Enabled bool `json:"enabled"`
}
// CreateUptimeUptimeAlertRequest represents the request to create a new Uptime Alert.
type CreateUptimeAlertRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Threshold int `json:"threshold"`
Comparison UptimeAlertComp `json:"comparison"`
Notifications *Notifications `json:"notifications"`
Period string `json:"period"`
}
// UpdateUptimeAlertRequest represents the request to update an alert.
type UpdateUptimeAlertRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Threshold int `json:"threshold"`
Comparison UptimeAlertComp `json:"comparison"`
Notifications *Notifications `json:"notifications"`
Period string `json:"period"`
}
// UptimeAlertComp represents an uptime alert comparison operation
type UptimeAlertComp string
type uptimeChecksRoot struct {
UptimeChecks []UptimeCheck `json:"checks"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type uptimeCheckStateRoot struct {
UptimeCheckState UptimeCheckState `json:"state"`
}
type uptimeAlertsRoot struct {
UptimeAlerts []UptimeAlert `json:"alerts"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type uptimeCheckRoot struct {
UptimeCheck *UptimeCheck `json:"check"`
}
type uptimeAlertRoot struct {
UptimeAlert *UptimeAlert `json:"alert"`
}
var _ UptimeChecksService = &UptimeChecksServiceOp{}
// List Checks.
func (p *UptimeChecksServiceOp) List(ctx context.Context, opts *ListOptions) ([]UptimeCheck, *Response, error) {
path, err := addOptions(uptimeChecksBasePath, opts)
if err != nil {
return nil, nil, err
}
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(uptimeChecksRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.UptimeChecks, resp, err
}
// GetState of uptime check.
func (p *UptimeChecksServiceOp) GetState(ctx context.Context, uptimeCheckID string) (*UptimeCheckState, *Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID, "/state")
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(uptimeCheckStateRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return &root.UptimeCheckState, resp, err
}
// Get retrieves a single uptime check by its ID.
func (p *UptimeChecksServiceOp) Get(ctx context.Context, uptimeCheckID string) (*UptimeCheck, *Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID)
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(uptimeCheckRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeCheck, resp, err
}
// Create a new uptime check.
func (p *UptimeChecksServiceOp) Create(ctx context.Context, cr *CreateUptimeCheckRequest) (*UptimeCheck, *Response, error) {
req, err := p.client.NewRequest(ctx, http.MethodPost, uptimeChecksBasePath, cr)
if err != nil {
return nil, nil, err
}
root := new(uptimeCheckRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeCheck, resp, err
}
// Update an uptime check.
func (p *UptimeChecksServiceOp) Update(ctx context.Context, uptimeCheckID string, ur *UpdateUptimeCheckRequest) (*UptimeCheck, *Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID)
req, err := p.client.NewRequest(ctx, http.MethodPut, path, ur)
if err != nil {
return nil, nil, err
}
root := new(uptimeCheckRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeCheck, resp, err
}
// Delete an existing uptime check.
func (p *UptimeChecksServiceOp) Delete(ctx context.Context, uptimeCheckID string) (*Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID)
req, err := p.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return p.client.Do(ctx, req, nil)
}
// alerts
// ListAlerts lists alerts for a check.
func (p *UptimeChecksServiceOp) ListAlerts(ctx context.Context, uptimeCheckID string, opts *ListOptions) ([]UptimeAlert, *Response, error) {
fullPath := path.Join(uptimeChecksBasePath, uptimeCheckID, "/alerts")
path, err := addOptions(fullPath, opts)
if err != nil {
return nil, nil, err
}
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(uptimeAlertsRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.UptimeAlerts, resp, err
}
// CreateAlert creates a new check alert.
func (p *UptimeChecksServiceOp) CreateAlert(ctx context.Context, uptimeCheckID string, cr *CreateUptimeAlertRequest) (*UptimeAlert, *Response, error) {
fullPath := path.Join(uptimeChecksBasePath, uptimeCheckID, "/alerts")
req, err := p.client.NewRequest(ctx, http.MethodPost, fullPath, cr)
if err != nil {
return nil, nil, err
}
root := new(uptimeAlertRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeAlert, resp, err
}
// GetAlert retrieves a single uptime check alert by its ID.
func (p *UptimeChecksServiceOp) GetAlert(ctx context.Context, uptimeCheckID string, alertID string) (*UptimeAlert, *Response, error) {
path := fmt.Sprintf("v2/uptime/checks/%s/alerts/%s", uptimeCheckID, alertID)
req, err := p.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(uptimeAlertRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeAlert, resp, err
}
// UpdateAlert updates an check's alert.
func (p *UptimeChecksServiceOp) UpdateAlert(ctx context.Context, uptimeCheckID string, alertID string, ur *UpdateUptimeAlertRequest) (*UptimeAlert, *Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID, "/alerts/", alertID)
req, err := p.client.NewRequest(ctx, http.MethodPut, path, ur)
if err != nil {
return nil, nil, err
}
root := new(uptimeAlertRoot)
resp, err := p.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.UptimeAlert, resp, err
}
// DeleteAlert deletes an existing check's alert.
func (p *UptimeChecksServiceOp) DeleteAlert(ctx context.Context, uptimeCheckID string, alertID string) (*Response, error) {
path := path.Join(uptimeChecksBasePath, uptimeCheckID, "/alerts/", alertID)
req, err := p.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return p.client.Do(ctx, req, nil)
}