-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
432 lines (364 loc) · 11.7 KB
/
service.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
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// Service definition https://api.ilert.com/api-docs/#tag/Services
type Service struct {
ID int64 `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Description string `json:"description"`
OneOpenIncidentOnly bool `json:"oneOpenIncidentOnly"`
ShowUptimeHistory bool `json:"showUptimeHistory"`
Teams []TeamShort `json:"teams"`
Subscribed bool `json:"subscribed,omitempty"`
Uptime *ServiceUptime `json:"uptime,omitempty"`
Incidents []Incident `json:"incidents,omitempty"`
}
// ServiceUptime defines services uptime
type ServiceUptime struct {
RangeStart string `json:"rangeStart"` // Date time string in ISO format
RangeEnd string `json:"rangeEnd"` // Date time string in ISO format
Outages []ServiceOutage `json:"outages"`
UptimePercentage *ServiceUptimePercentage `json:"uptimePercentage"`
}
// ServiceUptimePercentage defines service uptime percentage
type ServiceUptimePercentage struct {
P90 float64 `json:"p90"`
P60 float64 `json:"p60"`
P30 float64 `json:"p30"`
}
// ServiceOutage defines services outage
type ServiceOutage struct {
Status string `json:"status"`
From string `json:"from"` // Date time string in ISO format
Until string `json:"until"` // Date time string in ISO format
}
// ServiceStatus defines services status
var ServiceStatus = struct {
Operational string
UnderMaintenance string
Degraded string
PartialOutage string
MajorOutage string
}{
Operational: "OPERATIONAL",
UnderMaintenance: "UNDER_MAINTENANCE",
Degraded: "DEGRADED",
PartialOutage: "PARTIAL_OUTAGE",
MajorOutage: "MAJOR_OUTAGE",
}
// ServiceStatusAll defines services status list
var ServiceStatusAll = []string{
ServiceStatus.Operational,
ServiceStatus.UnderMaintenance,
ServiceStatus.Degraded,
ServiceStatus.PartialOutage,
ServiceStatus.MajorOutage,
}
// ServiceInclude defines included services
var ServiceInclude = struct {
Subscribed string
Uptime string
Incidents string
}{
Subscribed: "subscribed",
Uptime: "uptime",
Incidents: "incidents",
}
// ServiceIncludeAll defines included services list
var ServiceIncludeAll = []string{
ServiceInclude.Subscribed,
ServiceInclude.Uptime,
ServiceInclude.Incidents,
}
// CreateServiceInput represents the input of a CreateService operation.
type CreateServiceInput struct {
_ struct{}
Service *Service
}
// CreateServiceOutput represents the output of a CreateService operation.
type CreateServiceOutput struct {
_ struct{}
Service *Service
}
// CreateService creates a new service. https://api.ilert.com/api-docs/#tag/Services/paths/~1services/post
func (c *Client) CreateService(input *CreateServiceInput) (*CreateServiceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.Service == nil {
return nil, errors.New("service input is required")
}
resp, err := c.httpClient.R().SetBody(input.Service).Post(apiRoutes.services)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
service := &Service{}
err = json.Unmarshal(resp.Body(), service)
if err != nil {
return nil, err
}
return &CreateServiceOutput{Service: service}, nil
}
// GetServicesInput represents the input of a GetServices operation.
type GetServicesInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
// Default: 0
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Default: 10, Maximum: 25 or 100 without include
MaxResults *int
// describes optional properties that should be included in the response
Include []*string
}
// GetServicesOutput represents the output of a GetServices operation.
type GetServicesOutput struct {
_ struct{}
Services []*Service
}
// GetServices lists existing services. https://api.ilert.com/api-docs/#tag/Services/paths/~1services/get
func (c *Client) GetServices(input *GetServicesInput) (*GetServicesOutput, error) {
if input == nil {
input = &GetServicesInput{}
}
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
} else {
q.Add("start-index", "0")
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
} else {
q.Add("max-results", "10")
}
for _, include := range input.Include {
q.Add("include", *include)
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.services, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
services := make([]*Service, 0)
err = json.Unmarshal(resp.Body(), &services)
if err != nil {
return nil, err
}
return &GetServicesOutput{Services: services}, nil
}
// GetServiceInput represents the input of a GetService operation.
type GetServiceInput struct {
_ struct{}
ServiceID *int64
// describes optional properties that should be included in the response
Include []*string
}
// GetServiceOutput represents the output of a GetService operation.
type GetServiceOutput struct {
_ struct{}
Service *Service
}
// GetService gets a service by id. https://api.ilert.com/api-docs/#tag/Services/paths/~1services~1{id}/get
func (c *Client) GetService(input *GetServiceInput) (*GetServiceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceID == nil {
return nil, errors.New("service id is required")
}
q := url.Values{}
for _, include := range input.Include {
q.Add("include", *include)
}
var url = fmt.Sprintf("%s/%d?%s", apiRoutes.services, *input.ServiceID, q.Encode())
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
service := &Service{}
err = json.Unmarshal(resp.Body(), service)
if err != nil {
return nil, err
}
return &GetServiceOutput{Service: service}, nil
}
// GetServiceSubscribersInput represents the input of a GetServiceSubscribers operation.
type GetServiceSubscribersInput struct {
_ struct{}
ServiceID *int64
}
// GetServiceSubscribersOutput represents the output of a GetServiceSubscribers operation.
type GetServiceSubscribersOutput struct {
_ struct{}
Subscribers []*Subscriber
}
// GetServiceSubscribers gets subscribers of a service by id. https://api.ilert.com/api-docs/#tag/Services/paths/~1services~1{id}~1private-subscribers/get
func (c *Client) GetServiceSubscribers(input *GetServiceSubscribersInput) (*GetServiceSubscribersOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceID == nil {
return nil, errors.New("service id is required")
}
var url = fmt.Sprintf("%s/%d/private-subscribers", apiRoutes.services, *input.ServiceID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
subscribers := make([]*Subscriber, 0)
err = json.Unmarshal(resp.Body(), &subscribers)
if err != nil {
return nil, err
}
return &GetServiceSubscribersOutput{Subscribers: subscribers}, nil
}
// SearchServiceInput represents the input of a SearchService operation.
type SearchServiceInput struct {
_ struct{}
ServiceName *string
}
// SearchServiceOutput represents the output of a SearchService operation.
type SearchServiceOutput struct {
_ struct{}
Service *Service
}
// SearchService gets the service with specified name.
func (c *Client) SearchService(input *SearchServiceInput) (*SearchServiceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceName == nil {
return nil, errors.New("service name is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.services, *input.ServiceName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
service := &Service{}
err = json.Unmarshal(resp.Body(), service)
if err != nil {
return nil, err
}
return &SearchServiceOutput{Service: service}, nil
}
// UpdateServiceInput represents the input of a UpdateService operation.
type UpdateServiceInput struct {
_ struct{}
ServiceID *int64
Service *Service
}
// UpdateServiceOutput represents the output of a UpdateService operation.
type UpdateServiceOutput struct {
_ struct{}
Service *Service
}
// UpdateService updates the specific service. https://api.ilert.com/api-docs/#tag/Services/paths/~1services~1{id}/put
func (c *Client) UpdateService(input *UpdateServiceInput) (*UpdateServiceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceID == nil {
return nil, errors.New("service id is required")
}
if input.Service == nil {
return nil, errors.New("service input is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.services, *input.ServiceID)
resp, err := c.httpClient.R().SetBody(input.Service).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
service := &Service{}
err = json.Unmarshal(resp.Body(), service)
if err != nil {
return nil, err
}
return &UpdateServiceOutput{Service: service}, nil
}
// AddServiceSubscribersInput represents the input of a AddServiceSubscribers operation.
type AddServiceSubscribersInput struct {
_ struct{}
ServiceID *int64
Subscribers *[]Subscriber
}
// AddServiceSubscribersOutput represents the output of a AddServiceSubscribers operation.
type AddServiceSubscribersOutput struct {
_ struct{}
}
// AddServiceSubscribers adds a new subscriber to a service. https://api.ilert.com/api-docs/#tag/Services/paths/~1services~1{id}~1private-subscribers/post
func (c *Client) AddServiceSubscribers(input *AddServiceSubscribersInput) (*AddServiceSubscribersOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceID == nil {
return nil, errors.New("service id is required")
}
if input.Subscribers == nil {
return nil, errors.New("subscriber input is required")
}
url := fmt.Sprintf("%s/%d/private-subscribers", apiRoutes.services, *input.ServiceID)
resp, err := c.httpClient.R().SetBody(input.Subscribers).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 202); apiErr != nil {
return nil, apiErr
}
subscribers := make([]*Subscriber, 0)
err = json.Unmarshal(resp.Body(), &subscribers)
if err != nil {
return nil, err
}
return &AddServiceSubscribersOutput{}, nil
}
// DeleteServiceInput represents the input of a DeleteService operation.
type DeleteServiceInput struct {
_ struct{}
ServiceID *int64
}
// DeleteServiceOutput represents the output of a DeleteService operation.
type DeleteServiceOutput struct {
_ struct{}
}
// DeleteService deletes the specified service. https://api.ilert.com/api-docs/#tag/Services/paths/~1services~1{id}/delete
func (c *Client) DeleteService(input *DeleteServiceInput) (*DeleteServiceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.ServiceID == nil {
return nil, errors.New("service id is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.services, *input.ServiceID)
resp, err := c.httpClient.R().Delete(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteServiceOutput{}, nil
}