forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
356 lines (296 loc) · 12.1 KB
/
team.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
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// Team is a collection of users and escalation policies that represent a group of people within an organization.
type Team struct {
APIObject
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Parent *APIObject `json:"parent,omitempty"`
}
// ListTeamResponse is the structure used when calling the ListTeams API endpoint.
type ListTeamResponse struct {
APIListObject
Teams []Team
}
// ListTeamOptions are the input parameters used when calling the ListTeams API endpoint.
type ListTeamOptions 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"`
}
// ListTeams lists teams of your PagerDuty account, optionally filtered by a
// search query.
//
// Deprecated: Use ListTeamsWithContext instead.
func (c *Client) ListTeams(o ListTeamOptions) (*ListTeamResponse, error) {
return c.ListTeamsWithContext(context.Background(), o)
}
// ListTeamsWithContext lists teams of your PagerDuty account, optionally
// filtered by a search query.
func (c *Client) ListTeamsWithContext(ctx context.Context, o ListTeamOptions) (*ListTeamResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/teams?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListTeamResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// CreateTeam creates a new team.
//
// Deprecated: Use CreateTeamWithContext instead.
func (c *Client) CreateTeam(t *Team) (*Team, error) {
return c.CreateTeamWithContext(context.Background(), t)
}
// CreateTeamWithContext creates a new team.
func (c *Client) CreateTeamWithContext(ctx context.Context, t *Team) (*Team, error) {
resp, err := c.post(ctx, "/teams", t, nil)
return getTeamFromResponse(c, resp, err)
}
// DeleteTeam removes an existing team.
//
// Deprecated: Use DeleteTeamWithContext instead.
func (c *Client) DeleteTeam(id string) error {
return c.DeleteTeamWithContext(context.Background(), id)
}
// DeleteTeamWithContext removes an existing team.
func (c *Client) DeleteTeamWithContext(ctx context.Context, id string) error {
_, err := c.delete(ctx, "/teams/"+id)
return err
}
// GetTeam gets details about an existing team.
//
// Deprecated: Use GetTeamWithContext instead.
func (c *Client) GetTeam(id string) (*Team, error) {
return c.GetTeamWithContext(context.Background(), id)
}
// GetTeamWithContext gets details about an existing team.
func (c *Client) GetTeamWithContext(ctx context.Context, id string) (*Team, error) {
resp, err := c.get(ctx, "/teams/"+id, nil)
return getTeamFromResponse(c, resp, err)
}
// UpdateTeam updates an existing team.
//
// Deprecated: Use UpdateTeamWithContext instead.
func (c *Client) UpdateTeam(id string, t *Team) (*Team, error) {
return c.UpdateTeamWithContext(context.Background(), id, t)
}
// UpdateTeamWithContext updates an existing team.
func (c *Client) UpdateTeamWithContext(ctx context.Context, id string, t *Team) (*Team, error) {
resp, err := c.put(ctx, "/teams/"+id, t, nil)
return getTeamFromResponse(c, resp, err)
}
// RemoveEscalationPolicyFromTeam removes an escalation policy from a team.
//
// Deprecated: Use RemoveEscalationPolicyFromTeamWithContext instead.
func (c *Client) RemoveEscalationPolicyFromTeam(teamID, epID string) error {
return c.RemoveEscalationPolicyFromTeamWithContext(context.Background(), teamID, epID)
}
// RemoveEscalationPolicyFromTeamWithContext removes an escalation policy from a team.
func (c *Client) RemoveEscalationPolicyFromTeamWithContext(ctx context.Context, teamID, epID string) error {
_, err := c.delete(ctx, "/teams/"+teamID+"/escalation_policies/"+epID)
return err
}
// AddEscalationPolicyToTeam adds an escalation policy to a team.
//
// Deprecated: Use AddEscalationPolicyToTeamWithContext instead.
func (c *Client) AddEscalationPolicyToTeam(teamID, epID string) error {
return c.AddEscalationPolicyToTeamWithContext(context.Background(), teamID, epID)
}
// AddEscalationPolicyToTeamWithContext adds an escalation policy to a team.
func (c *Client) AddEscalationPolicyToTeamWithContext(ctx context.Context, teamID, epID string) error {
_, err := c.put(ctx, "/teams/"+teamID+"/escalation_policies/"+epID, nil, nil)
return err
}
// RemoveUserFromTeam removes a user from a team.
//
// Deprecated: Use RemoveUserFromTeamWithContext instead.
func (c *Client) RemoveUserFromTeam(teamID, userID string) error {
return c.RemoveUserFromTeamWithContext(context.Background(), teamID, userID)
}
// RemoveUserFromTeamWithContext removes a user from a team.
func (c *Client) RemoveUserFromTeamWithContext(ctx context.Context, teamID, userID string) error {
_, err := c.delete(ctx, "/teams/"+teamID+"/users/"+userID)
return err
}
// AddUserToTeam adds a user to a team.
//
// Deprecated: Use AddUserToTeamWithContext instead.
func (c *Client) AddUserToTeam(teamID, userID string) error {
return c.AddUserToTeamWithContext(context.Background(), AddUserToTeamOptions{TeamID: teamID, UserID: userID})
}
// TeamUserRole is a named type to represent the different Team Roles supported
// by PagerDuty when adding a user to a team.
//
// For more info: https://support.pagerduty.com/docs/advanced-permissions#team-roles
type TeamUserRole string
const (
// TeamUserRoleObserver is the obesrver team role, which generally provides
// read-only access. They gain responder-level permissions on an incident if
// one is assigned to them.
TeamUserRoleObserver TeamUserRole = "observer"
// TeamUserRoleResponder is the responder team role, and they are given the
// same permissions as the observer plus the ability to respond to
// incidents, trigger incidents, and manage overrides.
TeamUserRoleResponder TeamUserRole = "responder"
// TeamUserRoleManager is the manager team role, and they are given the same
// permissions as the responder plus the ability to edit and delete the
// different resources owned by the team.
TeamUserRoleManager TeamUserRole = "manager"
)
// AddUserToTeamOptions is an option struct for the AddUserToTeamWithContext
// method.
type AddUserToTeamOptions struct {
TeamID string `json:"-"`
UserID string `json:"-"`
Role TeamUserRole `json:"role,omitempty"`
}
// AddUserToTeamWithContext adds a user to a team.
func (c *Client) AddUserToTeamWithContext(ctx context.Context, o AddUserToTeamOptions) error {
_, err := c.put(ctx, "/teams/"+o.TeamID+"/users/"+o.UserID, o, nil)
return err
}
func getTeamFromResponse(c *Client, resp *http.Response, err error) (*Team, error) {
if err != nil {
return nil, err
}
var target map[string]Team
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "team"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}
// Member is a team member.
type Member struct {
User APIObject `json:"user"`
Role string `json:"role"`
}
// ListTeamMembersOptions are the optional parameters for a members request.
type ListTeamMembersOptions 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"`
}
// ListMembersOptions is the original type name and is retained as an alias for
// API compatibility.
//
// Deprecated: Use type ListTeamMembersOptions instead; will be removed in V2
type ListMembersOptions = ListTeamMembersOptions
// ListTeamMembersResponse is the response from the members endpoint.
type ListTeamMembersResponse struct {
APIListObject
Members []Member `json:"members"`
}
// ListMembersResponse is the original type name and is retained as an alias for
// API compatibility.
//
// Deprecated: Use type ListTeamMembersResponse instead; will be removed in V2
type ListMembersResponse = ListTeamMembersResponse
// ListMembers gets a page of users associated with the specified team.
//
// Deprecated: Use ListTeamMembers instead.
func (c *Client) ListMembers(teamID string, o ListTeamMembersOptions) (*ListTeamMembersResponse, error) {
return c.ListTeamMembers(context.Background(), teamID, o)
}
// ListMembersWithContext gets a page of users associated with the specified team.
//
// Deprecated: Use ListTeamMembers instead.
func (c *Client) ListMembersWithContext(ctx context.Context, teamID string, o ListTeamMembersOptions) (*ListTeamMembersResponse, error) {
return c.ListTeamMembers(ctx, teamID, o)
}
// ListTeamMembers gets a page of users associated with the specified team.
func (c *Client) ListTeamMembers(ctx context.Context, teamID string, o ListTeamMembersOptions) (*ListTeamMembersResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/teams/"+teamID+"/members?"+v.Encode(), nil)
if err != nil {
return nil, err
}
var result ListTeamMembersResponse
if err = c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// ListAllMembers gets all members associated with the specified team.
//
// Deprecated: Use ListTeamMembersPaginated instead.
func (c *Client) ListAllMembers(teamID string) ([]Member, error) {
return c.ListTeamMembersPaginated(context.Background(), teamID)
}
// ListMembersPaginated gets all members associated with the specified team.
//
// Deprecated: Use ListTeamMembersPaginated instead.
func (c *Client) ListMembersPaginated(ctx context.Context, teamID string) ([]Member, error) {
return c.ListTeamMembersPaginated(ctx, teamID)
}
// ListTeamMembersPaginated gets all members associated with the specified team.
func (c *Client) ListTeamMembersPaginated(ctx context.Context, teamID string) ([]Member, error) {
var members []Member
// Create a handler closure capable of parsing data from the members endpoint
// and appending resultant members to the return slice.
responseHandler := func(response *http.Response) (APIListObject, error) {
var result ListTeamMembersResponse
if err := c.decodeJSON(response, &result); err != nil {
return APIListObject{}, err
}
members = append(members, result.Members...)
// Return stats on the current page. Caller can use this information to
// adjust for requesting additional pages.
return APIListObject{
More: result.More,
Offset: result.Offset,
Limit: result.Limit,
}, nil
}
// Make call to get all pages associated with the base endpoint.
if err := c.pagedGet(ctx, "/teams/"+teamID+"/members", responseHandler); err != nil {
return nil, err
}
return members, nil
}