Skip to content

Commit

Permalink
Merge pull request #4 from mongodb/containers
Browse files Browse the repository at this point in the history
Containers Service
  • Loading branch information
marinsalinas committed Jul 8, 2019
2 parents b17cde3 + 10c8272 commit 1e64eac
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 7 deletions.
3 changes: 1 addition & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ build: fmtcheck


test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4
go test $(TEST) -timeout=30s -parallel=4 -cover

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w ./main.go
gofmt -s -w ./$(PKG_NAME)

fmtcheck:
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# go-client-mongodb-atlas
# go-client-mongodb-atlas [![Build Status](https://travis-ci.org/mongodb/go-client-mongodb-atlas.svg?branch=master)](https://travis-ci.org/mongodb/go-client-mongodb-atlas)

A Go HTTP client for the [MongoDB Atlas API](https://docs.atlas.mongodb.com/api/).


## Contribution and Development

```
make tools
make test
```
173 changes: 173 additions & 0 deletions mongodbatlas/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package mongodbatlas

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

const containersPath = "groups/%s/containers"

//ContainersService is an interface for interfacing with the Network Peering Containers
// endpoints of the MongoDB Atlas API.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc/
type ContainersService interface {
List(context.Context, string, *ListOptions) ([]Container, *Response, error)
Get(context.Context, string, string) (*Container, *Response, error)
Create(context.Context, string, *Container) (*Container, *Response, error)
Update(context.Context, string, string, *Container) (*Container, *Response, error)
Delete(context.Context, string, string) (*Response, error)
}

//ContainersServiceOp handles communication with the Network Peering Container related methods
// of the MongoDB Atlas API
type ContainersServiceOp struct {
client *Client
}

var _ ContainersService = &ContainersServiceOp{}

// Container represents MongoDB network peering containter.
type Container struct {
AtlasCIDRBlock string `json:"atlasCidrBlock,omitempty"`
AzureSubscriptionID string `json:"azureSubscriptionId,omitempty"`
GCPProjectID string `json:"gcpProjectId,omitempty"`
ID string `json:"id,omitempty"`
NetworkName string `json:"networkName,omitempty"`
ProviderName string `json:"providerName,omitempty"`
Provisioned *bool `json:"provisioned,omitempty"`
Region string `json:"region,omitempty"`
RegionName string `json:"regionName,omitempty"`
VNetName string `json:"vnetName,omitempty"`
VPCID string `json:"vpcId,omitempty"`
}

// containersResponse is the response from the ContainersService.List.
type containersResponse struct {
Links []*Link `json:"links,omitempty"`
Results []Container `json:"results,omitempty"`
TotalCount int `json:"totalCount,omitempty"`
}

//List all containers in the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-get-containers-list/
func (s *ContainersServiceOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]Container, *Response, error) {
path := fmt.Sprintf(containersPath, groupID)

//Add query params from listOptions
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(containersResponse)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

if l := root.Links; l != nil {
resp.Links = l
}

return root.Results, resp, nil
}

//Get gets the network peering container specified to {CONTAINER-ID} from the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-get-container/
func (s *ContainersServiceOp) Get(ctx context.Context, groupID string, containerID string) (*Container, *Response, error) {
if containerID == "" {
return nil, nil, NewArgError("perrID", "must be set")
}

basePath := fmt.Sprintf(containersPath, groupID)
escapedEntry := url.PathEscape(containerID)
path := fmt.Sprintf("%s/%s", basePath, escapedEntry)

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

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

return root, resp, err
}

//Add a network peering container to the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-create-container/
func (s *ContainersServiceOp) Create(ctx context.Context, groupID string, createRequest *Container) (*Container, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

path := fmt.Sprintf(containersPath, groupID)

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

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

return root, resp, err
}

//Update a network peering container in the project associated to {GROUP-ID}
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-update-container/
func (s *ContainersServiceOp) Update(ctx context.Context, groupID string, containerID string, updateRequest *Container) (*Container, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}

basePath := fmt.Sprintf(containersPath, groupID)
path := fmt.Sprintf("%s/%s", basePath, containerID)

req, err := s.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return nil, nil, err
}

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

return root, resp, err
}

//Delete the network peering container specified to {CONTAINER-ID} from the project associated to {GROUP-ID}.
// See more: https://docs.atlas.mongodb.com/reference/api/vpc-delete-one-container/
func (s *ContainersServiceOp) Delete(ctx context.Context, groupID string, containerID string) (*Response, error) {
if containerID == "" {
return nil, NewArgError("containerID", "must be set")
}

basePath := fmt.Sprintf(containersPath, groupID)
escapedEntry := url.PathEscape(containerID)
path := fmt.Sprintf("%s/%s", basePath, escapedEntry)

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
}

0 comments on commit 1e64eac

Please sign in to comment.