forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
409 lines (336 loc) · 14.8 KB
/
user.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
398
399
400
401
402
403
404
405
406
407
408
409
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// NotificationRule is a rule for notifying the user.
type NotificationRule struct {
ID string `json:"id"`
StartDelayInMinutes uint `json:"start_delay_in_minutes"`
CreatedAt string `json:"created_at"`
ContactMethod ContactMethod `json:"contact_method"`
Urgency string `json:"urgency"`
Type string `json:"type"`
}
// User is a member of a PagerDuty account that has the ability to interact with incidents and other data on the account.
type User struct {
APIObject
Type string `json:"type"`
Name string `json:"name"`
Summary string `json:"summary"`
Email string `json:"email"`
Timezone string `json:"time_zone,omitempty"`
Color string `json:"color,omitempty"`
Role string `json:"role,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Description string `json:"description,omitempty"`
InvitationSent bool `json:"invitation_sent,omitempty"`
ContactMethods []ContactMethod `json:"contact_methods"`
NotificationRules []NotificationRule `json:"notification_rules"`
JobTitle string `json:"job_title,omitempty"`
Teams []Team
}
// ContactMethod is a way of contacting the user.
type ContactMethod struct {
ID string `json:"id"`
Type string `json:"type"`
Summary string `json:"summary"`
Self string `json:"self"`
Label string `json:"label"`
Address string `json:"address"`
SendShortEmail bool `json:"send_short_email,omitempty"`
SendHTMLEmail bool `json:"send_html_email,omitempty"`
Blacklisted bool `json:"blacklisted,omitempty"`
CountryCode int `json:"country_code,omitempty"`
Enabled bool `json:"enabled,omitempty"`
HTMLUrl string `json:"html_url"`
}
// ListUsersResponse is the data structure returned from calling the ListUsers API endpoint.
type ListUsersResponse struct {
APIListObject
Users []User
}
// ListUsersOptions is the data structure used when calling the ListUsers API endpoint.
type ListUsersOptions struct {
APIListObject
Query string `url:"query,omitempty"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
Includes []string `url:"include,omitempty,brackets"`
}
// ListContactMethodsResponse is the data structure returned from calling the GetUserContactMethod API endpoint.
type ListContactMethodsResponse struct {
APIListObject
ContactMethods []ContactMethod `json:"contact_methods"`
}
// ListUserNotificationRulesResponse the data structure returned from calling the ListNotificationRules API endpoint.
type ListUserNotificationRulesResponse struct {
APIListObject
NotificationRules []NotificationRule `json:"notification_rules"`
}
// GetUserOptions is the data structure used when calling the GetUser API endpoint.
type GetUserOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// GetCurrentUserOptions is the data structure used when calling the GetCurrentUser API endpoint.
type GetCurrentUserOptions struct {
Includes []string `url:"include,omitempty,brackets"`
}
// ListUsers lists users of your PagerDuty account, optionally filtered by a
// search query. It's recommended to use ListUsersWithContext instead.
func (c *Client) ListUsers(o ListUsersOptions) (*ListUsersResponse, error) {
return c.ListUsersWithContext(context.Background(), o)
}
// ListUsersWithContext lists users of your PagerDuty account, optionally filtered by a search query.
func (c *Client) ListUsersWithContext(ctx context.Context, o ListUsersOptions) (*ListUsersResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/users?"+v.Encode())
if err != nil {
return nil, err
}
var result ListUsersResponse
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateUser creates a new user. It's recommended to use CreateUserWithContext
// instead.
func (c *Client) CreateUser(u User) (*User, error) {
return c.CreateUserWithContext(context.Background(), u)
}
// CreateUserWithContext creates a new user.
func (c *Client) CreateUserWithContext(ctx context.Context, u User) (*User, error) {
d := map[string]User{
"user": u,
}
resp, err := c.post(ctx, "/users", d, nil)
return getUserFromResponse(c, resp, err)
}
// DeleteUser deletes a user. It's recommended to use DeleteUserWithContext
// instead.
func (c *Client) DeleteUser(id string) error {
return c.DeleteUserWithContext(context.Background(), id)
}
// DeleteUserWithContext deletes a user.
func (c *Client) DeleteUserWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/users/"+id)
return err
}
// GetUser gets details about an existing user. It's recommended to use
// GetUserWithContext instead.
func (c *Client) GetUser(id string, o GetUserOptions) (*User, error) {
return c.GetUserWithContext(context.Background(), id, o)
}
// GetUserWithContext gets details about an existing user.
func (c *Client) GetUserWithContext(ctx context.Context, id string, o GetUserOptions) (*User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/users/"+id+"?"+v.Encode())
return getUserFromResponse(c, resp, err)
}
// UpdateUser updates an existing user. It's recommended to use
// UpdateUserWithContext instead.
func (c *Client) UpdateUser(u User) (*User, error) {
return c.UpdateUserWithContext(context.Background(), u)
}
// UpdateUserWithContext updates an existing user.
func (c *Client) UpdateUserWithContext(ctx context.Context, u User) (*User, error) {
d := map[string]User{
"user": u,
}
resp, err := c.put(ctx, "/users/"+u.ID, d, nil)
return getUserFromResponse(c, resp, err)
}
// GetCurrentUser gets details about the authenticated user when using a
// user-level API key or OAuth token. It's recommended to use
// GetCurrentUserWithContext instead.
func (c *Client) GetCurrentUser(o GetCurrentUserOptions) (*User, error) {
return c.GetCurrentUserWithContext(context.Background(), o)
}
// GetCurrentUserWithContext gets details about the authenticated user when
// using a user-level API key or OAuth token.
func (c *Client) GetCurrentUserWithContext(ctx context.Context, o GetCurrentUserOptions) (*User, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/users/me?"+v.Encode())
return getUserFromResponse(c, resp, err)
}
func getUserFromResponse(c *Client, resp *http.Response, err error) (*User, error) {
if err != nil {
return nil, err
}
var target map[string]User
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "user"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
// ListUserContactMethods fetches contact methods of the existing user. It's
// recommended to use ListUserContactMethodsWithContext instead.
func (c *Client) ListUserContactMethods(userID string) (*ListContactMethodsResponse, error) {
return c.ListUserContactMethodsWithContext(context.Background(), userID)
}
// ListUserContactMethodsWithContext fetches contact methods of the existing user.
func (c *Client) ListUserContactMethodsWithContext(ctx context.Context, userID string) (*ListContactMethodsResponse, error) {
resp, err := c.get(ctx, "/users/"+userID+"/contact_methods")
if err != nil {
return nil, err
}
var result ListContactMethodsResponse
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetUserContactMethod gets details about a contact method. It's recommended to
// use GetUserContactMethodWithContext instead.
func (c *Client) GetUserContactMethod(userID, contactMethodID string) (*ContactMethod, error) {
return c.GetUserContactMethodWithContext(context.Background(), userID, contactMethodID)
}
// GetUserContactMethodWithContext gets details about a contact method.
func (c *Client) GetUserContactMethodWithContext(ctx context.Context, userID, contactMethodID string) (*ContactMethod, error) {
resp, err := c.get(ctx, "/users/"+userID+"/contact_methods/"+contactMethodID)
return getContactMethodFromResponse(c, resp, err)
}
// DeleteUserContactMethod deletes a user. It's recommended to use
// DeleteUserContactMethodWithContext instead.
func (c *Client) DeleteUserContactMethod(userID, contactMethodID string) error {
return c.DeleteUserContactMethodWithContext(context.Background(), userID, contactMethodID)
}
// DeleteUserContactMethodWithContext deletes a user.
func (c *Client) DeleteUserContactMethodWithContext(ctx context.Context, userID, contactMethodID string) error {
_, err := c.delete(ctx, "/users/"+userID+"/contact_methods/"+contactMethodID)
return err
}
// CreateUserContactMethod creates a new contact method for user. It's
// recommended to use CreateUserContactMethodWithContext instead.
func (c *Client) CreateUserContactMethod(userID string, cm ContactMethod) (*ContactMethod, error) {
return c.CreateUserContactMethodWithContext(context.Background(), userID, cm)
}
// CreateUserContactMethodWithContext creates a new contact method for user.
func (c *Client) CreateUserContactMethodWithContext(ctx context.Context, userID string, cm ContactMethod) (*ContactMethod, error) {
d := map[string]ContactMethod{
"contact_method": cm,
}
resp, err := c.post(ctx, "/users/"+userID+"/contact_methods", d, nil)
return getContactMethodFromResponse(c, resp, err)
}
// UpdateUserContactMethod updates an existing user. It's recommended to use
// UpdateUserContactMethodWithContext instead.
func (c *Client) UpdateUserContactMethod(userID string, cm ContactMethod) (*ContactMethod, error) {
return c.UpdateUserContactMethodWthContext(context.Background(), userID, cm)
}
// UpdateUserContactMethodWthContext updates an existing user.
func (c *Client) UpdateUserContactMethodWthContext(ctx context.Context, userID string, cm ContactMethod) (*ContactMethod, error) {
d := map[string]ContactMethod{
"contact_method": cm,
}
resp, err := c.put(ctx, "/users/"+userID+"/contact_methods/"+cm.ID, d, nil)
return getContactMethodFromResponse(c, resp, err)
}
func getContactMethodFromResponse(c *Client, resp *http.Response, err error) (*ContactMethod, error) {
if err != nil {
return nil, err
}
var target map[string]ContactMethod
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "contact_method"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
// GetUserNotificationRule gets details about a notification rule. It's
// recommended to use GetUserNotificationRuleWithContext instead.
func (c *Client) GetUserNotificationRule(userID, ruleID string) (*NotificationRule, error) {
return c.GetUserNotificationRuleWithContext(context.Background(), userID, ruleID)
}
// GetUserNotificationRuleWithContext gets details about a notification rule.
func (c *Client) GetUserNotificationRuleWithContext(ctx context.Context, userID, ruleID string) (*NotificationRule, error) {
resp, err := c.get(ctx, "/users/"+userID+"/notification_rules/"+ruleID)
return getUserNotificationRuleFromResponse(c, resp, err)
}
// CreateUserNotificationRule creates a new notification rule for a user. It's
// recommended to use CreateUserNotificationRuleWithContext instead.
func (c *Client) CreateUserNotificationRule(userID string, rule NotificationRule) (*NotificationRule, error) {
return c.CreateUserNotificationRuleWithContext(context.Background(), userID, rule)
}
// CreateUserNotificationRuleWithContext creates a new notification rule for a user.
func (c *Client) CreateUserNotificationRuleWithContext(ctx context.Context, userID string, rule NotificationRule) (*NotificationRule, error) {
d := map[string]NotificationRule{
"notification_rule": rule,
}
resp, err := c.post(ctx, "/users/"+userID+"/notification_rules", d, nil)
return getUserNotificationRuleFromResponse(c, resp, err)
}
// UpdateUserNotificationRule updates a notification rule for a user. It's
// recommended to use UpdateUserNotificationRuleWithContext instead.
func (c *Client) UpdateUserNotificationRule(userID string, rule NotificationRule) (*NotificationRule, error) {
return c.UpdateUserNotificationRuleWithContext(context.Background(), userID, rule)
}
// UpdateUserNotificationRuleWithContext updates a notification rule for a user.
func (c *Client) UpdateUserNotificationRuleWithContext(ctx context.Context, userID string, rule NotificationRule) (*NotificationRule, error) {
d := map[string]NotificationRule{
"notification_rule": rule,
}
resp, err := c.put(ctx, "/users/"+userID+"/notification_rules/"+rule.ID, d, nil)
return getUserNotificationRuleFromResponse(c, resp, err)
}
// DeleteUserNotificationRule deletes a notification rule for a user. It's
// recommended to use DeleteUserNotificationRuleWithContext instead.
func (c *Client) DeleteUserNotificationRule(userID, ruleID string) error {
return c.DeleteUserNotificationRuleWithContext(context.Background(), userID, ruleID)
}
// DeleteUserNotificationRuleWithContext deletes a notification rule for a user.
func (c *Client) DeleteUserNotificationRuleWithContext(ctx context.Context, userID, ruleID string) error {
_, err := c.delete(ctx, "/users/"+userID+"/notification_rules/"+ruleID)
return err
}
// ListUserNotificationRules fetches notification rules of the existing user.
func (c *Client) ListUserNotificationRules(userID string) (*ListUserNotificationRulesResponse, error) {
return c.ListUserNotificationRulesWithContext(context.Background(), userID)
}
// ListUserNotificationRulesWithContext fetches notification rules of the existing user.
func (c *Client) ListUserNotificationRulesWithContext(ctx context.Context, userID string) (*ListUserNotificationRulesResponse, error) {
resp, err := c.get(ctx, "/users/"+userID+"/notification_rules")
if err != nil {
return nil, err
}
var result ListUserNotificationRulesResponse
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
func getUserNotificationRuleFromResponse(c *Client, resp *http.Response, err error) (*NotificationRule, error) {
if err != nil {
return nil, err
}
var target map[string]NotificationRule
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "notification_rule"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}