forked from heimweh/go-pagerduty
/
pagerduty.go
286 lines (242 loc) · 7.3 KB
/
pagerduty.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
package pagerduty
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
const (
defaultBaseURL = "https://api.pagerduty.com"
)
type service struct {
client *Client
}
// Config represents the configuration for a PagerDuty client
type Config struct {
BaseURL string
HTTPClient *http.Client
Token string
UserAgent string
Debug bool
}
// Client manages the communication with the PagerDuty API
type Client struct {
baseURL *url.URL
client *http.Client
Config *Config
Abilities *AbilityService
Addons *AddonService
EscalationPolicies *EscalationPolicyService
Extensions *ExtensionService
MaintenanceWindows *MaintenanceWindowService
Rulesets *RulesetService
Schedules *ScheduleService
Services *ServicesService
Teams *TeamService
ExtensionSchemas *ExtensionSchemaService
Users *UserService
Vendors *VendorService
EventRules *EventRuleService
BusinessServices *BusinessServiceService
ServiceDependencies *ServiceDependencyService
Priorities *PriorityService
ResponsePlays *ResponsePlayService
}
// Response is a wrapper around http.Response
type Response struct {
Response *http.Response
BodyBytes []byte
}
// RequestOptions is an object to setting options for HTTP requests
type RequestOptions struct {
Type string
Label string
Value string
}
// NewClient returns a new PagerDuty API client.
func NewClient(config *Config) (*Client, error) {
if config.HTTPClient == nil {
config.HTTPClient = http.DefaultClient
}
if config.BaseURL == "" {
config.BaseURL = defaultBaseURL
}
config.UserAgent = "heimweh/go-pagerduty(terraform)"
baseURL, err := url.Parse(config.BaseURL)
if err != nil {
return nil, err
}
c := &Client{
baseURL: baseURL,
client: config.HTTPClient,
Config: config,
}
c.Abilities = &AbilityService{c}
c.Addons = &AddonService{c}
c.EscalationPolicies = &EscalationPolicyService{c}
c.MaintenanceWindows = &MaintenanceWindowService{c}
c.Rulesets = &RulesetService{c}
c.Schedules = &ScheduleService{c}
c.Services = &ServicesService{c}
c.Teams = &TeamService{c}
c.Users = &UserService{c}
c.Vendors = &VendorService{c}
c.Extensions = &ExtensionService{c}
c.ExtensionSchemas = &ExtensionSchemaService{c}
c.EventRules = &EventRuleService{c}
c.BusinessServices = &BusinessServiceService{c}
c.ServiceDependencies = &ServiceDependencyService{c}
c.Priorities = &PriorityService{c}
c.ResponsePlays = &ResponsePlayService{c}
return c, nil
}
func (c *Client) newRequest(method, url string, body interface{}, options ...RequestOptions) (*http.Request, error) {
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
if c.Config.Debug {
log.Printf("[DEBUG] PagerDuty - Preparing %s request to %s with body: %s", method, url, buf)
}
u := c.baseURL.String() + url
req, err := http.NewRequest(method, u, buf)
if err != nil {
return nil, err
}
if len(options) > 0 {
for _, o := range options {
if o.Type == "header" {
req.Header.Add(o.Label, o.Value)
}
}
}
req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", c.Config.Token))
req.Header.Add("Content-Type", "application/json")
if c.Config.UserAgent != "" {
req.Header.Add("User-Agent", c.Config.UserAgent)
}
return req, nil
}
func (c *Client) newRequestDo(method, url string, qryOptions, body, v interface{}) (*Response, error) {
if qryOptions != nil {
values, err := query.Values(qryOptions)
if err != nil {
return nil, err
}
if v := values.Encode(); v != "" {
url = fmt.Sprintf("%s?%s", url, v)
}
}
req, err := c.newRequest(method, url, body)
if err != nil {
return nil, err
}
return c.do(req, v)
}
func (c *Client) newRequestDoOptions(method, url string, qryOptions, body, v interface{}, reqOptions ...RequestOptions) (*Response, error) {
if qryOptions != nil {
values, err := query.Values(qryOptions)
if err != nil {
return nil, err
}
if v := values.Encode(); v != "" {
url = fmt.Sprintf("%s?%s", url, v)
}
}
req, err := c.newRequest(method, url, body, reqOptions...)
if err != nil {
return nil, err
}
return c.do(req, v)
}
func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
response := &Response{
Response: resp,
BodyBytes: bodyBytes,
}
if err := c.checkResponse(response); err != nil {
return response, err
}
if v != nil {
if err := c.DecodeJSON(response, v); err != nil {
return response, err
}
}
return response, nil
}
// ListResp represents a list response from the PagerDuty API
type ListResp struct {
Offset int `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
More bool `json:"more,omitempty"`
Total int `json:"total,omitempty"`
}
// responseHandler is capable of parsing a response. At a minimum it must
// extract the page information for the current page. It can also execute
// additional necessary handling; for example, if a closure, it has access
// to the scope in which it was defined, and can be used to append data to
// a specific slice. The responseHandler is responsible for closing the response.
type responseHandler func(response *Response) (ListResp, *Response, error)
func (c *Client) newRequestPagedGetDo(basePath string, handler responseHandler, reqOptions ...RequestOptions) error {
// Indicates whether there are still additional pages associated with request.
var stillMore bool
// Offset to set for the next page request.
var nextOffset int
// While there are more pages, keep adjusting the offset to get all results.
for stillMore, nextOffset = true, 0; stillMore; {
response, err := c.newRequestDoOptions("GET", fmt.Sprintf("%s?offset=%d", basePath, nextOffset), nil, nil, nil, reqOptions...)
if err != nil {
return err
}
// Call handler to extract page information and execute additional necessary handling.
pageInfo, _, err := handler(response)
if err != nil {
return err
}
// Bump the offset as necessary and set whether more results exist.
nextOffset = pageInfo.Offset + pageInfo.Limit
stillMore = pageInfo.More
}
return nil
}
// ValidateAuth validates a token against the PagerDuty API
func (c *Client) ValidateAuth() error {
_, _, err := c.Abilities.List()
return err
}
// DecodeJSON decodes json body to given interface
func (c *Client) DecodeJSON(res *Response, v interface{}) error {
return json.Unmarshal(res.BodyBytes, v)
}
func (c *Client) checkResponse(res *Response) error {
if res.Response.StatusCode >= 200 && res.Response.StatusCode <= 299 {
return nil
}
return c.decodeErrorResponse(res)
}
func (c *Client) decodeErrorResponse(res *Response) error {
// Try to decode error response or fallback with standard error
v := &errorResponse{Error: &Error{ErrorResponse: res}}
if err := c.DecodeJSON(res, v); err != nil {
return fmt.Errorf("%s API call to %s failed: %v", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status)
}
return v.Error
}