-
Notifications
You must be signed in to change notification settings - Fork 243
/
maintenance_window.go
191 lines (159 loc) · 7 KB
/
maintenance_window.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// MaintenanceWindow is used to temporarily disable one or more services for a set period of time.
type MaintenanceWindow struct {
APIObject
SequenceNumber uint `json:"sequence_number,omitempty"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Description string `json:"description"`
Services []APIObject `json:"services"`
Teams []APIObject `json:"teams,omitempty"`
CreatedBy *APIObject `json:"created_by,omitempty"`
}
// ListMaintenanceWindowsResponse is the data structur returned from calling the ListMaintenanceWindows API endpoint.
type ListMaintenanceWindowsResponse struct {
APIListObject
MaintenanceWindows []MaintenanceWindow `json:"maintenance_windows"`
}
// ListMaintenanceWindowsOptions is the data structure used when calling the ListMaintenanceWindows API endpoint.
type ListMaintenanceWindowsOptions 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"`
Includes []string `url:"include,omitempty,brackets"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
Filter string `url:"filter,omitempty,brackets"`
}
// ListMaintenanceWindows lists existing maintenance windows, optionally
// filtered by service and/or team, or whether they are from the past, present
// or future.
//
// Deprecated: Use ListMaintenanceWindowsWithContext instead.
func (c *Client) ListMaintenanceWindows(o ListMaintenanceWindowsOptions) (*ListMaintenanceWindowsResponse, error) {
return c.ListMaintenanceWindowsWithContext(context.Background(), o)
}
// ListMaintenanceWindowsWithContext lists existing maintenance windows,
// optionally filtered by service and/or team, or whether they are from the
// past, present or future.
func (c *Client) ListMaintenanceWindowsWithContext(ctx context.Context, o ListMaintenanceWindowsOptions) (*ListMaintenanceWindowsResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/maintenance_windows?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListMaintenanceWindowsResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateMaintenanceWindow creates a new maintenance window for the specified
// services.
//
// Deprecated: Use CreateMaintenanceWindowWithContext instead.
func (c *Client) CreateMaintenanceWindow(from string, o MaintenanceWindow) (*MaintenanceWindow, error) {
return c.CreateMaintenanceWindowWithContext(context.Background(), from, o)
}
// CreateMaintenanceWindowWithContext creates a new maintenance window for the specified services.
func (c *Client) CreateMaintenanceWindowWithContext(ctx context.Context, from string, o MaintenanceWindow) (*MaintenanceWindow, error) {
o.Type = "maintenance_window"
d := map[string]MaintenanceWindow{
"maintenance_window": o,
}
var h map[string]string
if from != "" {
h = map[string]string{
"From": from,
}
}
resp, err := c.post(ctx, "/maintenance_windows", d, h)
return getMaintenanceWindowFromResponse(c, resp, err)
}
// CreateMaintenanceWindows creates a new maintenance window for the specified services.
//
// Deprecated: Use CreateMaintenanceWindowWithContext instead.
func (c *Client) CreateMaintenanceWindows(o MaintenanceWindow) (*MaintenanceWindow, error) {
return c.CreateMaintenanceWindowWithContext(context.Background(), "", o)
}
// DeleteMaintenanceWindow deletes an existing maintenance window if it's in the
// future, or ends it if it's currently on-going.
//
// Deprecated: Use DeleteMaintenanceWindowWithContext instead.
func (c *Client) DeleteMaintenanceWindow(id string) error {
return c.DeleteMaintenanceWindowWithContext(context.Background(), id)
}
// DeleteMaintenanceWindowWithContext deletes an existing maintenance window if it's in the
// future, or ends it if it's currently on-going.
func (c *Client) DeleteMaintenanceWindowWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/maintenance_windows/"+id)
return err
}
// GetMaintenanceWindowOptions is the data structure used when calling the GetMaintenanceWindow API endpoint.
type GetMaintenanceWindowOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// GetMaintenanceWindow gets an existing maintenance window.
//
// Deprecated: Use GetMaintenanceWindowWithContext instead.
func (c *Client) GetMaintenanceWindow(id string, o GetMaintenanceWindowOptions) (*MaintenanceWindow, error) {
return c.GetMaintenanceWindowWithContext(context.Background(), id, o)
}
// GetMaintenanceWindowWithContext gets an existing maintenance window.
func (c *Client) GetMaintenanceWindowWithContext(ctx context.Context, id string, o GetMaintenanceWindowOptions) (*MaintenanceWindow, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/maintenance_windows/"+id+"?"+v.Encode(), nil)
return getMaintenanceWindowFromResponse(c, resp, err)
}
// UpdateMaintenanceWindow updates an existing maintenance window.
//
// Deprecated: Use UpdateMaintenanceWindowWithContext instead.
func (c *Client) UpdateMaintenanceWindow(m MaintenanceWindow) (*MaintenanceWindow, error) {
return c.UpdateMaintenanceWindowWithContext(context.Background(), m)
}
// UpdateMaintenanceWindowWithContext updates an existing maintenance window.
func (c *Client) UpdateMaintenanceWindowWithContext(ctx context.Context, m MaintenanceWindow) (*MaintenanceWindow, error) {
resp, err := c.put(ctx, "/maintenance_windows/"+m.ID, m, nil)
return getMaintenanceWindowFromResponse(c, resp, err)
}
func getMaintenanceWindowFromResponse(c *Client, resp *http.Response, err error) (*MaintenanceWindow, error) {
if err != nil {
return nil, err
}
var target map[string]MaintenanceWindow
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "maintenance_window"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}