forked from checkly/checkly-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
535 lines (480 loc) · 18.4 KB
/
types.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package checkly
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
// Client is an interface that implements Checkly's API
type Client interface {
// Create creates a new check with the specified details.
// It returns the newly-created check, or an error.
Create(
ctx context.Context,
check Check,
) (*Check, error)
// Update updates an existing check with the specified details.
// It returns the updated check, or an error.
Update(
ctx context.Context,
ID string,
check Check,
) (*Check, error)
// Delete deletes the check with the specified ID.
// It returns a non-nil error if the request failed.
Delete(
ctx context.Context,
ID string,
) error
// Get takes the ID of an existing check, and returns the check parameters,
// or an error.
Get(
ctx context.Context,
ID string,
) (*Check, error)
// CreateGroup creates a new check group with the specified details.
// It returns the newly-created group, or an error.
CreateGroup(
ctx context.Context,
group Group,
) (*Group, error)
// GetGroup takes the ID of an existing check group, and returns the
// corresponding group, or an error.
GetGroup(
ctx context.Context,
ID int64,
) (*Group, error)
// UpdateGroup takes the ID of an existing check group, and updates the
// corresponding check group to match the supplied group. It returns the updated
// group, or an error.
UpdateGroup(
ctx context.Context,
ID int64,
group Group,
) (*Group, error)
// DeleteGroup deletes the check group with the specified ID. It returns a
// non-nil error if the request failed.
DeleteGroup(
ctx context.Context,
ID int64,
) error
// GetCheckResult gets a specific Check result, or it returns an error.
GetCheckResult(
ctx context.Context,
checkID,
checkResultID string,
) (*CheckResult, error)
// GetCheckResults gets the results of the given Check
GetCheckResults(
ctx context.Context,
checkID string,
filters *CheckResultsFilter,
) ([]CheckResult, error)
// CreateSnippet creates a new snippet with the specified details. It returns
// the newly-created snippet, or an error.
CreateSnippet(
ctx context.Context,
snippet Snippet,
) (*Snippet, error)
// GetSnippet takes the ID of an existing snippet, and returns the
// corresponding snippet, or an error.
GetSnippet(
ctx context.Context,
ID int64,
) (*Snippet, error)
// UpdateSnippet takes the ID of an existing snippet, and updates the
// corresponding snippet to match the supplied snippet. It returns the updated
// snippet, or an error.
UpdateSnippet(
ctx context.Context,
ID int64,
snippet Snippet,
) (*Snippet, error)
// DeleteSnippet deletes the snippet with the specified ID. It returns a
// non-nil error if the request failed.
DeleteSnippet(
ctx context.Context,
ID int64,
) error
// CreateEnvironmentVariable creates a new environment variable with the
// specified details. It returns the newly-created environment variable,
// or an error.
CreateEnvironmentVariable(
ctx context.Context,
envVar EnvironmentVariable,
) (*EnvironmentVariable, error)
// GetEnvironmentVariable takes the ID of an existing environment variable, and returns the
// corresponding environment variable, or an error.
GetEnvironmentVariable(
ctx context.Context,
key string,
) (*EnvironmentVariable, error)
// UpdateEnvironmentVariable takes the ID of an existing environment variable, and updates the
// corresponding environment variable to match the supplied environment variable. It returns the updated
// environment variable, or an error.
UpdateEnvironmentVariable(
ctx context.Context,
key string,
envVar EnvironmentVariable,
) (*EnvironmentVariable, error)
// DeleteEnvironmentVariable deletes the environment variable with the specified ID. It returns a
// non-nil error if the request failed.
DeleteEnvironmentVariable(
ctx context.Context,
key string,
) error
// CreateAlertChannel creates a new alert channel with the specified details. It returns
// the newly-created alert channel, or an error.
CreateAlertChannel(
ctx context.Context,
ac AlertChannel,
) (*AlertChannel, error)
// GetAlertChannel takes the ID of an existing alert channel, and returns the
// corresponding alert channel, or an error.
GetAlertChannel(
ctx context.Context,
ID int64,
) (*AlertChannel, error)
// UpdateAlertChannel takes the ID of an existing alert channel, and updates the
// corresponding alert channel to match the supplied alert channel. It returns the updated
// alert channel, or an error.
UpdateAlertChannel(
ctx context.Context,
ID int64,
ac AlertChannel,
) (*AlertChannel, error)
// DeleteAlertChannel deletes the alert channel with the specified ID. It returns a
// non-nil error if the request failed.
DeleteAlertChannel(
ctx context.Context,
ID int64,
) error
}
// client represents a Checkly client. If the Debug field is set to an io.Writer
// (for example os.Stdout), then the client will dump API requests and responses
// to it. To use a non-default HTTP client (for example, for testing, or to set
// a timeout), assign to the HTTPClient field. To set a non-default URL (for
// example, for testing), assign to the URL field.
type client struct {
apiKey string
url string
httpClient *http.Client
debug io.Writer
}
// Check represents the parameters for an existing check.
type Check struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"checkType"`
Frequency int `json:"frequency"`
FrequencyOffset int `json:"frequencyOffset,omitempty"`
Activated bool `json:"activated"`
Muted bool `json:"muted"`
ShouldFail bool `json:"shouldFail"`
Locations []string `json:"locations"`
DegradedResponseTime int `json:"degradedResponseTime"`
MaxResponseTime int `json:"maxResponseTime"`
Script string `json:"script,omitempty"`
EnvironmentVariables []EnvironmentVariable `json:"environmentVariables"`
DoubleCheck bool `json:"doubleCheck"`
Tags []string `json:"tags,omitempty"`
SSLCheck bool `json:"sslCheck"`
SetupSnippetID *int64 `json:"setupSnippetId"`
TearDownSnippetID *int64 `json:"tearDownSnippetId"`
LocalSetupScript string `json:"localSetupScript"`
LocalTearDownScript string `json:"localTearDownScript"`
AlertSettings *AlertSettings `json:"alertSettings,omitempty"`
UseGlobalAlertSettings bool `json:"useGlobalAlertSettings"`
Request Request `json:"request"`
GroupID *int64 `json:"groupId"`
GroupOrder int `json:"groupOrder,omitempty"`
AlertChannelSubscriptions []AlertChannelSubscription `json:"alertChannelSubscriptions,omitempty"`
}
// Request represents the parameters for the request made by the check.
type Request struct {
Method string `json:"method"`
URL string `json:"url"`
FollowRedirects bool `json:"followRedirects"`
Body string `json:"body"`
BodyType string `json:"bodyType,omitempty"`
Headers []KeyValue `json:"headers,omitempty"`
QueryParameters []KeyValue `json:"queryParameters,omitempty"`
Assertions []Assertion `json:"assertions,omitempty"`
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
}
// Assertion represents an assertion about an API response, which will be
// verified as part of the check.
type Assertion struct {
Edit bool `json:"edit"`
Order int `json:"order"`
ArrayIndex int `json:"arrayIndex"`
ArraySelector int `json:"arraySelector"`
Source string `json:"source"`
Property string `json:"property"`
Comparison string `json:"comparison"`
Target string `json:"target"`
}
// BasicAuth represents the HTTP basic authentication credentials for a request.
type BasicAuth struct {
Username string `json:"username"`
Password string `json:"password"`
}
// KeyValue represents a key-value pair, for example a request header setting,
// or a query parameter.
type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
Locked bool `json:"locked"`
}
// EnvironmentVariable represents a key-value pair for setting environment
// values during check execution.
type EnvironmentVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Locked bool `json:"locked"`
}
// AlertSettings represents an alert configuration.
type AlertSettings struct {
EscalationType string `json:"escalationType,omitempty"`
RunBasedEscalation *RunBasedEscalation `json:"runBasedEscalation,omitempty"`
TimeBasedEscalation *TimeBasedEscalation `json:"timeBasedEscalation,omitempty"`
Reminders *Reminders `json:"reminders,omitempty"`
SSLCertificates *SSLCertificates `json:"sslCertificates,omitempty"`
}
// RunBasedEscalation represents an alert escalation based on a number of failed
// check runs.
type RunBasedEscalation struct {
FailedRunThreshold int `json:"failedRunThreshold,omitempty"`
}
// TimeBasedEscalation represents an alert escalation based on the number of
// minutes after a check first starts failing.
type TimeBasedEscalation struct {
MinutesFailingThreshold int `json:"minutesFailingThreshold,omitempty"`
}
// Reminders represents the number of reminders to send after an alert
// notification, and the time interval between them.
type Reminders struct {
Amount int `json:"amount,omitempty"`
Interval int `json:"interval,omitempty"`
}
// SSLCertificates represents alert settings for expiring SSL certificates.
type SSLCertificates struct {
Enabled bool `json:"enabled"`
AlertThreshold int `json:"alertThreshold"`
}
// Group represents a check group.
type Group struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Activated bool `json:"activated"`
Muted bool `json:"muted"`
Tags []string `json:"tags"`
Locations []string `json:"locations"`
Concurrency int `json:"concurrency"`
APICheckDefaults APICheckDefaults `json:"apiCheckDefaults"`
EnvironmentVariables []EnvironmentVariable `json:"environmentVariables"`
DoubleCheck bool `json:"doubleCheck"`
UseGlobalAlertSettings bool `json:"useGlobalAlertSettings"`
AlertSettings AlertSettings `json:"alertSettings,omitempty"`
SetupSnippetID *int64 `json:"setupSnippetId"`
TearDownSnippetID *int64 `json:"tearDownSnippetId"`
LocalSetupScript string `json:"localSetupScript"`
LocalTearDownScript string `json:"localTearDownScript"`
AlertChannelSubscriptions []AlertChannelSubscription `json:"alertChannelSubscriptions,omitempty"`
}
// APICheckDefaults represents the default settings for API checks within a
// given group.
type APICheckDefaults struct {
BaseURL string `json:"url"`
Headers []KeyValue `json:"headers,omitempty"`
QueryParameters []KeyValue `json:"queryParameters,omitempty"`
Assertions []Assertion `json:"assertions,omitempty"`
BasicAuth BasicAuth `json:"basicAuth,omitempty"`
}
// CheckResult represents a Check result
type CheckResult struct {
ID string `json:"id"`
Name string `json:"name"`
CheckID string `json:"checkId"`
HasFailures bool `json:"hasFailures"`
HasErrors bool `json:"hasErrors"`
IsDegraded bool `json:"isDegraded"`
OverMaxResponseTime bool `json:"overMaxResponseTime"`
RunLocation string `json:"runLocation"`
StartedAt time.Time `json:"startedAt"`
StoppedAt time.Time `json:"stoppedAt"`
CreatedAt time.Time `json:"created_at"`
ResponseTime int64 `json:"responseTime"`
ApiCheckResult *ApiCheckResult `json:"apiCheckResult"`
BrowserCheckResult *BrowserCheckResult `json:"browserCheckResult"`
CheckRunID int64 `json:"checkRunId"`
Attempts int64 `json:"attempts"`
}
// ApiCheckResult represents an API Check result
type ApiCheckResult map[string]interface{}
// BrowserCheckResult represents a Browser Check result
type BrowserCheckResult map[string]interface{}
// CheckResultsFilter represents the parameters that can be passed while
// getting Check Results
type CheckResultsFilter struct {
Limit int64
Page int64
Location string
To int64
From int64
CheckType CheckType
HasFailures bool
}
//Snippet defines Snippet type
type Snippet struct {
ID int64 `json:"id"`
Name string `json:"name"`
Script string `json:"script"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
const (
AlertTypeEmail = "EMAIL"
AlertTypeSlack = "SLACK"
AlertTypeWebhook = "WEBHOOK"
AlertTypeSMS = "SMS"
AlertTypePagerduty = "PAGERDUTY"
AlertTypeOpsgenie = "OPSGENIE"
)
// AlertChannelSubscription represents a subscription to an alert channel.
type AlertChannelSubscription struct {
ChannelID int64 `json:"alertChannelId"`
Activated bool `json:"activated"`
}
//AlertChannelEmail defines a type for an email alert channel
type AlertChannelEmail struct {
Address string `json:"address"`
}
//AlertChannelSlack defines a type for a slack alert channel
type AlertChannelSlack struct {
WebhookURL string `json:"url"`
Channel string `json:"channel"`
}
//AlertChannelSMS defines a type for a sms alert channel
type AlertChannelSMS struct {
Name string `json:"name"`
Number string `json:"number"`
}
//AlertChannelOpsgenie defines a type for an opsgenie alert channel
type AlertChannelOpsgenie struct {
Name string `json:"name"`
APIKey string `json:"apiKey"`
Region string `json:"region"`
Priority string `json:"priority"`
}
//AlertChannelPagerduty defines a type for an pager duty alert channel
type AlertChannelPagerduty struct {
Account string `json:"account,omitempty"`
ServiceKey string `json:"serviceKey"`
ServiceName string `json:"serviceName,omitempty"`
}
//AlertChannelWebhook defines a type for a webhook alert channel
type AlertChannelWebhook struct {
Name string `json:"name"`
Method string `json:"method"`
Headers []KeyValue `json:"headers"`
QueryParameters []KeyValue `json:"queryParameters"`
Template string `json:"template"`
URL string `json:"url"`
WebhookSecret string `json:"webhookSecret"`
}
// AlertChannel represents an alert channel and its subscribed checks. The API
// defines this data as read-only.
type AlertChannel struct {
ID int64 `json:"id,omitempty"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Email *AlertChannelEmail `json:"-"`
Slack *AlertChannelSlack `json:"-"`
SMS *AlertChannelSMS `json:"-"`
Opsgenie *AlertChannelOpsgenie `json:"-"`
Webhook *AlertChannelWebhook `json:"-"`
Pagerduty *AlertChannelPagerduty `json:"-"`
SendRecovery *bool `json:"sendRecovery"`
SendFailure *bool `json:"sendFailure"`
SendDegraded *bool `json:"sendDegraded"`
SSLExpiry *bool `json:"sslExpiry"`
SSLExpiryThreshold *int `json:"sslExpiryThreshold"`
}
//SetConfig sets config of alert channel based on it's type
func (a *AlertChannel) SetConfig(cfg interface{}) {
switch v := cfg.(type) {
case *AlertChannelEmail:
a.Email = cfg.(*AlertChannelEmail)
case *AlertChannelSMS:
a.SMS = cfg.(*AlertChannelSMS)
case *AlertChannelSlack:
a.Slack = cfg.(*AlertChannelSlack)
case *AlertChannelWebhook:
a.Webhook = cfg.(*AlertChannelWebhook)
case *AlertChannelOpsgenie:
a.Opsgenie = cfg.(*AlertChannelOpsgenie)
case *AlertChannelPagerduty:
a.Pagerduty = cfg.(*AlertChannelPagerduty)
default:
log.Printf("Unknown config type %v", v)
}
}
//GetConfig gets the config of the alert channel based on it's type
func (a *AlertChannel) GetConfig() (cfg map[string]interface{}) {
byts := []byte{}
var err error
switch a.Type {
case AlertTypeEmail:
byts, err = json.Marshal(a.Email)
case AlertTypeSMS:
byts, err = json.Marshal(a.SMS)
case AlertTypeSlack:
byts, err = json.Marshal(a.Slack)
case AlertTypeOpsgenie:
byts, err = json.Marshal(a.Opsgenie)
case AlertTypePagerduty:
byts, err = json.Marshal(a.Pagerduty)
case AlertTypeWebhook:
byts, err = json.Marshal(a.Webhook)
}
if err != nil {
panic(err)
}
err = json.Unmarshal(byts, &cfg)
return cfg
}
//AlertChannelConfigFromJSON gets AlertChannel.config from JSON
func AlertChannelConfigFromJSON(channelType string, cfgJSON []byte) (interface{}, error) {
switch channelType {
case AlertTypeEmail:
r := AlertChannelEmail{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
case AlertTypeSMS:
r := AlertChannelSMS{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
case AlertTypeSlack:
r := AlertChannelSlack{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
case AlertTypeOpsgenie:
r := AlertChannelOpsgenie{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
case AlertTypePagerduty:
r := AlertChannelPagerduty{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
case AlertTypeWebhook:
r := AlertChannelWebhook{}
json.Unmarshal(cfgJSON, &r)
return &r, nil
}
return nil, fmt.Errorf("Unknown AlertChannel.config type")
}