Skip to content

Commit e981906

Browse files
committed
feat: add whitelist_api_keys service to handle API
1 parent c9c2259 commit e981906

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

mongodbatlas/mongodbatlas.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Client struct {
4242
Peers PeersService
4343
Containers ContainersService
4444
EncryptionsAtRest EncryptionsAtRestService
45+
WhitelistAPIKeys WhitelistAPIKeysService
4546

4647
onRequestCompleted RequestCompletionCallback
4748
}
@@ -144,6 +145,7 @@ func NewClient(httpClient *http.Client) *Client {
144145
c.Peers = &PeersServiceOp{client: c}
145146
c.Containers = &ContainersServiceOp{client: c}
146147
c.EncryptionsAtRest = &EncryptionsAtRestServiceOp{client: c}
148+
c.WhitelistAPIKeys = &WhitelistAPIKeysServiceOp{client: c}
147149

148150
return c
149151
}

mongodbatlas/whitelist_api_keys.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const whitelistAPIKeysPath = "orgs/%s/apiKeys/%s/whitelist"
10+
11+
// WhitelistAPIKeysService is an interface for interfacing with the Whitelist API Keys
12+
// endpoints of the MongoDB Atlas API.
13+
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys/#organization-api-key-endpoints
14+
type WhitelistAPIKeysService interface {
15+
List(context.Context, string, string) (*WhitelistAPIKeys, *Response, error)
16+
Get(context.Context, string, string, string) (*WhitelistAPIKey, *Response, error)
17+
Create(context.Context, string, string, *[]WhitelistAPIKeysReq) (*WhitelistAPIKeys, *Response, error)
18+
Delete(context.Context, string, string, string) (*Response, error)
19+
}
20+
21+
// WhitelistAPIKeysServiceOp handles communication with the Whitelist API keys related methods of the
22+
// MongoDB Atlas API
23+
type WhitelistAPIKeysServiceOp struct {
24+
client *Client
25+
}
26+
27+
var _ WhitelistAPIKeysService = &WhitelistAPIKeysServiceOp{}
28+
29+
// WhitelistAPIKey represents a Whitelist API key.
30+
type WhitelistAPIKey struct {
31+
CidrBlock string `json:"cidrBlock,omitempty"` // CIDR-notated range of whitelisted IP addresses.
32+
Count int `json:"count,omitempty"` // Total number of requests that have originated from this IP address.
33+
Created string `json:"created,omitempty"` // Date this IP address was added to the whitelist.
34+
IPAddress string `json:"ipAddress,omitempty"` // Whitelisted IP address.
35+
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.
36+
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.
37+
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.}
38+
}
39+
40+
// WhitelistAPIKeys represents all Whitelist API keys.
41+
type WhitelistAPIKeys struct {
42+
Results []*WhitelistAPIKey `json:"results,omitempty"` // Includes one WhitelistAPIKey object for each item detailed in the results array section.
43+
Links []*Link `json:"links,omitempty"` // One or more links to sub-resources and/or related resources.
44+
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.
45+
}
46+
47+
// WhitelistAPIKeysReq represents the request to the mehtod create
48+
type WhitelistAPIKeysReq struct {
49+
IPAddress string `json:"ipAddress,omitempty"` // IP address to be added to the whitelist for the API key.
50+
CidrBlock string `json:"cidrBlock,omitempty"` // Whitelist entry in CIDR notation to be added for the API key.
51+
}
52+
53+
// List gets all Whitelist API keys.
54+
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-org-whitelist-get-all/
55+
func (s *WhitelistAPIKeysServiceOp) List(ctx context.Context, orgID string, apiKeyID string) (*WhitelistAPIKeys, *Response, error) {
56+
if orgID == "" {
57+
return nil, nil, NewArgError("orgID", "must be set")
58+
}
59+
if apiKeyID == "" {
60+
return nil, nil, NewArgError("apiKeyID", "must be set")
61+
}
62+
63+
path := fmt.Sprintf(whitelistAPIKeysPath, orgID, apiKeyID)
64+
65+
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
70+
root := new(WhitelistAPIKeys)
71+
resp, err := s.client.Do(ctx, req, root)
72+
if err != nil {
73+
return nil, resp, err
74+
}
75+
76+
if l := root.Links; l != nil {
77+
resp.Links = l
78+
}
79+
80+
return root, resp, nil
81+
}
82+
83+
//Get gets the Whitelist API keys.
84+
//See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-snapshot-get-one/
85+
func (s *WhitelistAPIKeysServiceOp) Get(ctx context.Context, orgID string, apiKeyID string, ipAddress string) (*WhitelistAPIKey, *Response, error) {
86+
if orgID == "" {
87+
return nil, nil, NewArgError("orgID", "must be set")
88+
}
89+
if apiKeyID == "" {
90+
return nil, nil, NewArgError("apiKeyID", "must be set")
91+
}
92+
if ipAddress == "" {
93+
return nil, nil, NewArgError("ipAddress", "must be set")
94+
}
95+
96+
path := fmt.Sprintf(whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress)
97+
98+
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
99+
if err != nil {
100+
return nil, nil, err
101+
}
102+
103+
root := new(WhitelistAPIKey)
104+
resp, err := s.client.Do(ctx, req, root)
105+
if err != nil {
106+
return nil, resp, err
107+
}
108+
109+
return root, resp, err
110+
}
111+
112+
// 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.
113+
// See more: https://docs.atlas.mongodb.com/reference/api/apiKeys-org-whitelist-create/
114+
func (s *WhitelistAPIKeysServiceOp) Create(ctx context.Context, orgID string, apiKeyID string, createRequest *[]WhitelistAPIKeysReq) (*WhitelistAPIKeys, *Response, error) {
115+
if orgID == "" {
116+
return nil, nil, NewArgError("orgID", "must be set")
117+
}
118+
if apiKeyID == "" {
119+
return nil, nil, NewArgError("apiKeyID", "must be set")
120+
}
121+
if createRequest == nil {
122+
return nil, nil, NewArgError("createRequest", "cannot be nil")
123+
}
124+
125+
path := fmt.Sprintf(whitelistAPIKeysPath, orgID, apiKeyID)
126+
127+
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
128+
if err != nil {
129+
return nil, nil, err
130+
}
131+
132+
root := new(WhitelistAPIKeys)
133+
resp, err := s.client.Do(ctx, req, root)
134+
if err != nil {
135+
return nil, resp, err
136+
}
137+
138+
return root, resp, err
139+
}
140+
141+
// Delete deletes the Whitelist API keys.
142+
// See more: https://docs.atlas.mongodb.com/reference/api/cloud-provider-snapshot-delete-one/
143+
func (s *WhitelistAPIKeysServiceOp) Delete(ctx context.Context, orgID string, apiKeyID string, ipAddress string) (*Response, error) {
144+
if orgID == "" {
145+
return nil, NewArgError("groupId", "must be set")
146+
}
147+
if apiKeyID == "" {
148+
return nil, NewArgError("clusterName", "must be set")
149+
}
150+
if ipAddress == "" {
151+
return nil, NewArgError("snapshotId", "must be set")
152+
}
153+
154+
path := fmt.Sprintf(whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress)
155+
156+
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
157+
if err != nil {
158+
return nil, err
159+
}
160+
resp, err := s.client.Do(ctx, req, nil)
161+
162+
return resp, err
163+
}

0 commit comments

Comments
 (0)