diff --git a/mongodbatlas/project_ip_whitelist.go b/mongodbatlas/project_ip_whitelist.go index 01366b145..82d4ea069 100644 --- a/mongodbatlas/project_ip_whitelist.go +++ b/mongodbatlas/project_ip_whitelist.go @@ -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) } @@ -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. @@ -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 } @@ -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") @@ -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 } @@ -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 { diff --git a/mongodbatlas/project_ip_whitelist_test.go b/mongodbatlas/project_ip_whitelist_test.go index 47c9c3ba7..c031a3845 100644 --- a/mongodbatlas/project_ip_whitelist_test.go +++ b/mongodbatlas/project_ip_whitelist_test.go @@ -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, @@ -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