Skip to content

Commit

Permalink
CLOUDP-96138: Add serverless to the atlas go client (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Jul 23, 2021
1 parent eb731fe commit 34d12ee
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mongodbatlas/accesslist_api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *AccessListAPIKeysServiceOp) List(ctx context.Context, orgID, apiKeyID s
return root, resp, nil
}

// Get retrieve information on a single API Key access list entry using the unique identifier for the API Key and desired permitted address.
// Get retrieves information on a single API Key access list entry using the unique identifier for the API Key and desired permitted address.
//
// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/get-one-api-access-entry/
func (s *AccessListAPIKeysServiceOp) Get(ctx context.Context, orgID, apiKeyID, ipAddress string) (*AccessListAPIKey, *Response, error) {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s *AccessListAPIKeysServiceOp) Get(ctx context.Context, orgID, apiKeyID, i
return root, resp, err
}

// Create one or more new access list entries for the specified API Key.
// Create creates one or more new access list entries for the specified API Key.
//
// See more: https://docs.atlas.mongodb.com/reference/api/api-access-list/create-api-access-entries/
func (s *AccessListAPIKeysServiceOp) Create(ctx context.Context, orgID, apiKeyID string, createRequest []*AccessListAPIKeysReq) (*AccessListAPIKeys, *Response, error) {
Expand Down
2 changes: 2 additions & 0 deletions mongodbatlas/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type Cluster struct {
MongoURIUpdated string `json:"mongoURIUpdated,omitempty"`
MongoURIWithOptions string `json:"mongoURIWithOptions,omitempty"`
Name string `json:"name,omitempty"`
CreateDate string `json:"createDate,omitempty"`
NumShards *int64 `json:"numShards,omitempty"`
Paused *bool `json:"paused,omitempty"`
PitEnabled *bool `json:"pitEnabled,omitempty"`
Expand All @@ -164,6 +165,7 @@ type Cluster struct {
SrvAddress string `json:"srvAddress,omitempty"`
StateName string `json:"stateName,omitempty"`
ConnectionStrings *ConnectionStrings `json:"connectionStrings,omitempty"`
Links []*Link `json:"links,omitempty"`
}

// ProcessArgs represents the advanced configuration options for the cluster.
Expand Down
5 changes: 5 additions & 0 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type Client struct {
DefaultMongoDBMajorVersion DefaultMongoDBMajorVersionService
IPInfo IPInfoService
AdvancedClusters AdvancedClustersService
ServerlessInstances ServerlessInstancesService

onRequestCompleted RequestCompletionCallback
}
Expand Down Expand Up @@ -167,6 +168,9 @@ type ListOptions struct {

// For paginated result sets, the number of results to include per page.
ItemsPerPage int `url:"itemsPerPage,omitempty"`

// Flag that indicates whether Atlas returns the totalCount parameter in the response body.
IncludeCount bool `url:"includeCount,omitempty"`
}

func (resp *Response) getCurrentPageLink() (*Link, error) {
Expand Down Expand Up @@ -270,6 +274,7 @@ func NewClient(httpClient *http.Client) *Client {
c.DefaultMongoDBMajorVersion = &DefaultMongoDBMajorVersionServiceOp{Client: c}
c.IPInfo = &IPInfoServiceOp{Client: c}
c.AdvancedClusters = &AdvancedClustersServiceOp{Client: c}
c.ServerlessInstances = &ServerlessInstancesServiceOp{Client: c}

return c
}
Expand Down
162 changes: 162 additions & 0 deletions mongodbatlas/serverless_instances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2021 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 mongodbatlas

import (
"context"
"fmt"
"net/http"
)

const serverlessInstancesPath = "api/atlas/v1.0/groups/%s/serverless"

// ServerlessInstancesService is an interface for interfacing with the Serverless Instances endpoints of the MongoDB Atlas API.
//
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-one-serverless-instance/
type ServerlessInstancesService interface {
List(context.Context, string, *ListOptions) (*ClustersResponse, *Response, error)
Get(context.Context, string, string) (*Cluster, *Response, error)
Create(context.Context, string, *ServerlessCreateRequestParams) (*Cluster, *Response, error)
Delete(context.Context, string, string) (*Response, error)
}

// ServerlessInstancesServiceOp handles communication with the Serverless Instances related methods of the MongoDB Atlas API.
type ServerlessInstancesServiceOp service

var _ ServerlessInstancesService = &ServerlessInstancesServiceOp{}

// ClustersResponse represents the response of ServerlessInstancesService.List.
type ClustersResponse struct {
Links []*Link `json:"links,omitempty"`
Results []*Cluster `json:"results,omitempty"`
TotalCount int `json:"totalCount,omitempty"`
}

// ServerlessCreateRequestParams represents the Request Body Parameters of ServerlessInstancesService.Create.
type ServerlessCreateRequestParams struct {
Name string `json:"name,omitempty"`
ProviderSettings *ServerlessProviderSettings `json:"providerSettings,omitempty"`
}

// ServerlessProviderSettings represents the Provider Settings of serverless instances.
type ServerlessProviderSettings struct {
BackingProviderName string `json:"backingProviderName,omitempty"`
ProviderName string `json:"providerName,omitempty"`
RegionName string `json:"regionName,omitempty"`
}

// List gets all serverless instances in the specified project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-all-serverless-instances/
func (s *ServerlessInstancesServiceOp) List(ctx context.Context, projectID string, listOptions *ListOptions) (*ClustersResponse, *Response, error) {
if projectID == "" {
return nil, nil, NewArgError("projectID", "must be set")
}

path := fmt.Sprintf(serverlessInstancesPath, projectID)
path, err := setListOptions(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(ClustersResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, nil
}

// Get retrieves one serverless instance in the specified project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-one-serverless-instance/
func (s *ServerlessInstancesServiceOp) Get(ctx context.Context, projectID, instanceName string) (*Cluster, *Response, error) {
if projectID == "" {
return nil, nil, NewArgError("projectID", "must be set")
}

if instanceName == "" {
return nil, nil, NewArgError("instanceName", "must be set")
}

basePath := fmt.Sprintf(serverlessInstancesPath, projectID)
path := fmt.Sprintf("%s/%s", basePath, instanceName)

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(Cluster)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

// Create creates one serverless instance in the specified project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/create-one-serverless-instance/
func (s *ServerlessInstancesServiceOp) Create(ctx context.Context, projectID string, bodyParams *ServerlessCreateRequestParams) (*Cluster, *Response, error) {
if projectID == "" {
return nil, nil, NewArgError("projectID", "must be set")
}

path := fmt.Sprintf(serverlessInstancesPath, projectID)

req, err := s.Client.NewRequest(ctx, http.MethodPost, path, bodyParams)
if err != nil {
return nil, nil, err
}

root := new(Cluster)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

// Delete deletes one serverless instance in the specified project.
//
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/remove-one-serverless-instance/
func (s *ServerlessInstancesServiceOp) Delete(ctx context.Context, projectID, instanceName string) (*Response, error) {
if projectID == "" {
return nil, NewArgError("projectID", "must be set")
}
if instanceName == "" {
return nil, NewArgError("instanceName", "must be set")
}

basePath := fmt.Sprintf(serverlessInstancesPath, projectID)
path := fmt.Sprintf("%s/%s", basePath, instanceName)

req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := s.Client.Do(ctx, req, nil)

return resp, err
}
Loading

0 comments on commit 34d12ee

Please sign in to comment.