/
teams.go
386 lines (319 loc) · 11.3 KB
/
teams.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
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package opsmngr
import (
"context"
"fmt"
"net/http"
atlas "go.mongodb.org/atlas/mongodbatlas"
)
const (
teamsBasePath = "orgs/%s/teams"
)
// TeamsService provides access to the team related functions in the Ops Manager API.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/
type TeamsService interface {
List(context.Context, string, *atlas.ListOptions) ([]atlas.Team, *atlas.Response, error)
Get(context.Context, string, string) (*atlas.Team, *atlas.Response, error)
GetOneTeamByName(context.Context, string, string) (*atlas.Team, *atlas.Response, error)
GetTeamUsersAssigned(context.Context, string, string) ([]*User, *atlas.Response, error)
Create(context.Context, string, *atlas.Team) (*atlas.Team, *atlas.Response, error)
Rename(context.Context, string, string, string) (*atlas.Team, *atlas.Response, error)
UpdateTeamRoles(context.Context, string, string, *atlas.TeamUpdateRoles) ([]atlas.TeamRoles, *atlas.Response, error)
AddUsersToTeam(context.Context, string, string, []string) ([]*User, *atlas.Response, error)
RemoveUserToTeam(context.Context, string, string, string) (*atlas.Response, error)
RemoveTeamFromOrganization(context.Context, string, string) (*atlas.Response, error)
RemoveTeamFromProject(context.Context, string, string) (*atlas.Response, error)
}
// TeamsServiceOp provides an implementation of the TeamsService interface
type TeamsServiceOp service
var _ TeamsService = &TeamsServiceOp{}
// List gets all teams.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-get-all/
func (s *TeamsServiceOp) List(ctx context.Context, orgID string, listOptions *atlas.ListOptions) ([]atlas.Team, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
path := fmt.Sprintf(teamsBasePath, orgID)
// Add query params from listOptions
path, err := setQueryParams(path, listOptions)
if err != nil {
return nil, nil, err
}
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(atlas.TeamsResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Results, resp, nil
}
// Get gets a single team in the organization by team ID.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-get-one-by-id/
func (s *TeamsServiceOp) Get(ctx context.Context, orgID, teamID string) (*atlas.Team, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, nil, atlas.NewArgError("teamID", "must be set")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s", basePath, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(atlas.Team)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// GetOneTeamByName gets a single project by its name.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-get-one-by-name/
func (s *TeamsServiceOp) GetOneTeamByName(ctx context.Context, orgID, teamName string) (*atlas.Team, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamName == "" {
return nil, nil, atlas.NewArgError("teamName", "must be set")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/byName/%s", basePath, teamName)
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(atlas.Team)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// GetTeamUsersAssigned gets all the users assigned to a team.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-get-all-users/
func (s *TeamsServiceOp) GetTeamUsersAssigned(ctx context.Context, orgID, teamID string) ([]*User, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, nil, atlas.NewArgError("teamID", "must be set")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s/users", basePath, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(UsersResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Results, resp, nil
}
// Create creates a team.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-create-one/
func (s *TeamsServiceOp) Create(ctx context.Context, orgID string, createRequest *atlas.Team) (*atlas.Team, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if createRequest == nil {
return nil, nil, atlas.NewArgError("createRequest", "cannot be nil")
}
req, err := s.Client.NewRequest(ctx, http.MethodPost, fmt.Sprintf(teamsBasePath, orgID), createRequest)
if err != nil {
return nil, nil, err
}
root := new(atlas.Team)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// Rename renames a team.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-rename-one/
func (s *TeamsServiceOp) Rename(ctx context.Context, orgID, teamID, teamName string) (*atlas.Team, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, nil, atlas.NewArgError("teamID", "must be set")
}
if teamName == "" {
return nil, nil, atlas.NewArgError("teamName", "cannot be nil")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s", basePath, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, map[string]interface{}{
"name": teamName,
})
if err != nil {
return nil, nil, err
}
root := new(atlas.Team)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// UpdateTeamRoles Update the roles of a team in an Atlas project.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-update-roles/
func (s *TeamsServiceOp) UpdateTeamRoles(ctx context.Context, orgID, teamID string, updateTeamRolesRequest *atlas.TeamUpdateRoles) ([]atlas.TeamRoles, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, nil, atlas.NewArgError("teamID", "must be set")
}
if updateTeamRolesRequest == nil {
return nil, nil, atlas.NewArgError("updateTeamRolesRequest", "cannot be nil")
}
path := fmt.Sprintf("groups/%s/teams/%s", orgID, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, updateTeamRolesRequest)
if err != nil {
return nil, nil, err
}
root := new(atlas.TeamUpdateRolesResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Results, resp, nil
}
// AddUsersToTeam adds a users from the organization associated with {ORG-ID} to the team with ID {TEAM-ID}.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-add-user/
func (s *TeamsServiceOp) AddUsersToTeam(ctx context.Context, orgID, teamID string, usersID []string) ([]*User, *atlas.Response, error) {
if orgID == "" {
return nil, nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, nil, atlas.NewArgError("teamID", "must be set")
}
if len(usersID) < 1 {
return nil, nil, atlas.NewArgError("usersID", "cannot empty at leas one userID must be set")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s/users", basePath, teamID)
users := make([]map[string]interface{}, len(usersID))
for i, id := range usersID {
users[i] = map[string]interface{}{"id": id}
}
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, users)
if err != nil {
return nil, nil, err
}
root := new(UsersResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.Results, resp, nil
}
// RemoveUserToTeam removes the specified user from the specified team.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-remove-user/
func (s *TeamsServiceOp) RemoveUserToTeam(ctx context.Context, orgID, teamID, userID string) (*atlas.Response, error) {
if orgID == "" {
return nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, atlas.NewArgError("teamID", "must be set")
}
if userID == "" {
return nil, atlas.NewArgError("userID", "cannot be nil")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s/users/%s", basePath, teamID, userID)
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.Client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// RemoveTeamFromOrganization deletes the team with ID {TEAM-ID} from the organization specified to {ORG-ID}.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-delete-one/
func (s *TeamsServiceOp) RemoveTeamFromOrganization(ctx context.Context, orgID, teamID string) (*atlas.Response, error) {
if orgID == "" {
return nil, atlas.NewArgError("orgID", "must be set")
}
if teamID == "" {
return nil, atlas.NewArgError("teamID", "cannot be nil")
}
basePath := fmt.Sprintf(teamsBasePath, orgID)
path := fmt.Sprintf("%s/%s", basePath, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.Client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// RemoveTeamFromProject removes the specified team from the specified project.
//
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/teams/teams-remove-from-project/
func (s *TeamsServiceOp) RemoveTeamFromProject(ctx context.Context, groupID, teamID string) (*atlas.Response, error) {
if groupID == "" {
return nil, atlas.NewArgError("groupID", "must be set")
}
if teamID == "" {
return nil, atlas.NewArgError("teamID", "cannot be nil")
}
path := fmt.Sprintf("groups/%s/teams/%s", groupID, teamID)
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.Client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}