Skip to content

Commit 05ebd06

Browse files
CLOUDP-93381: Add Programmatic Invite Management for organization to the atlas go client (#227)
1 parent 1cae739 commit 05ebd06

File tree

4 files changed

+514
-21
lines changed

4 files changed

+514
-21
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright 2021 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mongodbatlas
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"net/http"
21+
)
22+
23+
const invitationBasePath = orgsBasePath + "/%s/invites"
24+
25+
// InvitationOptions filtering options for invitations.
26+
type InvitationOptions struct {
27+
Username string `url:"username,omitempty"`
28+
}
29+
30+
// Invitation represents the structure of an Invitation.
31+
type Invitation struct {
32+
ID string `json:"id,omitempty"`
33+
OrgID string `json:"orgId,omitempty"`
34+
OrgName string `json:"orgName,omitempty"`
35+
CreatedAt string `json:"createdAt,omitempty"`
36+
ExpiresAt string `json:"expiresAt,omitempty"`
37+
InviterUsername string `json:"inviterUsername,omitempty"`
38+
Username string `json:"username,omitempty"`
39+
Roles []string `json:"roles,omitempty"`
40+
TeamIDs []string `json:"teamIds,omitempty"`
41+
}
42+
43+
// Invitations gets all unaccepted invitations to the specified Atlas organization.
44+
//
45+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-get-invitations/
46+
func (s *OrganizationsServiceOp) Invitations(ctx context.Context, orgID string, opts *InvitationOptions) ([]*Invitation, *Response, error) {
47+
if orgID == "" {
48+
return nil, nil, NewArgError("orgID", "must be set")
49+
}
50+
51+
basePath := fmt.Sprintf(invitationBasePath, orgID)
52+
path, err := setListOptions(basePath, opts)
53+
if err != nil {
54+
return nil, nil, err
55+
}
56+
57+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
58+
if err != nil {
59+
return nil, nil, err
60+
}
61+
62+
var root []*Invitation
63+
resp, err := s.Client.Do(ctx, req, &root)
64+
if err != nil {
65+
return nil, resp, err
66+
}
67+
68+
return root, resp, nil
69+
}
70+
71+
// Invitation gets details for one unaccepted invitation to the specified Atlas organization.
72+
//
73+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-get-one-invitation/
74+
func (s *OrganizationsServiceOp) Invitation(ctx context.Context, orgID, invitationID string) (*Invitation, *Response, error) {
75+
if orgID == "" {
76+
return nil, nil, NewArgError("orgID", "must be set")
77+
}
78+
79+
if invitationID == "" {
80+
return nil, nil, NewArgError("invitationID", "must be set")
81+
}
82+
83+
basePath := fmt.Sprintf(invitationBasePath, orgID)
84+
path := fmt.Sprintf("%s/%s", basePath, invitationID)
85+
86+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
87+
if err != nil {
88+
return nil, nil, err
89+
}
90+
91+
root := new(Invitation)
92+
resp, err := s.Client.Do(ctx, req, root)
93+
if err != nil {
94+
return nil, resp, err
95+
}
96+
97+
return root, resp, nil
98+
}
99+
100+
// InviteUser invites one user to the Atlas organization that you specify.
101+
//
102+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-create-one-invitation/
103+
func (s *OrganizationsServiceOp) InviteUser(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) {
104+
if invitation.OrgID == "" {
105+
return nil, nil, NewArgError("orgID", "must be set")
106+
}
107+
108+
path := fmt.Sprintf(invitationBasePath, invitation.OrgID)
109+
110+
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, invitation)
111+
if err != nil {
112+
return nil, nil, err
113+
}
114+
115+
root := new(Invitation)
116+
resp, err := s.Client.Do(ctx, req, root)
117+
if err != nil {
118+
return nil, resp, err
119+
}
120+
121+
return root, resp, nil
122+
}
123+
124+
// UpdateInvitation updates one pending invitation to the Atlas organization that you specify.
125+
//
126+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-update-one-invitation/
127+
func (s *OrganizationsServiceOp) UpdateInvitation(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) {
128+
if invitation.OrgID == "" {
129+
return nil, nil, NewArgError("orgID", "must be set")
130+
}
131+
132+
return s.updateInvitation(ctx, invitation)
133+
}
134+
135+
// UpdateInvitationByID updates one invitation to the Atlas organization.
136+
//
137+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-update-one-invitation-by-id/
138+
func (s *OrganizationsServiceOp) UpdateInvitationByID(ctx context.Context, invitationID string, invitation *Invitation) (*Invitation, *Response, error) {
139+
if invitation.OrgID == "" {
140+
return nil, nil, NewArgError("orgID", "must be set")
141+
}
142+
143+
if invitationID == "" {
144+
return nil, nil, NewArgError("invitationID", "must be set")
145+
}
146+
147+
invitation.ID = invitationID
148+
149+
return s.updateInvitation(ctx, invitation)
150+
}
151+
152+
// DeleteInvitation deletes one unaccepted invitation to the specified Atlas organization. You can't delete an invitation that a user has accepted.
153+
//
154+
// See more: https://docs.atlas.mongodb.com/reference/api/organization-delete-invitation/
155+
func (s *OrganizationsServiceOp) DeleteInvitation(ctx context.Context, orgID, invitationID string) (*Response, error) {
156+
if orgID == "" {
157+
return nil, NewArgError("orgID", "must be set")
158+
}
159+
160+
if invitationID == "" {
161+
return nil, NewArgError("invitationID", "must be set")
162+
}
163+
164+
basePath := fmt.Sprintf(invitationBasePath, orgID)
165+
path := fmt.Sprintf("%s/%s", basePath, invitationID)
166+
167+
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
resp, err := s.Client.Do(ctx, req, nil)
173+
174+
return resp, err
175+
}
176+
177+
func (s *OrganizationsServiceOp) updateInvitation(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) {
178+
path := fmt.Sprintf(invitationBasePath, invitation.OrgID)
179+
180+
if invitation.ID != "" {
181+
path = fmt.Sprintf("%s/%s", path, invitation.ID)
182+
}
183+
184+
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, invitation)
185+
if err != nil {
186+
return nil, nil, err
187+
}
188+
189+
root := new(Invitation)
190+
resp, err := s.Client.Do(ctx, req, root)
191+
if err != nil {
192+
return nil, resp, err
193+
}
194+
195+
return root, resp, nil
196+
}

0 commit comments

Comments
 (0)