forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
264 lines (237 loc) · 8.61 KB
/
schedule.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
package pagerduty
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// Restriction limits on-call responsibility for a layer to certain times of the day or week.
type Restriction struct {
Type string `json:"type,omitempty"`
StartTimeOfDay string `json:"start_time_of_day,omitempty"`
StartDayOfWeek uint `json:"start_day_of_week,omitempty"`
DurationSeconds uint `json:"duration_seconds,omitempty"`
}
// RenderedScheduleEntry represents the computed set of schedule layer entries that put users on call for a schedule, and cannot be modified directly.
type RenderedScheduleEntry struct {
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
User APIObject `json:"user,omitempty"`
}
// ScheduleLayer is an entry that puts users on call for a schedule.
type ScheduleLayer struct {
APIObject
Name string `json:"name,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
RotationVirtualStart string `json:"rotation_virtual_start,omitempty"`
RotationTurnLengthSeconds uint `json:"rotation_turn_length_seconds,omitempty"`
Users []UserReference `json:"users,omitempty"`
Restrictions []Restriction `json:"restrictions,omitempty"`
RenderedScheduleEntries []RenderedScheduleEntry `json:"rendered_schedule_entries,omitempty"`
RenderedCoveragePercentage float64 `json:"rendered_coverage_percentage,omitempty"`
}
// Schedule determines the time periods that users are on call.
type Schedule struct {
APIObject
Name string `json:"name,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Description string `json:"description,omitempty"`
EscalationPolicies []APIObject `json:"escalation_policies,omitempty"`
Users []APIObject `json:"users,omitempty"`
ScheduleLayers []ScheduleLayer `json:"schedule_layers,omitempty"`
OverrideSubschedule ScheduleLayer `json:"override_subschedule,omitempty"`
FinalSchedule ScheduleLayer `json:"final_schedule,omitempty"`
}
// ListSchedulesOptions is the data structure used when calling the ListSchedules API endpoint.
type ListSchedulesOptions struct {
APIListObject
Query string `url:"query,omitempty"`
}
// ListSchedulesResponse is the data structure returned from calling the ListSchedules API endpoint.
type ListSchedulesResponse struct {
APIListObject
Schedules []Schedule `json:"schedules"`
}
// UserReference is a reference to an authorized PagerDuty user.
type UserReference struct {
User APIObject `json:"user"`
}
// ListSchedules lists the on-call schedules.
func (c *Client) ListSchedules(o ListSchedulesOptions) (*ListSchedulesResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/schedules?" + v.Encode())
if err != nil {
return nil, err
}
var result ListSchedulesResponse
return &result, c.decodeJSON(resp, &result)
}
// CreateSchedule creates a new on-call schedule.
func (c *Client) CreateSchedule(s Schedule) (*Schedule, error) {
data := make(map[string]Schedule)
data["schedule"] = s
resp, err := c.post("/schedules", data, nil)
if err != nil {
return nil, err
}
return getScheduleFromResponse(c, resp)
}
// PreviewScheduleOptions is the data structure used when calling the PreviewSchedule API endpoint.
type PreviewScheduleOptions struct {
APIListObject
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Overflow bool `url:"overflow,omitempty"`
}
// PreviewSchedule previews what an on-call schedule would look like without saving it.
func (c *Client) PreviewSchedule(s Schedule, o PreviewScheduleOptions) error {
v, err := query.Values(o)
if err != nil {
return err
}
var data map[string]Schedule
data["schedule"] = s
_, e := c.post("/schedules/preview?"+v.Encode(), data, nil)
return e
}
// DeleteSchedule deletes an on-call schedule.
func (c *Client) DeleteSchedule(id string) error {
_, err := c.delete("/schedules/" + id)
return err
}
// GetScheduleOptions is the data structure used when calling the GetSchedule API endpoint.
type GetScheduleOptions struct {
APIListObject
TimeZone string `url:"time_zone,omitempty"`
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
}
// GetSchedule shows detailed information about a schedule, including entries for each layer and sub-schedule.
func (c *Client) GetSchedule(id string, o GetScheduleOptions) (*Schedule, error) {
v, err := query.Values(o)
if err != nil {
return nil, fmt.Errorf("Could not parse values for query: %v", err)
}
resp, err := c.get("/schedules/" + id + "?" + v.Encode())
if err != nil {
return nil, err
}
return getScheduleFromResponse(c, resp)
}
// UpdateScheduleOptions is the data structure used when calling the UpdateSchedule API endpoint.
type UpdateScheduleOptions struct {
Overflow bool `url:"overflow,omitempty"`
}
// UpdateSchedule updates an existing on-call schedule.
func (c *Client) UpdateSchedule(id string, s Schedule) (*Schedule, error) {
v := make(map[string]Schedule)
v["schedule"] = s
resp, err := c.put("/schedules/"+id, v, nil)
if err != nil {
return nil, err
}
return getScheduleFromResponse(c, resp)
}
// ListOverridesOptions is the data structure used when calling the ListOverrides API endpoint.
type ListOverridesOptions struct {
APIListObject
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
Editable bool `url:"editable,omitempty"`
Overflow bool `url:"overflow,omitempty"`
}
// Overrides are any schedule layers from the override layer.
type Override struct {
ID string `json:"id,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
User APIObject `json:"user,omitempty"`
}
// ListOverrides lists overrides for a given time range.
func (c *Client) ListOverrides(id string, o ListOverridesOptions) ([]Override, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/schedules/" + id + "/overrides?" + v.Encode())
if err != nil {
return nil, err
}
var result map[string][]Override
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
overrides, ok := result["overrides"]
if !ok {
return nil, fmt.Errorf("JSON response does not have overrides field")
}
return overrides, nil
}
// CreateOverride creates an override for a specific user covering the specified time range.
func (c *Client) CreateOverride(id string, o Override) (*Override, error) {
data := make(map[string]Override)
data["override"] = o
resp, err := c.post("/schedules/"+id+"/overrides", data, nil)
if err != nil {
return nil, err
}
return getOverrideFromResponse(c, resp)
}
// DeleteOverride removes an override.
func (c *Client) DeleteOverride(scheduleID, overrideID string) error {
_, err := c.delete("/schedules/" + scheduleID + "/overrides/" + overrideID)
return err
}
// ListOnCallUsersOptions is the data structure used when calling the ListOnCallUsers API endpoint.
type ListOnCallUsersOptions struct {
APIListObject
Since string `url:"since,omitempty"`
Until string `url:"until,omitempty"`
}
// ListOnCallUsers lists all of the users on call in a given schedule for a given time range.
func (c *Client) ListOnCallUsers(id string, o ListOnCallUsersOptions) ([]User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/schedules/" + id + "/users?" + v.Encode())
if err != nil {
return nil, err
}
var result map[string][]User
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
u, ok := result["users"]
if !ok {
return nil, fmt.Errorf("JSON response does not have users field")
}
return u, nil
}
func getScheduleFromResponse(c *Client, resp *http.Response) (*Schedule, error) {
var target map[string]Schedule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "schedule"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
func getOverrideFromResponse(c *Client, resp *http.Response) (*Override, error) {
var target map[string]Override
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "override"
o, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &o, nil
}