-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mongodb/containers
Containers Service
- Loading branch information
Showing
6 changed files
with
507 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.