Skip to content

Commit

Permalink
Merge pull request #3 from mongodb/network-peering-connection
Browse files Browse the repository at this point in the history
Peers Service
  • Loading branch information
marinsalinas committed Jul 8, 2019
2 parents 39ff057 + f7c9c3e commit b17cde3
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Client struct {
CloudProviderSnapshots CloudProviderSnapshotsService
APIKeys APIKeysService
CloudProviderSnapshotRestoreJobs CloudProviderSnapshotRestoreJobsService
Peers PeersService

onRequestCompleted RequestCompletionCallback
}
Expand Down Expand Up @@ -136,6 +137,7 @@ func NewClient(httpClient *http.Client) *Client {
c.CloudProviderSnapshots = &CloudProviderSnapshotsServiceOp{client: c}
c.APIKeys = &APIKeysServiceOp{client: c}
c.CloudProviderSnapshotRestoreJobs = &CloudProviderSnapshotRestoreJobsServiceOp{client: c}
c.Peers = &PeersServiceOp{client: c}

return c
}
Expand Down
182 changes: 182 additions & 0 deletions mongodbatlas/peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package mongodbatlas

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

const peersPath = "groups/%s/peers"

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

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

var _ PeersService = &PeersServiceOp{}

// Peer represents MongoDB peer connection.
type Peer struct {
AccepterRegionName string `json:"accepterRegionName,omitempty"`
AWSAccountId string `json:"awsAccountId,omitempty"`
ConnectionID string `json:"connectionId,omitempty"`
ContainerID string `json:"containerId,omitempty"`
ErrorStateName string `json:"errorStateName,omitempty"`
ID string `json:"id,omitempty"`
ProviderName string `json:"providerName,omitempty"`
RouteTableCIDRBlock string `json:"routeTableCidrBlock,omitempty"`
StatusName string `json:"statusName,omitempty"`
VpcID string `json:"vpcId,omitempty"`
AtlasCIDRBlock string `json:"atlasCidrBlock,omitempty"`
AzureDirectoryID string `json:"azureDirectoryId,omitempty"`
AzureSubscriptionId string `json:"azureSubscriptionId,omitempty"`
ResourceGroupName string `json:"resourceGroupName,omitempty"`
VNetName string `json:"vnetName,omitempty"`
ErrorState string `json:"errorState,omitempty"`
Status string `json:"status,omitempty"`
GCPProjectID string `json:"gcpProjectId,omitempty"`
NetworkName string `json:"networkName,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
}

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

//List all peers in the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-get-connections-list/
func (s *PeersServiceOp) List(ctx context.Context, groupID string, listOptions *ListOptions) ([]Peer, *Response, error) {
path := fmt.Sprintf(peersPath, 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(peersResponse)
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 netwprk peering connection specified to {PEER-ID} from the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/vpc-get-connection/
func (s *PeersServiceOp) Get(ctx context.Context, groupID string, peerID string) (*Peer, *Response, error) {
if peerID == "" {
return nil, nil, NewArgError("perrID", "must be set")
}

basePath := fmt.Sprintf(peersPath, groupID)
escapedEntry := url.PathEscape(peerID)
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(Peer)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

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

path := fmt.Sprintf(peersPath, groupID)

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

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

return root, resp, err
}

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

basePath := fmt.Sprintf(peersPath, groupID)
path := fmt.Sprintf("%s/%s", basePath, peerID)

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

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

return root, resp, err
}

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

basePath := fmt.Sprintf(peersPath, groupID)
escapedEntry := url.PathEscape(peerID)
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
}
Loading

0 comments on commit b17cde3

Please sign in to comment.