forked from heimweh/go-pagerduty
/
user.go
382 lines (314 loc) · 13.3 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
package pagerduty
import (
"fmt"
"log"
)
// UserService handles the communication with user
// related methods of the PagerDuty API.
type UserService service
// NotificationRule represents a user notification rule.
type NotificationRule struct {
NotificationRule *NotificationRule `json:"notification_rule,omitempty"`
ContactMethod *ContactMethodReference `json:"contact_method,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
ID string `json:"id,omitempty"`
Self string `json:"self,omitempty"`
StartDelayInMinutes int `json:"start_delay_in_minutes"`
Summary string `json:"summary,omitempty"`
Type string `json:"type,omitempty"`
Urgency string `json:"urgency,omitempty"`
}
// User represents a user.
type User struct {
AvatarURL string `json:"avatar_url,omitempty"`
Color string `json:"color,omitempty"`
ContactMethods []*ContactMethodReference `json:"contact_methods,omitempty"`
Description string `json:"description,omitempty"`
Email string `json:"email,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
ID string `json:"id,omitempty"`
InvitationSent bool `json:"invitation_sent,omitempty"`
JobTitle string `json:"job_title,omitempty"`
Name string `json:"name,omitempty"`
NotificationRules []*NotificationRule `json:"notification_rules,omitempty"`
Role string `json:"role,omitempty"`
Self string `json:"self,omitempty"`
Summary string `json:"summary,omitempty"`
Teams []*TeamReference `json:"teams,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Type string `json:"type,omitempty"`
User *User `json:"user,omitempty"`
}
// User represents a user.
type FullUser struct {
AvatarURL string `json:"avatar_url,omitempty"`
Color string `json:"color,omitempty"`
ContactMethods []*ContactMethod `json:"contact_methods,omitempty"`
Description string `json:"description,omitempty"`
Email string `json:"email,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
ID string `json:"id,omitempty"`
InvitationSent bool `json:"invitation_sent,omitempty"`
JobTitle string `json:"job_title,omitempty"`
Name string `json:"name,omitempty"`
NotificationRules []*NotificationRule `json:"notification_rules,omitempty"`
Role string `json:"role,omitempty"`
Self string `json:"self,omitempty"`
Summary string `json:"summary,omitempty"`
Teams []*Team `json:"teams,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
Type string `json:"type,omitempty"`
User *FullUser `json:"user,omitempty"`
}
// ContactMethod represents a contact method for a user.
type ContactMethod struct {
ContactMethod *ContactMethod `json:"contact_method,omitempty"`
ID string `json:"id,omitempty"`
Summary string `json:"summary,omitempty"`
Type string `json:"type,omitempty"`
Self string `json:"self,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
Label string `json:"label,omitempty"`
Address string `json:"address,omitempty"`
BlackListed bool `json:"blacklisted,omitempty"`
// Email contact method options
SendShortEmail bool `json:"send_short_email,omitempty"`
// Phone contact method options
CountryCode int `json:"country_code,omitempty"`
Enabled bool `json:"enabled,omitempty"`
// Push contact method options
DeviceType string `json:"device_type,omitempty"`
Sounds []*PushContactMethodSound `json:"sounds,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
// PushContactMethodSound represents a sound for a push contact method.
type PushContactMethodSound struct {
Type string `json:"type,omitempty"`
File string `json:"file,omitempty"`
}
// ListContactMethodsResponse represents
type ListContactMethodsResponse struct {
Limit int `json:"limit,omitempty"`
More bool `json:"more,omitempty"`
Offset int `json:"offset,omitempty"`
Total int `json:"total,omitempty"`
ContactMethods []*ContactMethod `json:"contact_methods,omitempty"`
}
// ListUsersOptions represents options when listing users.
type ListUsersOptions struct {
Limit int `url:"limit,omitempty"`
More bool `url:"more,omitempty"`
Offset int `url:"offset,omitempty"`
Total int `url:"total,omitempty"`
Include []string `url:"include,omitempty,brackets"`
Query string `url:"query,omitempty"`
TeamIDs []string `url:"team_ids,omitempty,brackets"`
}
// ListUsersResponse represents a list response of users.
type ListUsersResponse struct {
Limit int `json:"limit,omitempty"`
More bool `json:"more,omitempty"`
Offset int `json:"offset,omitempty"`
Total int `json:"total,omitempty"`
Users []*User `json:"users,omitempty"`
}
type ListFullUsersResponse struct {
Limit int `json:"limit,omitempty"`
More bool `json:"more,omitempty"`
Offset int `json:"offset,omitempty"`
Total int `json:"total,omitempty"`
Users []*FullUser `json:"users,omitempty"`
}
// GetUserOptions represents options when retrieving a user.
type GetUserOptions struct {
Include []string `url:"include,omitempty,brackets"`
}
// List lists existing users.
func (s *UserService) List(o *ListUsersOptions) (*ListUsersResponse, *Response, error) {
u := "/users"
v := new(ListUsersResponse)
resp, err := s.client.newRequestDo("GET", u, o, nil, &v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
func (s *UserService) ListAll(o *ListUsersOptions) ([]*FullUser, error) {
var users = make([]*FullUser, 0, 25)
var v *ListFullUsersResponse
more := true
offset := 0
for more {
log.Printf("==== Getting users at offset %d", offset)
v = new(ListFullUsersResponse)
_, err := s.client.newRequestDo("GET", "/users", o, nil, &v)
if err != nil {
return users, err
}
users = append(users, v.Users...)
more = v.More
offset += v.Limit
o.Offset = offset
}
return users, nil
}
// Create creates a new user.
func (s *UserService) Create(user *User) (*User, *Response, error) {
u := "/users"
v := new(User)
resp, err := s.client.newRequestDo("POST", u, nil, &User{User: user}, &v)
if err != nil {
return nil, nil, err
}
if err = cacheInsertUser(v.User); err != nil {
log.Printf("===== Error adding user %q to cache: %q", v.User.ID, err)
} else {
log.Printf("===== Added user %q to cache", v.User.ID)
}
return v.User, resp, nil
}
// Delete removes an existing user.
func (s *UserService) Delete(id string) (*Response, error) {
u := fmt.Sprintf("/users/%s", id)
resp, err := s.client.newRequestDo("DELETE", u, nil, nil, nil)
if cerr := cacheDeleteUser(id); cerr != nil {
log.Printf("===== Error deleting user %q from cache: %q", id, cerr)
} else {
log.Printf("===== Deleted user %q from cache", id)
}
return resp, err
}
// Get retrieves information about a user.
func (s *UserService) Get(id string, o *GetUserOptions) (*User, *Response, error) {
u := fmt.Sprintf("/users/%s", id)
v := new(User)
if err := cacheGetUser(id, v); err == nil {
return v, nil, nil
}
resp, err := s.client.newRequestDo("GET", u, o, nil, v)
if err != nil {
return nil, nil, err
}
return v.User, resp, nil
}
// Update updates an existing user.
func (s *UserService) Update(id string, user *User) (*User, *Response, error) {
u := fmt.Sprintf("/users/%s", id)
v := new(User)
resp, err := s.client.newRequestDo("PUT", u, nil, &User{User: user}, &v)
if err != nil {
return nil, nil, err
}
cacheUpdateUser(v.User)
return v.User, resp, nil
}
// ListContactMethods lists contact methods for a user.
func (s *UserService) ListContactMethods(userID string) (*ListContactMethodsResponse, *Response, error) {
u := fmt.Sprintf("/users/%s/contact_methods", userID)
v := new(ListContactMethodsResponse)
resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
// CreateContactMethod creates a new contact method for a user.
func (s *UserService) CreateContactMethod(userID string, contactMethod *ContactMethod) (*ContactMethod, *Response, error) {
u := fmt.Sprintf("/users/%s/contact_methods", userID)
v := new(ContactMethod)
resp, err := s.client.newRequestDo("POST", u, nil, &ContactMethod{ContactMethod: contactMethod}, &v)
if err != nil {
return nil, nil, err
}
if err = cacheInsertContactMethod(v.ContactMethod); err != nil {
log.Printf("===== Error adding contact method %q to cache: %q", v.ContactMethod.ID, err)
} else {
log.Printf("===== Added contact method %q to cache", v.ContactMethod.ID)
}
return v.ContactMethod, resp, nil
}
// GetContactMethod retrieves a contact method for a user.
func (s *UserService) GetContactMethod(userID string, contactMethodID string) (*ContactMethod, *Response, error) {
u := fmt.Sprintf("/users/%s/contact_methods/%s", userID, contactMethodID)
v := new(ContactMethod)
if err := cacheGetContactMethod(contactMethodID, v); err == nil {
return v, nil, nil
}
resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}
return v.ContactMethod, resp, nil
}
// UpdateContactMethod updates a contact method for a user.
func (s *UserService) UpdateContactMethod(userID, contactMethodID string, contactMethod *ContactMethod) (*ContactMethod, *Response, error) {
u := fmt.Sprintf("/users/%s/contact_methods/%s", userID, contactMethodID)
v := new(ContactMethod)
resp, err := s.client.newRequestDo("PUT", u, nil, &ContactMethod{ContactMethod: contactMethod}, &v)
if err != nil {
return nil, nil, err
}
cacheUpdateContactMethod(v.ContactMethod)
return v.ContactMethod, resp, nil
}
// DeleteContactMethod deletes a contact method for a user.
func (s *UserService) DeleteContactMethod(userID, contactMethodID string) (*Response, error) {
u := fmt.Sprintf("/users/%s/contact_methods/%s", userID, contactMethodID)
resp, err := s.client.newRequestDo("DELETE", u, nil, nil, nil)
if cerr := cacheDeleteContactMethod(contactMethodID); cerr != nil {
log.Printf("===== Error deleting contact method %q from cache: %q", contactMethodID, cerr)
} else {
log.Printf("===== Deleted contact method %q from cache", contactMethodID)
}
return resp, err
}
// CreateNotificationRule creates a new notification rule for a user.
func (s *UserService) CreateNotificationRule(userID string, rule *NotificationRule) (*NotificationRule, *Response, error) {
u := fmt.Sprintf("/users/%s/notification_rules", userID)
v := new(NotificationRule)
resp, err := s.client.newRequestDo("POST", u, nil, &NotificationRule{NotificationRule: rule}, &v)
if err != nil {
return nil, nil, err
}
if err = cacheInsertNotificationRule(v.NotificationRule); err != nil {
log.Printf("===== Error adding notification rule %q to cache: %q", v.NotificationRule.ID, err)
} else {
log.Printf("===== Added notification rule %q to cache", v.NotificationRule.ID)
}
return v.NotificationRule, resp, nil
}
// GetNotificationRule retrieves a notification rule for a user.
func (s *UserService) GetNotificationRule(userID string, ruleID string) (*NotificationRule, *Response, error) {
u := fmt.Sprintf("/users/%s/notification_rules/%s", userID, ruleID)
v := new(NotificationRule)
if err := cacheGetNotificationRule(ruleID, v); err == nil {
return v, nil, nil
}
resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}
return v.NotificationRule, resp, nil
}
// UpdateNotificationRule updates a notification rulefor a user.
func (s *UserService) UpdateNotificationRule(userID, ruleID string, rule *NotificationRule) (*NotificationRule, *Response, error) {
u := fmt.Sprintf("/users/%s/notification_rules/%s", userID, ruleID)
v := new(NotificationRule)
resp, err := s.client.newRequestDo("PUT", u, nil, &NotificationRule{NotificationRule: rule}, &v)
if err != nil {
return nil, nil, err
}
cacheUpdateNotificationRule(v.NotificationRule)
return v.NotificationRule, resp, nil
}
// DeleteNotificationRule deletes a notification rule for a user.
func (s *UserService) DeleteNotificationRule(userID, ruleID string) (*Response, error) {
u := fmt.Sprintf("/users/%s/notification_rules/%s", userID, ruleID)
resp, err := s.client.newRequestDo("DELETE", u, nil, nil, nil)
if cerr := cacheDeleteNotificationRule(ruleID); cerr != nil {
log.Printf("===== Error deleting notification rule %q from cache: %q", ruleID, cerr)
} else {
log.Printf("===== Deleted notification rule %q from cache", ruleID)
}
return resp, err
}