|
| 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 serverlessInstancesPath = "api/atlas/v1.0/groups/%s/serverless" |
| 24 | + |
| 25 | +// ServerlessInstancesService is an interface for interfacing with the Serverless Instances endpoints of the MongoDB Atlas API. |
| 26 | +// |
| 27 | +// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-one-serverless-instance/ |
| 28 | +type ServerlessInstancesService interface { |
| 29 | + List(context.Context, string, *ListOptions) (*ClustersResponse, *Response, error) |
| 30 | + Get(context.Context, string, string) (*Cluster, *Response, error) |
| 31 | + Create(context.Context, string, *ServerlessCreateRequestParams) (*Cluster, *Response, error) |
| 32 | + Delete(context.Context, string, string) (*Response, error) |
| 33 | +} |
| 34 | + |
| 35 | +// ServerlessInstancesServiceOp handles communication with the Serverless Instances related methods of the MongoDB Atlas API. |
| 36 | +type ServerlessInstancesServiceOp service |
| 37 | + |
| 38 | +var _ ServerlessInstancesService = &ServerlessInstancesServiceOp{} |
| 39 | + |
| 40 | +// ClustersResponse represents the response of ServerlessInstancesService.List. |
| 41 | +type ClustersResponse struct { |
| 42 | + Links []*Link `json:"links,omitempty"` |
| 43 | + Results []*Cluster `json:"results,omitempty"` |
| 44 | + TotalCount int `json:"totalCount,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// ServerlessCreateRequestParams represents the Request Body Parameters of ServerlessInstancesService.Create. |
| 48 | +type ServerlessCreateRequestParams struct { |
| 49 | + Name string `json:"name,omitempty"` |
| 50 | + ProviderSettings *ServerlessProviderSettings `json:"providerSettings,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +// ServerlessProviderSettings represents the Provider Settings of serverless instances. |
| 54 | +type ServerlessProviderSettings struct { |
| 55 | + BackingProviderName string `json:"backingProviderName,omitempty"` |
| 56 | + ProviderName string `json:"providerName,omitempty"` |
| 57 | + RegionName string `json:"regionName,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +// List gets all serverless instances in the specified project. |
| 61 | +// |
| 62 | +// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-all-serverless-instances/ |
| 63 | +func (s *ServerlessInstancesServiceOp) List(ctx context.Context, projectID string, listOptions *ListOptions) (*ClustersResponse, *Response, error) { |
| 64 | + if projectID == "" { |
| 65 | + return nil, nil, NewArgError("projectID", "must be set") |
| 66 | + } |
| 67 | + |
| 68 | + path := fmt.Sprintf(serverlessInstancesPath, projectID) |
| 69 | + path, err := setListOptions(path, listOptions) |
| 70 | + if err != nil { |
| 71 | + return nil, nil, err |
| 72 | + } |
| 73 | + |
| 74 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 75 | + if err != nil { |
| 76 | + return nil, nil, err |
| 77 | + } |
| 78 | + |
| 79 | + root := new(ClustersResponse) |
| 80 | + resp, err := s.Client.Do(ctx, req, root) |
| 81 | + if err != nil { |
| 82 | + return nil, resp, err |
| 83 | + } |
| 84 | + |
| 85 | + return root, resp, nil |
| 86 | +} |
| 87 | + |
| 88 | +// Get retrieves one serverless instance in the specified project. |
| 89 | +// |
| 90 | +// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-one-serverless-instance/ |
| 91 | +func (s *ServerlessInstancesServiceOp) Get(ctx context.Context, projectID, instanceName string) (*Cluster, *Response, error) { |
| 92 | + if projectID == "" { |
| 93 | + return nil, nil, NewArgError("projectID", "must be set") |
| 94 | + } |
| 95 | + |
| 96 | + if instanceName == "" { |
| 97 | + return nil, nil, NewArgError("instanceName", "must be set") |
| 98 | + } |
| 99 | + |
| 100 | + basePath := fmt.Sprintf(serverlessInstancesPath, projectID) |
| 101 | + path := fmt.Sprintf("%s/%s", basePath, instanceName) |
| 102 | + |
| 103 | + req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil) |
| 104 | + if err != nil { |
| 105 | + return nil, nil, err |
| 106 | + } |
| 107 | + |
| 108 | + root := new(Cluster) |
| 109 | + resp, err := s.Client.Do(ctx, req, root) |
| 110 | + if err != nil { |
| 111 | + return nil, resp, err |
| 112 | + } |
| 113 | + |
| 114 | + return root, resp, err |
| 115 | +} |
| 116 | + |
| 117 | +// Create creates one serverless instance in the specified project. |
| 118 | +// |
| 119 | +// See more: https://docs.atlas.mongodb.com/reference/api/serverless/create-one-serverless-instance/ |
| 120 | +func (s *ServerlessInstancesServiceOp) Create(ctx context.Context, projectID string, bodyParams *ServerlessCreateRequestParams) (*Cluster, *Response, error) { |
| 121 | + if projectID == "" { |
| 122 | + return nil, nil, NewArgError("projectID", "must be set") |
| 123 | + } |
| 124 | + |
| 125 | + path := fmt.Sprintf(serverlessInstancesPath, projectID) |
| 126 | + |
| 127 | + req, err := s.Client.NewRequest(ctx, http.MethodPost, path, bodyParams) |
| 128 | + if err != nil { |
| 129 | + return nil, nil, err |
| 130 | + } |
| 131 | + |
| 132 | + root := new(Cluster) |
| 133 | + resp, err := s.Client.Do(ctx, req, root) |
| 134 | + if err != nil { |
| 135 | + return nil, resp, err |
| 136 | + } |
| 137 | + |
| 138 | + return root, resp, err |
| 139 | +} |
| 140 | + |
| 141 | +// Delete deletes one serverless instance in the specified project. |
| 142 | +// |
| 143 | +// See more: https://docs.atlas.mongodb.com/reference/api/serverless/remove-one-serverless-instance/ |
| 144 | +func (s *ServerlessInstancesServiceOp) Delete(ctx context.Context, projectID, instanceName string) (*Response, error) { |
| 145 | + if projectID == "" { |
| 146 | + return nil, NewArgError("projectID", "must be set") |
| 147 | + } |
| 148 | + if instanceName == "" { |
| 149 | + return nil, NewArgError("instanceName", "must be set") |
| 150 | + } |
| 151 | + |
| 152 | + basePath := fmt.Sprintf(serverlessInstancesPath, projectID) |
| 153 | + path := fmt.Sprintf("%s/%s", basePath, instanceName) |
| 154 | + |
| 155 | + req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + resp, err := s.Client.Do(ctx, req, nil) |
| 160 | + |
| 161 | + return resp, err |
| 162 | +} |
0 commit comments