|
| 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 projectInvitationBasePath = projectBasePath + "/%s/invites" |
| 24 | + |
| 25 | +// Invitations gets all unaccepted invitations to the specified Atlas project. |
| 26 | +// |
| 27 | +// See more: https://docs.atlas.mongodb.com/reference/api/project-get-invitations/ |
| 28 | +func (s *ProjectsServiceOp) Invitations(ctx context.Context, groupID string, opts *InvitationOptions) ([]*Invitation, *Response, error) { |
| 29 | + if groupID == "" { |
| 30 | + return nil, nil, NewArgError("groupID", "must be set") |
| 31 | + } |
| 32 | + |
| 33 | + basePath := fmt.Sprintf(projectInvitationBasePath, groupID) |
| 34 | + path, err := setListOptions(basePath, opts) |
| 35 | + if err != nil { |
| 36 | + return nil, nil, err |
| 37 | + } |
| 38 | + |
| 39 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 40 | + if err != nil { |
| 41 | + return nil, nil, err |
| 42 | + } |
| 43 | + |
| 44 | + var root []*Invitation |
| 45 | + resp, err := s.Client.Do(ctx, req, &root) |
| 46 | + if err != nil { |
| 47 | + return nil, resp, err |
| 48 | + } |
| 49 | + |
| 50 | + return root, resp, nil |
| 51 | +} |
| 52 | + |
| 53 | +// Invitation gets details for one unaccepted invitation to the specified Atlas project. |
| 54 | +// |
| 55 | +// See more: https://docs.atlas.mongodb.com/reference/api/project-get-one-invitation/ |
| 56 | +func (s *ProjectsServiceOp) Invitation(ctx context.Context, groupID, invitationID string) (*Invitation, *Response, error) { |
| 57 | + if groupID == "" { |
| 58 | + return nil, nil, NewArgError("groupID", "must be set") |
| 59 | + } |
| 60 | + |
| 61 | + if invitationID == "" { |
| 62 | + return nil, nil, NewArgError("invitationID", "must be set") |
| 63 | + } |
| 64 | + |
| 65 | + basePath := fmt.Sprintf(projectInvitationBasePath, groupID) |
| 66 | + path := fmt.Sprintf("%s/%s", basePath, invitationID) |
| 67 | + |
| 68 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 69 | + if err != nil { |
| 70 | + return nil, nil, err |
| 71 | + } |
| 72 | + |
| 73 | + root := new(Invitation) |
| 74 | + resp, err := s.Client.Do(ctx, req, root) |
| 75 | + if err != nil { |
| 76 | + return nil, resp, err |
| 77 | + } |
| 78 | + |
| 79 | + return root, resp, nil |
| 80 | +} |
| 81 | + |
| 82 | +// InviteUser invites one user to the Atlas project that you specify. |
| 83 | +func (s *ProjectsServiceOp) InviteUser(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) { |
| 84 | + if invitation.GroupID == "" { |
| 85 | + return nil, nil, NewArgError("groupID", "must be set") |
| 86 | + } |
| 87 | + |
| 88 | + path := fmt.Sprintf(projectInvitationBasePath, invitation.GroupID) |
| 89 | + |
| 90 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, invitation) |
| 91 | + if err != nil { |
| 92 | + return nil, nil, err |
| 93 | + } |
| 94 | + |
| 95 | + root := new(Invitation) |
| 96 | + resp, err := s.Client.Do(ctx, req, root) |
| 97 | + if err != nil { |
| 98 | + return nil, resp, err |
| 99 | + } |
| 100 | + |
| 101 | + return root, resp, nil |
| 102 | +} |
| 103 | + |
| 104 | +// UpdateInvitation updates one pending invitation to the Atlas project that you specify. |
| 105 | +// |
| 106 | +// See more: https://docs.atlas.mongodb.com/reference/api/project-update-one-invitation/ |
| 107 | +func (s *ProjectsServiceOp) UpdateInvitation(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) { |
| 108 | + if invitation.GroupID == "" { |
| 109 | + return nil, nil, NewArgError("groupID", "must be set") |
| 110 | + } |
| 111 | + |
| 112 | + return s.updateInvitation(ctx, invitation) |
| 113 | +} |
| 114 | + |
| 115 | +// UpdateInvitationByID updates one invitation to the Atlas project. |
| 116 | +// |
| 117 | +// See more: https://docs.atlas.mongodb.com/reference/api/project-update-one-invitation-by-id/ |
| 118 | +func (s *ProjectsServiceOp) UpdateInvitationByID(ctx context.Context, invitationID string, invitation *Invitation) (*Invitation, *Response, error) { |
| 119 | + if invitation.GroupID == "" { |
| 120 | + return nil, nil, NewArgError("groupID", "must be set") |
| 121 | + } |
| 122 | + |
| 123 | + if invitationID == "" { |
| 124 | + return nil, nil, NewArgError("invitationID", "must be set") |
| 125 | + } |
| 126 | + |
| 127 | + invitation.ID = invitationID |
| 128 | + |
| 129 | + return s.updateInvitation(ctx, invitation) |
| 130 | +} |
| 131 | + |
| 132 | +// DeleteInvitation deletes one unaccepted invitation to the specified Atlas project. You can't delete an invitation that a user has accepted. |
| 133 | +// |
| 134 | +// See more: https://docs.atlas.mongodb.com/reference/api/project-delete-invitation/ |
| 135 | +func (s *ProjectsServiceOp) DeleteInvitation(ctx context.Context, groupID, invitationID string) (*Response, error) { |
| 136 | + if groupID == "" { |
| 137 | + return nil, NewArgError("groupID", "must be set") |
| 138 | + } |
| 139 | + |
| 140 | + if invitationID == "" { |
| 141 | + return nil, NewArgError("invitationID", "must be set") |
| 142 | + } |
| 143 | + |
| 144 | + basePath := fmt.Sprintf(projectInvitationBasePath, groupID) |
| 145 | + path := fmt.Sprintf("%s/%s", basePath, invitationID) |
| 146 | + |
| 147 | + req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + resp, err := s.Client.Do(ctx, req, nil) |
| 153 | + |
| 154 | + return resp, err |
| 155 | +} |
| 156 | + |
| 157 | +func (s *ProjectsServiceOp) updateInvitation(ctx context.Context, invitation *Invitation) (*Invitation, *Response, error) { |
| 158 | + path := fmt.Sprintf(projectInvitationBasePath, invitation.GroupID) |
| 159 | + |
| 160 | + if invitation.ID != "" { |
| 161 | + path = fmt.Sprintf("%s/%s", path, invitation.ID) |
| 162 | + } |
| 163 | + |
| 164 | + req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, invitation) |
| 165 | + if err != nil { |
| 166 | + return nil, nil, err |
| 167 | + } |
| 168 | + |
| 169 | + root := new(Invitation) |
| 170 | + resp, err := s.Client.Do(ctx, req, root) |
| 171 | + if err != nil { |
| 172 | + return nil, resp, err |
| 173 | + } |
| 174 | + |
| 175 | + return root, resp, nil |
| 176 | +} |
0 commit comments