forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
397 lines (329 loc) · 13.5 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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// InlineModel represents when a scheduled action will occur.
type InlineModel struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
}
// ScheduledAction contains scheduled actions for the service.
type ScheduledAction struct {
Type string `json:"type,omitempty"`
At InlineModel `json:"at,omitempty"`
ToUrgency string `json:"to_urgency"`
}
// IncidentUrgencyType are the incidents urgency during or outside support hours.
type IncidentUrgencyType struct {
Type string `json:"type,omitempty"`
Urgency string `json:"urgency,omitempty"`
}
// SupportHours are the support hours for the service.
type SupportHours struct {
Type string `json:"type,omitempty"`
Timezone string `json:"time_zone,omitempty"`
StartTime string `json:"start_time,omitempty"`
EndTime string `json:"end_time,omitempty"`
DaysOfWeek []uint `json:"days_of_week,omitempty"`
}
// IncidentUrgencyRule is the default urgency for new incidents.
type IncidentUrgencyRule struct {
Type string `json:"type,omitempty"`
Urgency string `json:"urgency,omitempty"`
DuringSupportHours *IncidentUrgencyType `json:"during_support_hours,omitempty"`
OutsideSupportHours *IncidentUrgencyType `json:"outside_support_hours,omitempty"`
}
// ListServiceRulesResponse represents a list of rules in a service
type ListServiceRulesResponse struct {
Offset uint `json:"offset,omitempty"`
Limit uint `json:"limit,omitempty"`
More bool `json:"more,omitempty"`
Total uint `json:"total,omitempty"`
Rules []ServiceRule `json:"rules,omitempty"`
}
// ServiceRule represents a Service rule
type ServiceRule struct {
ID string `json:"id,omitempty"`
Self string `json:"self,omitempty"`
Disabled *bool `json:"disabled,omitempty"`
Conditions *RuleConditions `json:"conditions,omitempty"`
TimeFrame *RuleTimeFrame `json:"time_frame,omitempty"`
Position *int `json:"position,omitempty"`
Actions *ServiceRuleActions `json:"actions,omitempty"`
}
// ServiceRuleActions represents a rule action
type ServiceRuleActions struct {
Annotate *RuleActionParameter `json:"annotate,omitempty"`
EventAction *RuleActionParameter `json:"event_action,omitempty"`
Extractions []RuleActionExtraction `json:"extractions,omitempty"`
Priority *RuleActionParameter `json:"priority,omitempty"`
Severity *RuleActionParameter `json:"severity,omitempty"`
Suppress *RuleActionSuppress `json:"suppress,omitempty"`
Suspend *RuleActionSuspend `json:"suspend,omitempty"`
}
// Service represents something you monitor (like a web service, email service, or database service).
type Service struct {
APIObject
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
AutoResolveTimeout *uint `json:"auto_resolve_timeout,omitempty"`
AcknowledgementTimeout *uint `json:"acknowledgement_timeout,omitempty"`
CreateAt string `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"`
Integrations []Integration `json:"integrations,omitempty"`
EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"`
Teams []Team `json:"teams,omitempty"`
IncidentUrgencyRule *IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"`
SupportHours *SupportHours `json:"support_hours,omitempty"`
ScheduledActions []ScheduledAction `json:"scheduled_actions,omitempty"`
AlertCreation string `json:"alert_creation,omitempty"`
AlertGrouping string `json:"alert_grouping,omitempty"`
AlertGroupingTimeout *uint `json:"alert_grouping_timeout,omitempty"`
AlertGroupingParameters *AlertGroupingParameters `json:"alert_grouping_parameters,omitempty"`
ResponsePlay *APIObject `json:"response_play,omitempty"`
Addons []Addon `json:"addons,omitempty"`
}
// AlertGroupingParameters defines how alerts on the service will be automatically grouped into incidents
type AlertGroupingParameters struct {
Type string `json:"type,omitempty"`
Config *AlertGroupParamsConfig `json:"config,omitempty"`
}
// AlertGroupParamsConfig is the config object on alert_grouping_parameters
type AlertGroupParamsConfig struct {
Timeout *uint `json:"timeout,omitempty"`
Aggregate string `json:"aggregate,omitempty"`
Fields []string `json:"fields,omitempty"`
}
// ListServiceOptions is the data structure used when calling the ListServices API endpoint.
type ListServiceOptions struct {
// Limit is the pagination parameter that limits the number of results per
// page. PagerDuty defaults this value to 25 if omitted, and sets an upper
// bound of 100.
Limit uint `url:"limit,omitempty"`
// Offset is the pagination parameter that specifies the offset at which to
// start pagination results. When trying to request the next page of
// results, the new Offset value should be currentOffset + Limit.
Offset uint `url:"offset,omitempty"`
// Total is the pagination parameter to request that the API return the
// total count of items in the response. If this field is omitted or set to
// false, the total number of results will not be sent back from the PagerDuty API.
//
// Setting this to true will slow down the API response times, and so it's
// recommended to omit it unless you've a specific reason for wanting the
// total count of items in the collection.
Total bool `url:"total,omitempty"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
TimeZone string `url:"time_zone,omitempty"`
SortBy string `url:"sort_by,omitempty"`
Query string `url:"query,omitempty"`
Includes []string `url:"include,omitempty,brackets"`
}
// ListServiceResponse is the data structure returned from calling the ListServices API endpoint.
type ListServiceResponse struct {
APIListObject
Services []Service
}
// ListServices lists existing services.
//
// Deprecated: Use ListServicesWithContext instead.
func (c *Client) ListServices(o ListServiceOptions) (*ListServiceResponse, error) {
return c.ListServicesWithContext(context.Background(), o)
}
// ListServicesWithContext lists existing services.
func (c *Client) ListServicesWithContext(ctx context.Context, o ListServiceOptions) (*ListServiceResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/services?"+v.Encode())
if err != nil {
return nil, err
}
var result ListServiceResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// ListServicesPaginated lists existing services processing paginated responses
func (c *Client) ListServicesPaginated(ctx context.Context, o ListServiceOptions) ([]Service, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
var services []Service
responseHandler := func(response *http.Response) (APIListObject, error) {
var result ListServiceResponse
if err := c.decodeJSON(response, &result); err != nil {
return APIListObject{}, err
}
services = append(services, result.Services...)
return APIListObject{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, nil
}
if err := c.pagedGet(ctx, "/services?"+v.Encode(), responseHandler); err != nil {
return nil, err
}
return services, nil
}
// GetServiceOptions is the data structure used when calling the GetService API endpoint.
type GetServiceOptions struct {
Includes []string `url:"include,brackets,omitempty"`
}
// GetService gets details about an existing service.
//
// Deprecated: Use GetServiceWithContext instead.
func (c *Client) GetService(id string, o *GetServiceOptions) (*Service, error) {
return c.GetServiceWithContext(context.Background(), id, o)
}
// GetServiceWithContext gets details about an existing service.
func (c *Client) GetServiceWithContext(ctx context.Context, id string, o *GetServiceOptions) (*Service, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/services/"+id+"?"+v.Encode())
return getServiceFromResponse(c, resp, err)
}
// CreateService creates a new service.
//
// Deprecated: Use CreateServiceWithContext instead.
func (c *Client) CreateService(s Service) (*Service, error) {
return c.CreateServiceWithContext(context.Background(), s)
}
// CreateServiceWithContext creates a new service.
func (c *Client) CreateServiceWithContext(ctx context.Context, s Service) (*Service, error) {
d := map[string]Service{
"service": s,
}
resp, err := c.post(ctx, "/services", d, nil)
return getServiceFromResponse(c, resp, err)
}
// UpdateService updates an existing service.
//
// Deprecated: Use UpdateServiceWithContext instead.
func (c *Client) UpdateService(s Service) (*Service, error) {
return c.UpdateServiceWithContext(context.Background(), s)
}
// UpdateServiceWithContext updates an existing service.
func (c *Client) UpdateServiceWithContext(ctx context.Context, s Service) (*Service, error) {
d := map[string]Service{
"service": s,
}
resp, err := c.put(ctx, "/services/"+s.ID, d, nil)
return getServiceFromResponse(c, resp, err)
}
// DeleteService deletes an existing service.
//
// Deprecated: Use DeleteServiceWithContext instead.
func (c *Client) DeleteService(id string) error {
return c.DeleteServiceWithContext(context.Background(), id)
}
// DeleteServiceWithContext deletes an existing service.
func (c *Client) DeleteServiceWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/services/"+id)
return err
}
// ListServiceRulesPaginated gets all rules for a service.
func (c *Client) ListServiceRulesPaginated(ctx context.Context, serviceID string) ([]ServiceRule, error) {
var rules []ServiceRule
// Create a handler closure capable of parsing data from the Service rules endpoint
// and appending resultant Service rules to the return slice.
responseHandler := func(response *http.Response) (APIListObject, error) {
var result ListServiceRulesResponse
if err := c.decodeJSON(response, &result); err != nil {
return APIListObject{}, err
}
rules = append(rules, result.Rules...)
// Return stats on the current page. Caller can use this information to
// adjust for requesting additional pages.
return APIListObject{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, nil
}
// Make call to get all pages associated with the base endpoint.
if err := c.pagedGet(ctx, "/services/"+serviceID+"/rules", responseHandler); err != nil {
return nil, err
}
return rules, nil
}
// GetServiceRule gets a service rule.
func (c *Client) GetServiceRule(ctx context.Context, serviceID, ruleID string) (ServiceRule, error) {
resp, err := c.get(ctx, "/services/"+serviceID+"/rules/"+ruleID)
return getServiceRuleFromResponse(c, resp, err)
}
// DeleteServiceRule deletes a service rule.
func (c *Client) DeleteServiceRule(ctx context.Context, serviceID, ruleID string) error {
_, err := c.delete(ctx, "/services/"+serviceID+"/rules/"+ruleID)
return err
}
// CreateServiceRule creates a service rule.
func (c *Client) CreateServiceRule(ctx context.Context, serviceID string, rule ServiceRule) (ServiceRule, error) {
d := map[string]ServiceRule{
"rule": rule,
}
resp, err := c.post(ctx, "/services/"+serviceID+"/rules/", d, nil)
return getServiceRuleFromResponse(c, resp, err)
}
// UpdateServiceRule updates a service rule.
func (c *Client) UpdateServiceRule(ctx context.Context, serviceID, ruleID string, rule ServiceRule) (ServiceRule, error) {
d := map[string]ServiceRule{
"rule": rule,
}
resp, err := c.put(ctx, "/services/"+serviceID+"/rules/"+ruleID, d, nil)
return getServiceRuleFromResponse(c, resp, err)
}
func getServiceRuleFromResponse(c *Client, resp *http.Response, err error) (ServiceRule, error) {
if err != nil {
return ServiceRule{}, err
}
var target map[string]ServiceRule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return ServiceRule{}, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "rule"
t, nodeOK := target[rootNode]
if !nodeOK {
return ServiceRule{}, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return t, nil
}
func getServiceFromResponse(c *Client, resp *http.Response, err error) (*Service, error) {
if err != nil {
return nil, err
}
var target map[string]Service
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "service"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
func getIntegrationFromResponse(c *Client, resp *http.Response, err error) (*Integration, error) {
if err != nil {
return nil, err
}
var target map[string]Integration
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", err)
}
const rootNode = "integration"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}