Skip to content

Commit

Permalink
Merge pull request #11 from mongodb/white_list_api_keys
Browse files Browse the repository at this point in the history
Whitelist API Keys
  • Loading branch information
marinsalinas authored Aug 2, 2019
2 parents ae553f9 + 88995b3 commit f71d72e
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 11 deletions.
13 changes: 7 additions & 6 deletions mongodbatlas/api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

const apiKeysPath = "orgs/%s/apiKeys"

//APIKeysService is an interface for interfacing with the APIKeys
// APIKeysService is an interface for interfacing with the APIKeys
// endpoints of the MongoDB Atlas API.
//See more: https://docs.atlas.mongodb.com/reference/api/clusters/
//See more: https://docs.atlas.mongodb.com/reference/api/apiKeys/
type APIKeysService interface {
List(context.Context, string, *ListOptions) ([]APIKey, *Response, error)
Get(context.Context, string, string) (*APIKey, *Response, error)
Expand All @@ -20,21 +20,21 @@ type APIKeysService interface {
Delete(context.Context, string, string) (*Response, error)
}

//APIKeysServiceOp handles communication with the APIKey related methods
// APIKeysServiceOp handles communication with the APIKey related methods
// of the MongoDB Atlas API
type APIKeysServiceOp struct {
client *Client
}

var _ APIKeysService = &APIKeysServiceOp{}

// APIKeyInput represents MongoDB cluster input reuest for Create and Update.
// APIKeyInput represents MongoDB API key input request for Create.
type APIKeyInput struct {
Desc string `json:"desc,omitempty"`
Roles []string `json:"roles,omitempty"`
}

// APIKey represents MongoDB cluster.
// APIKey represents MongoDB API Key.
type APIKey struct {
ID string `json:"id,omitempty"`
Desc string `json:"desc,omitempty"`
Expand All @@ -43,6 +43,7 @@ type APIKey struct {
PublicKey string `json:"publicKey,omitempty"`
}

// APIKeyRole represents a role name of API key
type APIKeyRole struct {
GroupID string `json:"groupId,omitempty"`
OrgID string `json:"orgId,omitempty"`
Expand Down Expand Up @@ -158,7 +159,7 @@ func (s *APIKeysServiceOp) Update(ctx context.Context, orgID string, apiKeyID st
}

//Delete the API Key specified to {API-KEY-ID} from the organization associated to {ORG-ID}.
// See more: https://docs.atlas.mongodb.com/reference/api/clusters-delete-one/
// See more: https://docs.atlas.mongodb.com/reference/api/apiKey-delete-one-apiKey/
func (s *APIKeysServiceOp) Delete(ctx context.Context, orgID string, apiKeyID string) (*Response, error) {
if apiKeyID == "" {
return nil, NewArgError("apiKeyID", "must be set")
Expand Down
2 changes: 2 additions & 0 deletions mongodbatlas/mongodbatlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Client struct {
Peers PeersService
Containers ContainersService
EncryptionsAtRest EncryptionsAtRestService
WhitelistAPIKeys WhitelistAPIKeysService

onRequestCompleted RequestCompletionCallback
}
Expand Down Expand Up @@ -144,6 +145,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Peers = &PeersServiceOp{client: c}
c.Containers = &ContainersServiceOp{client: c}
c.EncryptionsAtRest = &EncryptionsAtRestServiceOp{client: c}
c.WhitelistAPIKeys = &WhitelistAPIKeysServiceOp{client: c}

return c
}
Expand Down
10 changes: 5 additions & 5 deletions mongodbatlas/project_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *ProjectAPIKeysOp) List(ctx context.Context, groupID string, listOptions
}

//Create an API Key by the {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-orgs-create-one/
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/create-one-apiKey-in-one-project/
func (s *ProjectAPIKeysOp) Create(ctx context.Context, groupID string, createRequest *APIKeyInput) (*APIKey, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
Expand All @@ -78,8 +78,8 @@ func (s *ProjectAPIKeysOp) Create(ctx context.Context, groupID string, createReq
return root, resp, err
}

//Assign an API-KEY related to {GROUP-ID} to a the project with {PROJECT-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-orgs-get-all/
//Assign an API-KEY related to {GROUP-ID} to a the project with {API-KEY-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/assign-one-org-apiKey-to-one-project/
func (s *ProjectAPIKeysOp) Assign(ctx context.Context, groupID string, keyID string) (*Response, error) {
if groupID == "" {
return nil, NewArgError("apiKeyID", "must be set")
Expand All @@ -103,8 +103,8 @@ func (s *ProjectAPIKeysOp) Assign(ctx context.Context, groupID string, keyID str
return resp, err
}

//Unassign an API-KEY related to {GROUP-ID} to a the project with {PROJECT-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-orgs-get-all/
//Unassign an API-KEY related to {GROUP-ID} to a the project with {API-KEY-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/projectApiKeys/delete-one-apiKey-in-one-project/
func (s *ProjectAPIKeysOp) Unassign(ctx context.Context, groupID string, keyID string) (*Response, error) {
if groupID == "" {
return nil, NewArgError("apiKeyID", "must be set")
Expand Down
163 changes: 163 additions & 0 deletions mongodbatlas/whitelist_api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package mongodbatlas

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

const whitelistAPIKeysPath = "orgs/%s/apiKeys/%s/whitelist"

// WhitelistAPIKeysService is an interface for interfacing with the Whitelist API Keys
// endpoints of the MongoDB Atlas API.
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys/#organization-api-key-endpoints
type WhitelistAPIKeysService interface {
List(context.Context, string, string) (*WhitelistAPIKeys, *Response, error)
Get(context.Context, string, string, string) (*WhitelistAPIKey, *Response, error)
Create(context.Context, string, string, *[]WhitelistAPIKeysReq) (*WhitelistAPIKeys, *Response, error)
Delete(context.Context, string, string, string) (*Response, error)
}

// WhitelistAPIKeysServiceOp handles communication with the Whitelist API keys related methods of the
// MongoDB Atlas API
type WhitelistAPIKeysServiceOp struct {
client *Client
}

var _ WhitelistAPIKeysService = &WhitelistAPIKeysServiceOp{}

// WhitelistAPIKey represents a Whitelist API key.
type WhitelistAPIKey struct {
CidrBlock string `json:"cidrBlock,omitempty"` // CIDR-notated range of whitelisted IP addresses.
Count int `json:"count,omitempty"` // Total number of requests that have originated from this IP address.
Created string `json:"created,omitempty"` // Date this IP address was added to the whitelist.
IPAddress string `json:"ipAddress,omitempty"` // Whitelisted IP address.
LastUsed string `json:"lastUsed,omitempty"` // Date of the most recent request that originated from this IP address. This field only appears if at least one request has originated from this IP address, and is only updated when a whitelisted resource is accessed.
LastUsedAddress string `json:"lastUsedAddress,omitempty"` // IP address from which the last call to the API was issued. This field only appears if at least one request has originated from this IP address.
Links []*Link `json:"links,omitempty"` // An array of documents, representing a link to one or more sub-resources and/or related resources such as list pagination. See Linking for more information.}
}

// WhitelistAPIKeys represents all Whitelist API keys.
type WhitelistAPIKeys struct {
Results []*WhitelistAPIKey `json:"results,omitempty"` // Includes one WhitelistAPIKey object for each item detailed in the results array section.
Links []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources.
TotalCount int `json:"totalCount,omitempty"` // Count of the total number of items in the result set. It may be greater than the number of objects in the results array if the entire result set is paginated.
}

// WhitelistAPIKeysReq represents the request to the mehtod create
type WhitelistAPIKeysReq struct {
IPAddress string `json:"ipAddress,omitempty"` // IP address to be added to the whitelist for the API key.
CidrBlock string `json:"cidrBlock,omitempty"` // Whitelist entry in CIDR notation to be added for the API key.
}

// List gets all Whitelist API keys.
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-org-whitelist-get-all/
func (s *WhitelistAPIKeysServiceOp) List(ctx context.Context, orgID string, apiKeyID string) (*WhitelistAPIKeys, *Response, error) {
if orgID == "" {
return nil, nil, NewArgError("orgID", "must be set")
}
if apiKeyID == "" {
return nil, nil, NewArgError("apiKeyID", "must be set")
}

path := fmt.Sprintf(whitelistAPIKeysPath, orgID, apiKeyID)

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

root := new(WhitelistAPIKeys)
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, resp, nil
}

//Get gets the Whitelist API keys.
//See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-snapshot-get-one/
func (s *WhitelistAPIKeysServiceOp) Get(ctx context.Context, orgID string, apiKeyID string, ipAddress string) (*WhitelistAPIKey, *Response, error) {
if orgID == "" {
return nil, nil, NewArgError("orgID", "must be set")
}
if apiKeyID == "" {
return nil, nil, NewArgError("apiKeyID", "must be set")
}
if ipAddress == "" {
return nil, nil, NewArgError("ipAddress", "must be set")
}

path := fmt.Sprintf(whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress)

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

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

return root, resp, err
}

// Create a submit a POST request containing ipAddress or cidrBlock values which are not already present in the whitelist, Atlas adds those entries to the list of existing entries in the whitelist.
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-org-whitelist-create/
func (s *WhitelistAPIKeysServiceOp) Create(ctx context.Context, orgID string, apiKeyID string, createRequest *[]WhitelistAPIKeysReq) (*WhitelistAPIKeys, *Response, error) {
if orgID == "" {
return nil, nil, NewArgError("orgID", "must be set")
}
if apiKeyID == "" {
return nil, nil, NewArgError("apiKeyID", "must be set")
}
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

path := fmt.Sprintf(whitelistAPIKeysPath, orgID, apiKeyID)

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

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

return root, resp, err
}

// Delete deletes the Whitelist API keys.
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-snapshot-delete-one/
func (s *WhitelistAPIKeysServiceOp) Delete(ctx context.Context, orgID string, apiKeyID string, ipAddress string) (*Response, error) {
if orgID == "" {
return nil, NewArgError("groupId", "must be set")
}
if apiKeyID == "" {
return nil, NewArgError("clusterName", "must be set")
}
if ipAddress == "" {
return nil, NewArgError("snapshotId", "must be set")
}

path := fmt.Sprintf(whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress)

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 f71d72e

Please sign in to comment.