Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist #35

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 33 additions & 33 deletions mongodbatlas/project_ip_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ProjectIPWhitelistService interface {
List(context.Context, string, *ListOptions) ([]ProjectIPWhitelist, *Response, error)
Get(context.Context, string, string) (*ProjectIPWhitelist, *Response, error)
Create(context.Context, string, []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error)
Update(context.Context, string, string, []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error)
Update(context.Context, string, []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error)
Delete(context.Context, string, string) (*Response, error)
}

Expand All @@ -30,11 +30,12 @@ var _ ProjectIPWhitelistService = &ProjectIPWhitelistServiceOp{}

// ProjectIPWhitelist represents MongoDB project's IP whitelist.
type ProjectIPWhitelist struct {
Comment string `json:"comment,omitempty"`
GroupID string `json:"groupId,omitempty"`
CIDRBlock string `json:"cidrBlock,omitempty"`
IPAddress string `json:"ipAddress,omitempty"`
DeleteAfterDate string `json:"deleteAfterDate,omitempty"`
GroupID string `json:"groupId,omitempty"` // The unique identifier for the project for which you want to update one or more whitelist entries.
AwsSecurityGroup string `json:"awsSecurityGroup,omitempty"` // ID of the whitelisted AWS security group to update. Mutually exclusive with cidrBlock and ipAddress.
CIDRBlock string `json:"cidrBlock,omitempty"` // Whitelist entry in Classless Inter-Domain Routing (CIDR) notation to update. Mutually exclusive with awsSecurityGroup and ipAddress.
IPAddress string `json:"ipAddress,omitempty"` // Whitelisted IP address to update. Mutually exclusive with awsSecurityGroup and cidrBlock.
Comment string `json:"comment,omitempty"` // Optional The comment associated with the whitelist entry. Specify an empty string "" to delete the comment associated to an IP address.
DeleteAfterDate string `json:"deleteAfterDate,omitempty"` // Optional The ISO-8601-formatted UTC date after which Atlas removes the entry from the whitelist. The specified date must be in the future and within one week of the time you make the API request. To update a temporary whitelist entry to be permanent, set the value of this field to null
}

// projectIPWhitelistsResponse is the response from the ProjectIPWhitelistService.List.
Expand All @@ -44,18 +45,16 @@ type projectIPWhitelistsResponse struct {
TotalCount int `json:"totalCount"`
}

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

//Add query params from listOptions
path, err := setListOptions(path, listOptions)
if err != nil {
return nil, nil, err
// Create adds one or more whitelist entries to the project associated to {GROUP-ID}.
// See more: https://docs.atlas.mongodb.com/reference/api/database-users-create-a-user/
func (s *ProjectIPWhitelistServiceOp) Create(ctx context.Context, groupID string, createRequest []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
path := fmt.Sprintf(projectIPWhitelistPath, groupID)

req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
Expand All @@ -70,11 +69,11 @@ func (s *ProjectIPWhitelistServiceOp) List(ctx context.Context, groupID string,
resp.Links = l
}

return root.Results, resp, nil
return root.Results, resp, err
}

//Get gets the whitelist entry specified to {WHITELIST-ENTRY} from the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/whitelist-get-one-entry/
// Get gets the whitelist entry specified to {WHITELIST-ENTRY} from the project associated to {GROUP-ID}.
// See more: https://docs.atlas.mongodb.com/reference/api/whitelist-get-one-entry/
func (s *ProjectIPWhitelistServiceOp) Get(ctx context.Context, groupID string, whiteListEntry string) (*ProjectIPWhitelist, *Response, error) {
if whiteListEntry == "" {
return nil, nil, NewArgError("whiteListEntry", "must be set")
Expand All @@ -98,16 +97,18 @@ func (s *ProjectIPWhitelistServiceOp) Get(ctx context.Context, groupID string, w
return root, resp, err
}

//Add one or more whitelist entries to the project associated to {GROUP-ID}.
//See more: https://docs.atlas.mongodb.com/reference/api/database-users-create-a-user/
func (s *ProjectIPWhitelistServiceOp) Create(ctx context.Context, groupID string, createRequest []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}

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

req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
// 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
}
Expand All @@ -122,18 +123,17 @@ func (s *ProjectIPWhitelistServiceOp) Create(ctx context.Context, groupID string
resp.Links = l
}

return root.Results, resp, err
return root.Results, resp, nil
}

//Update one or more whitelist entries in the project associated to {GROUP-ID}
//See more: https://docs.atlas.mongodb.com/reference/api/whitelist-update-one/
func (s *ProjectIPWhitelistServiceOp) Update(ctx context.Context, groupID string, whitelistEntry string, updateRequest []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error) {
// Update one or more whitelist entries in the project associated to {GROUP-ID}
// See more: https://docs.atlas.mongodb.com/reference/api/whitelist-update-one/
func (s *ProjectIPWhitelistServiceOp) Update(ctx context.Context, groupID string, updateRequest []*ProjectIPWhitelist) ([]ProjectIPWhitelist, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}

basePath := fmt.Sprintf(projectIPWhitelistPath, groupID)
path := fmt.Sprintf("%s/%s", basePath, whitelistEntry)
path := fmt.Sprintf(projectIPWhitelistPath, groupID)

req, err := s.client.NewRequest(ctx, http.MethodPost, path, updateRequest)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions mongodbatlas/project_ip_whitelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestProjectIPWhitelist_Update(t *testing.T) {
IPAddress: ipAddress,
}}

mux.HandleFunc(fmt.Sprintf("/groups/%s/whitelist/%s", groupID, ipAddress), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/groups/%s/whitelist", groupID), func(w http.ResponseWriter, r *http.Request) {
expected := []map[string]interface{}{{
"ipAddress": ipAddress,
"groupId": groupID,
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestProjectIPWhitelist_Update(t *testing.T) {
fmt.Fprint(w, jsonBlob)
})

projectIPWhitelist, _, err := client.ProjectIPWhitelist.Update(ctx, groupID, ipAddress, createRequest)
projectIPWhitelist, _, err := client.ProjectIPWhitelist.Update(ctx, groupID, createRequest)
if err != nil {
t.Errorf("ProjectIPWhitelist.Update returned error: %v", err)
return
Expand Down