diff --git a/mongodbatlas/mongodbatlas.go b/mongodbatlas/mongodbatlas.go index 5e5dbef0e..602ab6db0 100644 --- a/mongodbatlas/mongodbatlas.go +++ b/mongodbatlas/mongodbatlas.go @@ -107,7 +107,6 @@ type Client struct { Peers PeersService Containers ContainersService EncryptionsAtRest EncryptionsAtRestService - WhitelistAPIKeys WhitelistAPIKeysService AccessListAPIKeys AccessListAPIKeysService PrivateIPMode PrivateIPModeService MaintenanceWindows MaintenanceWindowsService @@ -255,7 +254,6 @@ func NewClient(httpClient *http.Client) *Client { c.ProjectAPIKeys = &ProjectAPIKeysOp{Client: c} c.Peers = &PeersServiceOp{Client: c} c.ProjectIPAccessList = &ProjectIPAccessListServiceOp{Client: c} - c.WhitelistAPIKeys = &WhitelistAPIKeysServiceOp{Client: c} c.AccessListAPIKeys = &AccessListAPIKeysServiceOp{Client: c} c.PrivateIPMode = &PrivateIPModeServiceOp{Client: c} c.MaintenanceWindows = &MaintenanceWindowsServiceOp{Client: c} diff --git a/mongodbatlas/whitelist_api_keys.go b/mongodbatlas/whitelist_api_keys.go deleted file mode 100644 index 37e940164..000000000 --- a/mongodbatlas/whitelist_api_keys.go +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright 2021 MongoDB Inc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mongodbatlas - -import ( - "context" - "fmt" - "net/http" -) - -const whitelistAPIKeysPath = "api/atlas/v1.0/orgs/%s/apiKeys/%s/whitelist" //nolint:gosec // This is a path - -// 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 -// -// Deprecated: AccessListAPIKeysService replaces WhitelistAPIKeysService. -// Atlas now refers to programmatic API key whitelists as access lists. -// Atlas has deprecated the whitelist method and will disable it in June 2021. -// Please update any dependent work to use WhitelistAPIKeysService. -type WhitelistAPIKeysService interface { - List(context.Context, string, string, *ListOptions) (*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 service - -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, apiKeyID string, listOptions *ListOptions) (*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) - 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(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, apiKeyID, 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, 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, apiKeyID, 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 -} diff --git a/mongodbatlas/whitelist_api_keys_test.go b/mongodbatlas/whitelist_api_keys_test.go deleted file mode 100644 index 3c1d94ac3..000000000 --- a/mongodbatlas/whitelist_api_keys_test.go +++ /dev/null @@ -1,298 +0,0 @@ -// Copyright 2021 MongoDB Inc -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package mongodbatlas - -import ( - "encoding/json" - "fmt" - "net/http" - "testing" - - "github.com/go-test/deep" -) - -func TestWhitelistAPIKeys_List(t *testing.T) { - client, mux, teardown := setup() - defer teardown() - - mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath, orgID, apiKeyID), func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `{ - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100", - "rel": "self" - } - ], - "results": [ - { - "cidrBlock": "147.58.184.16/32", - "count": 0, - "created": "2019-01-24T16:34:57Z", - "ipAddress": "147.58.184.16", - "lastUsed": "2019-01-24T20:18:25Z", - "lastUsedAddress": "147.58.184.16", - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - "rel": "self" - } - ] - }, - { - "cidrBlock": "84.255.48.125/32", - "count": 0, - "created": "2019-01-24T16:26:37Z", - "ipAddress": "84.255.48.125", - "lastUsed": "2019-01-24T20:18:25Z", - "lastUsedAddress": "84.255.48.125", - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/206.252.195.126", - "rel": "self" - } - ] - } - ], - "totalCount": 2 - }`) - }) - - whitelistAPIKeys, _, err := client.WhitelistAPIKeys.List(ctx, orgID, apiKeyID, nil) - if err != nil { - t.Fatalf("WhitelistAPIKeys.List returned error: %v", err) - } - - expected := &WhitelistAPIKeys{ - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100", - Rel: "self", - }, - }, - Results: []*WhitelistAPIKey{ - { - CidrBlock: "147.58.184.16/32", - Count: 0, - Created: "2019-01-24T16:34:57Z", - IPAddress: "147.58.184.16", - LastUsed: "2019-01-24T20:18:25Z", - LastUsedAddress: "147.58.184.16", - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - Rel: "self", - }, - }, - }, - { - CidrBlock: "84.255.48.125/32", - Count: 0, - Created: "2019-01-24T16:26:37Z", - IPAddress: "84.255.48.125", - LastUsed: "2019-01-24T20:18:25Z", - LastUsedAddress: "84.255.48.125", - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/206.252.195.126", - Rel: "self", - }, - }, - }, - }, - TotalCount: 2, - } - - if diff := deep.Equal(whitelistAPIKeys, expected); diff != nil { - t.Error(diff) - } -} - -func TestWhitelistAPIKeys_Get(t *testing.T) { - client, mux, teardown := setup() - defer teardown() - - mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress), func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodGet) - fmt.Fprint(w, `{ - "cidrBlock": "147.58.184.16/32", - "count": 0, - "created": "2019-01-24T16:34:57Z", - "ipAddress": "147.58.184.16", - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - "rel": "self" - } - ] - }`) - }) - - whitelistAPIKey, _, err := client.WhitelistAPIKeys.Get(ctx, orgID, apiKeyID, ipAddress) - if err != nil { - t.Fatalf("WhitelistAPIKeys.Get returned error: %v", err) - } - - expected := &WhitelistAPIKey{ - CidrBlock: "147.58.184.16/32", - Count: 0, - Created: "2019-01-24T16:34:57Z", - IPAddress: "147.58.184.16", - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - Rel: "self", - }, - }, - } - - if diff := deep.Equal(whitelistAPIKey, expected); diff != nil { - t.Error(diff) - } -} - -func TestWhitelistAPIKeys_Create(t *testing.T) { - client, mux, teardown := setup() - defer teardown() - - createRequest := []*WhitelistAPIKeysReq{ - { - IPAddress: "77.54.32.11", - CidrBlock: "77.54.32.11/32", - }, - } - - mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath, orgID, apiKeyID), func(w http.ResponseWriter, r *http.Request) { - expected := []map[string]interface{}{ - { - "ipAddress": "77.54.32.11", - "cidrBlock": "77.54.32.11/32", - }, - } - - var v []map[string]interface{} - err := json.NewDecoder(r.Body).Decode(&v) - if err != nil { - t.Fatalf("Decode json: %v", err) - } - - if diff := deep.Equal(v, expected); diff != nil { - t.Error(diff) - } - - fmt.Fprint(w, `{ - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100", - "rel": "self" - } - ], - "results": [ - { - "cidrBlock": "147.58.184.16/32", - "count": 0, - "created": "2019-01-24T16:34:57Z", - "ipAddress": "147.58.184.16", - "lastUsed": "2019-01-24T20:18:25Z", - "lastUsedAddress": "147.58.184.16", - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - "rel": "self" - } - ] - }, - { - "cidrBlock": "77.54.32.11/32", - "count": 0, - "created": "2019-01-24T16:26:37Z", - "ipAddress": "77.54.32.11", - "lastUsed": "2019-01-24T20:18:25Z", - "lastUsedAddress": "77.54.32.11", - "links": [ - { - "href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/77.54.32.11", - "rel": "self" - } - ] - } - ], - "totalCount": 2 - }`) - }) - - whitelistAPIKey, _, err := client.WhitelistAPIKeys.Create(ctx, orgID, apiKeyID, createRequest) - if err != nil { - t.Fatalf("WhitelistAPIKeys.Create returned error: %v", err) - } - - expected := &WhitelistAPIKeys{ - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100", - Rel: "self", - }, - }, - Results: []*WhitelistAPIKey{ - { - CidrBlock: "147.58.184.16/32", - Count: 0, - Created: "2019-01-24T16:34:57Z", - IPAddress: "147.58.184.16", - LastUsed: "2019-01-24T20:18:25Z", - LastUsedAddress: "147.58.184.16", - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16", - Rel: "self", - }, - }, - }, - { - CidrBlock: "77.54.32.11/32", - Count: 0, - Created: "2019-01-24T16:26:37Z", - IPAddress: "77.54.32.11", - LastUsed: "2019-01-24T20:18:25Z", - LastUsedAddress: "77.54.32.11", - Links: []*Link{ - { - Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/77.54.32.11", - Rel: "self", - }, - }, - }, - }, - TotalCount: 2, - } - - if diff := deep.Equal(whitelistAPIKey, expected); diff != nil { - t.Error(diff) - } -} - -func TestWhitelistAPIKeys_Delete(t *testing.T) { - client, mux, teardown := setup() - defer teardown() - - mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress), func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodDelete) - }) - - _, err := client.WhitelistAPIKeys.Delete(ctx, orgID, apiKeyID, ipAddress) - if err != nil { - t.Fatalf("WhitelistAPIKeys.Delete returned error: %v", err) - } -}