|
| 1 | +package mongodbatlas |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + usersBasePath = "groups/%s/users" |
| 11 | +) |
| 12 | + |
| 13 | +// AtlasUsersService is an interface for interfacing with the AtlasUsers |
| 14 | +// endpoints of the MongoDB Atlas API. |
| 15 | +// See more: https://docs.atlas.mongodb.com/reference/api/users/ |
| 16 | +type AtlasUsersService interface { |
| 17 | + List(context.Context, string, *ListOptions) ([]AtlasUser, *Response, error) |
| 18 | + Get(context.Context, string) (*AtlasUser, *Response, error) |
| 19 | + GetByName(context.Context, string) (*AtlasUser, *Response, error) |
| 20 | + Create(context.Context, *AtlasUser) (*AtlasUser, *Response, error) |
| 21 | +} |
| 22 | + |
| 23 | +//AtlasUsersServiceOp handles communication with the AtlasUsers related methos of the |
| 24 | +//MongoDB Atlas API |
| 25 | +type AtlasUsersServiceOp struct { |
| 26 | + client *Client |
| 27 | +} |
| 28 | + |
| 29 | +var _ AtlasUsersService = &AtlasUsersServiceOp{} |
| 30 | + |
| 31 | +// AtlasUsers represents a array of project |
| 32 | +type AtlasUsersResponse struct { |
| 33 | + Links []*Link `json:"links"` |
| 34 | + Results []AtlasUser `json:"results"` |
| 35 | + TotalCount int `json:"totalCount"` |
| 36 | +} |
| 37 | + |
| 38 | +type AtlasUser struct { |
| 39 | + EmailAddress string `json:"emailAddress"` |
| 40 | + FirstName string `json:"firstName"` |
| 41 | + ID string `json:"id,omitempty"` |
| 42 | + LastName string `json:"lastName"` |
| 43 | + Roles []AtlasRole `json:"roles"` |
| 44 | + TeamIds []string `json:"teamIds,omitempty"` |
| 45 | + Username string `json:"username"` |
| 46 | + MobileNumber string `json:"mobileNumber"` |
| 47 | + Password string `json:"password"` |
| 48 | + Country string `json:"country"` |
| 49 | +} |
| 50 | + |
| 51 | +//List gets all users. |
| 52 | +//See more: https://docs.atlas.mongodb.com/reference/api/user-get-all/ |
| 53 | +func (s *AtlasUsersServiceOp) List(ctx context.Context, orgID string, listOptions *ListOptions) ([]AtlasUser, *Response, error) { |
| 54 | + path := fmt.Sprintf(usersBasePath, orgID) |
| 55 | + |
| 56 | + //Add query params from listOptions |
| 57 | + path, err := setListOptions(path, listOptions) |
| 58 | + if err != nil { |
| 59 | + return nil, nil, err |
| 60 | + } |
| 61 | + |
| 62 | + req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + |
| 67 | + root := new(AtlasUsersResponse) |
| 68 | + resp, err := s.client.Do(ctx, req, root) |
| 69 | + if err != nil { |
| 70 | + return nil, resp, err |
| 71 | + } |
| 72 | + |
| 73 | + if l := root.Links; l != nil { |
| 74 | + resp.Links = l |
| 75 | + } |
| 76 | + |
| 77 | + return root.Results, resp, nil |
| 78 | +} |
| 79 | + |
| 80 | +//Get gets a single atlas user. |
| 81 | +//See more: https://docs.atlas.mongodb.com/reference/api/user-get-by-id/ |
| 82 | +func (s *AtlasUsersServiceOp) Get(ctx context.Context, userID string) (*AtlasUser, *Response, error) { |
| 83 | + if userID == "" { |
| 84 | + return nil, nil, NewArgError("userID", "must be set") |
| 85 | + } |
| 86 | + |
| 87 | + path := fmt.Sprintf("users/%s", userID) |
| 88 | + |
| 89 | + req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) |
| 90 | + if err != nil { |
| 91 | + return nil, nil, err |
| 92 | + } |
| 93 | + |
| 94 | + root := new(AtlasUser) |
| 95 | + resp, err := s.client.Do(ctx, req, root) |
| 96 | + if err != nil { |
| 97 | + return nil, resp, err |
| 98 | + } |
| 99 | + |
| 100 | + return root, resp, err |
| 101 | +} |
| 102 | + |
| 103 | +//GetByName gets a single atlas user by name. |
| 104 | +//See more: https://docs.atlas.mongodb.com/reference/api/user-get-one-by-name/ |
| 105 | +func (s *AtlasUsersServiceOp) GetByName(ctx context.Context, username string) (*AtlasUser, *Response, error) { |
| 106 | + if username == "" { |
| 107 | + return nil, nil, NewArgError("username", "must be set") |
| 108 | + } |
| 109 | + |
| 110 | + path := fmt.Sprintf("users/byName/%s", username) |
| 111 | + |
| 112 | + req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) |
| 113 | + if err != nil { |
| 114 | + return nil, nil, err |
| 115 | + } |
| 116 | + |
| 117 | + root := new(AtlasUser) |
| 118 | + resp, err := s.client.Do(ctx, req, root) |
| 119 | + if err != nil { |
| 120 | + return nil, resp, err |
| 121 | + } |
| 122 | + |
| 123 | + return root, resp, err |
| 124 | +} |
| 125 | + |
| 126 | +//Create creates an Atlas User. |
| 127 | +//See more: https://docs.atlas.mongodb.com/reference/api/user-create/ |
| 128 | +func (s *AtlasUsersServiceOp) Create(ctx context.Context, createRequest *AtlasUser) (*AtlasUser, *Response, error) { |
| 129 | + if createRequest == nil { |
| 130 | + return nil, nil, NewArgError("createRequest", "cannot be nil") |
| 131 | + } |
| 132 | + |
| 133 | + req, err := s.client.NewRequest(ctx, http.MethodPost, fmt.Sprintf("users"), createRequest) |
| 134 | + if err != nil { |
| 135 | + return nil, nil, err |
| 136 | + } |
| 137 | + |
| 138 | + root := new(AtlasUser) |
| 139 | + resp, err := s.client.Do(ctx, req, root) |
| 140 | + if err != nil { |
| 141 | + return nil, resp, err |
| 142 | + } |
| 143 | + |
| 144 | + return root, resp, err |
| 145 | +} |
0 commit comments