-
Notifications
You must be signed in to change notification settings - Fork 243
/
escalation_policy.go
307 lines (253 loc) · 11.1 KB
/
escalation_policy.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const (
escPath = "/escalation_policies"
)
// EscalationRule is a rule for an escalation policy to trigger.
type EscalationRule struct {
ID string `json:"id,omitempty"`
Delay uint `json:"escalation_delay_in_minutes,omitempty"`
Targets []APIObject `json:"targets"`
}
// EscalationPolicy is a collection of escalation rules.
type EscalationPolicy struct {
APIObject
Name string `json:"name,omitempty"`
EscalationRules []EscalationRule `json:"escalation_rules,omitempty"`
Services []APIObject `json:"services,omitempty"`
NumLoops uint `json:"num_loops,omitempty"`
Teams []APIReference `json:"teams"`
Description string `json:"description,omitempty"`
OnCallHandoffNotifications string `json:"on_call_handoff_notifications,omitempty"`
}
// ListEscalationPoliciesResponse is the data structure returned from calling the ListEscalationPolicies API endpoint.
type ListEscalationPoliciesResponse struct {
APIListObject
EscalationPolicies []EscalationPolicy `json:"escalation_policies"`
}
// ListEscalationRulesResponse represents the data structure returned when
// calling the ListEscalationRules API endpoint.
type ListEscalationRulesResponse struct {
APIListObject
EscalationRules []EscalationRule `json:"escalation_rules"`
}
// ListEscalationPoliciesOptions is the data structure used when calling the ListEscalationPolicies API endpoint.
type ListEscalationPoliciesOptions 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"`
Query string `url:"query,omitempty"`
UserIDs []string `url:"user_ids,omitempty,brackets"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
Includes []string `url:"include,omitempty,brackets"`
SortBy string `url:"sort_by,omitempty"`
}
// GetEscalationRuleOptions is the data structure used when calling the GetEscalationRule API endpoint.
type GetEscalationRuleOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// ListEscalationPolicies lists all of the existing escalation policies.
//
// Deprecated: Use ListEscalationPoliciesWithContext instead.
func (c *Client) ListEscalationPolicies(o ListEscalationPoliciesOptions) (*ListEscalationPoliciesResponse, error) {
return c.ListEscalationPoliciesWithContext(context.Background(), o)
}
// ListEscalationPoliciesWithContext lists all of the existing escalation policies.
func (c *Client) ListEscalationPoliciesWithContext(ctx context.Context, o ListEscalationPoliciesOptions) (*ListEscalationPoliciesResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, escPath+"?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListEscalationPoliciesResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateEscalationPolicy creates a new escalation policy.
//
// Deprecated: Use CreateEscalationPolicyWithContext instead.
func (c *Client) CreateEscalationPolicy(e EscalationPolicy) (*EscalationPolicy, error) {
return c.CreateEscalationPolicyWithContext(context.Background(), e)
}
// CreateEscalationPolicyWithContext creates a new escalation policy.
func (c *Client) CreateEscalationPolicyWithContext(ctx context.Context, e EscalationPolicy) (*EscalationPolicy, error) {
d := map[string]EscalationPolicy{
"escalation_policy": e,
}
resp, err := c.post(ctx, escPath, d, nil)
return getEscalationPolicyFromResponse(c, resp, err)
}
// DeleteEscalationPolicy deletes an existing escalation policy and rules.
//
// Deprecated: Use DeleteEscalationPolicyWithContext instead.
func (c *Client) DeleteEscalationPolicy(id string) error {
return c.DeleteEscalationPolicyWithContext(context.Background(), id)
}
// DeleteEscalationPolicyWithContext deletes an existing escalation policy and rules.
func (c *Client) DeleteEscalationPolicyWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, escPath+"/"+id)
return err
}
// GetEscalationPolicyOptions is the data structure used when calling the GetEscalationPolicy API endpoint.
type GetEscalationPolicyOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// GetEscalationPolicy gets information about an existing escalation policy and
// its rules.
//
// Deprecated: Use GetEscalationPolicyWithContext instead.
func (c *Client) GetEscalationPolicy(id string, o *GetEscalationPolicyOptions) (*EscalationPolicy, error) {
return c.GetEscalationPolicyWithContext(context.Background(), id, o)
}
// GetEscalationPolicyWithContext gets information about an existing escalation
// policy and its rules.
func (c *Client) GetEscalationPolicyWithContext(ctx context.Context, id string, o *GetEscalationPolicyOptions) (*EscalationPolicy, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, escPath+"/"+id+"?"+v.Encode(), nil)
return getEscalationPolicyFromResponse(c, resp, err)
}
// UpdateEscalationPolicy updates an existing escalation policy and its rules.
//
// Deprecated: Use UpdateEscalationPolicyWithContext instead.
func (c *Client) UpdateEscalationPolicy(id string, e *EscalationPolicy) (*EscalationPolicy, error) {
return c.UpdateEscalationPolicyWithContext(context.Background(), id, *e)
}
// UpdateEscalationPolicyWithContext updates an existing escalation policy and its rules.
func (c *Client) UpdateEscalationPolicyWithContext(ctx context.Context, id string, e EscalationPolicy) (*EscalationPolicy, error) {
d := map[string]EscalationPolicy{
"escalation_policy": e,
}
resp, err := c.put(ctx, escPath+"/"+id, d, nil)
return getEscalationPolicyFromResponse(c, resp, err)
}
// CreateEscalationRule creates a new escalation rule for an escalation policy
// and appends it to the end of the existing escalation rules.
//
// Deprecated: Use CreateEscalationRuleWithContext instead.
func (c *Client) CreateEscalationRule(escID string, e EscalationRule) (*EscalationRule, error) {
return c.CreateEscalationRuleWithContext(context.Background(), escID, e)
}
// CreateEscalationRuleWithContext creates a new escalation rule for an escalation policy
// and appends it to the end of the existing escalation rules.
func (c *Client) CreateEscalationRuleWithContext(ctx context.Context, escID string, e EscalationRule) (*EscalationRule, error) {
d := map[string]EscalationRule{
"escalation_rule": e,
}
resp, err := c.post(ctx, escPath+"/"+escID+"/escalation_rules", d, nil)
return getEscalationRuleFromResponse(c, resp, err)
}
// GetEscalationRule gets information about an existing escalation rule.
//
// Deprecated: Use GetEscalationRuleWithContext instead.
func (c *Client) GetEscalationRule(escID string, id string, o *GetEscalationRuleOptions) (*EscalationRule, error) {
return c.GetEscalationRuleWithContext(context.Background(), escID, id, o)
}
// GetEscalationRuleWithContext gets information about an existing escalation rule.
func (c *Client) GetEscalationRuleWithContext(ctx context.Context, escID string, id string, o *GetEscalationRuleOptions) (*EscalationRule, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, escPath+"/"+escID+"/escalation_rules/"+id+"?"+v.Encode(), nil)
return getEscalationRuleFromResponse(c, resp, err)
}
// DeleteEscalationRule deletes an existing escalation rule.
//
// Deprecated: Use DeleteEscalationRuleWithContext instead.
func (c *Client) DeleteEscalationRule(escID string, id string) error {
return c.DeleteEscalationRuleWithContext(context.Background(), escID, id)
}
// DeleteEscalationRuleWithContext deletes an existing escalation rule.
func (c *Client) DeleteEscalationRuleWithContext(ctx context.Context, escID string, id string) error {
_, err := c.delete(ctx, escPath+"/"+escID+"/escalation_rules/"+id)
return err
}
// UpdateEscalationRule updates an existing escalation rule.
//
// Deprecated: Use UpdateEscalationRuleWithContext instead.
func (c *Client) UpdateEscalationRule(escID string, id string, e *EscalationRule) (*EscalationRule, error) {
return c.UpdateEscalationRuleWithContext(context.Background(), escID, id, *e)
}
// UpdateEscalationRuleWithContext updates an existing escalation rule.
func (c *Client) UpdateEscalationRuleWithContext(ctx context.Context, escID string, id string, e EscalationRule) (*EscalationRule, error) {
d := map[string]EscalationRule{
"escalation_rule": e,
}
resp, err := c.put(ctx, escPath+"/"+escID+"/escalation_rules/"+id, d, nil)
return getEscalationRuleFromResponse(c, resp, err)
}
// ListEscalationRules lists all of the escalation rules for an existing
// escalation policy.
//
// Deprecated: Use ListEscalationRulesWithContext instead.
func (c *Client) ListEscalationRules(escID string) (*ListEscalationRulesResponse, error) {
return c.ListEscalationRulesWithContext(context.Background(), escID)
}
// ListEscalationRulesWithContext lists all of the escalation rules for an existing escalation policy.
func (c *Client) ListEscalationRulesWithContext(ctx context.Context, escID string) (*ListEscalationRulesResponse, error) {
resp, err := c.get(ctx, escPath+"/"+escID+"/escalation_rules", nil)
if err != nil {
return nil, err
}
var result ListEscalationRulesResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
func getEscalationRuleFromResponse(c *Client, resp *http.Response, err error) (*EscalationRule, error) {
if err != nil {
return nil, err
}
var target map[string]EscalationRule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "escalation_rule"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
func getEscalationPolicyFromResponse(c *Client, resp *http.Response, err error) (*EscalationPolicy, error) {
if err != nil {
return nil, err
}
var target map[string]EscalationPolicy
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "escalation_policy"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}