Skip to content

Commit e50596d

Browse files
authored
Merge pull request #139 from mongodb/access-list
feat: add access list API
2 parents 15be5f7 + cfb1e0b commit e50596d

File tree

3 files changed

+392
-0
lines changed

3 files changed

+392
-0
lines changed

mongodbatlas/mongodbatlas.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Client struct {
6666
CustomDBRoles CustomDBRolesService
6767
DatabaseUsers DatabaseUsersService
6868
ProjectIPWhitelist ProjectIPWhitelistService
69+
ProjectIPAccessList ProjectIPAccessListService
6970
Organizations OrganizationsService
7071
Projects ProjectsService
7172
Clusters ClustersService
@@ -215,6 +216,7 @@ func NewClient(httpClient *http.Client) *Client {
215216
c.ProjectAPIKeys = &ProjectAPIKeysOp{Client: c}
216217
c.Peers = &PeersServiceOp{Client: c}
217218
c.ProjectIPWhitelist = &ProjectIPWhitelistServiceOp{Client: c}
219+
c.ProjectIPAccessList = &ProjectIPAccessListServiceOp{Client: c}
218220
c.WhitelistAPIKeys = &WhitelistAPIKeysServiceOp{Client: c}
219221
c.PrivateIPMode = &PrivateIPModeServiceOp{Client: c}
220222
c.MaintenanceWindows = &MaintenanceWindowsServiceOp{Client: c}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
const projectIPAccessListPath = "groups/%s/accessList"
11+
12+
// ProjectIPAccessListService provides access to the project access list related functions in the Atlas API.
13+
//
14+
// See more: https://docs.atlas.mongodb.com/reference/api/organizations/
15+
type ProjectIPAccessListService interface {
16+
List(context.Context, string, *ListOptions) (*ProjectIPAccessLists, *Response, error)
17+
Get(context.Context, string, string) (*ProjectIPAccessList, *Response, error)
18+
Create(context.Context, string, []*ProjectIPAccessList) (*ProjectIPAccessLists, *Response, error)
19+
Delete(context.Context, string, string) (*Response, error)
20+
}
21+
22+
// ProjectIPAccessListServiceOp provides an implementation of the ProjectIPAccessListService interface
23+
type ProjectIPAccessListServiceOp service
24+
25+
var _ ProjectIPAccessListService = &ProjectIPAccessListServiceOp{}
26+
27+
// ProjectIPAccessList represents MongoDB project's IP access list.
28+
type ProjectIPAccessList struct {
29+
AwsSecurityGroup string `json:"awsSecurityGroup,omitempty"` // Unique identifier of AWS security group in this access list entry.
30+
CIDRBlock string `json:"cidrBlock,omitempty"` // Range of IP addresses in CIDR notation in this access list entry.
31+
Comment string `json:"comment,omitempty"` // Comment associated with this access list entry.
32+
DeleteAfterDate string `json:"deleteAfterDate,omitempty"` // Timestamp in ISO 8601 date and time format in UTC after which Atlas deletes the temporary access list entry. Atlas returns this field if you specified an expiration date when creating this access list entry.
33+
GroupID string `json:"groupId,omitempty"` // Unique identifier of the project to which this access list entry applies.
34+
IPAddress string `json:"ipAddress,omitempty"` // Entry using an IP address in this access list entry.
35+
}
36+
37+
// ProjectIPAccessLists is the response from the ProjectIPAccessListService.List.
38+
type ProjectIPAccessLists struct {
39+
Links []*Link `json:"links"`
40+
Results []ProjectIPAccessList `json:"results"`
41+
TotalCount int `json:"totalCount"`
42+
}
43+
44+
// List all access list entries in the project associated to {PROJECT-ID}.
45+
//
46+
// See more: https://docs.atlas.mongodb.com/reference/api/ip-access-list/get-all-access-list-entries/
47+
func (s *ProjectIPAccessListServiceOp) List(ctx context.Context, groupID string, listOptions *ListOptions) (*ProjectIPAccessLists, *Response, error) {
48+
path := fmt.Sprintf(projectIPAccessListPath, groupID)
49+
50+
// Add query params from listOptions
51+
path, err := setListOptions(path, listOptions)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
56+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
61+
root := new(ProjectIPAccessLists)
62+
resp, err := s.Client.Do(ctx, req, root)
63+
if err != nil {
64+
return nil, resp, err
65+
}
66+
67+
if l := root.Links; l != nil {
68+
resp.Links = l
69+
}
70+
71+
return root, resp, nil
72+
}
73+
74+
// Get the access list entry specified to {ACCESS-LIST-ENTRY} from the project associated to {PROJECT-ID}.
75+
//
76+
// See more: https://docs.atlas.mongodb.com/reference/api/ip-access-list/get-one-access-list-entry/
77+
func (s *ProjectIPAccessListServiceOp) Get(ctx context.Context, groupID, entry string) (*ProjectIPAccessList, *Response, error) {
78+
if entry == "" {
79+
return nil, nil, NewArgError("entry", "must be set")
80+
}
81+
82+
basePath := fmt.Sprintf(projectIPAccessListPath, groupID)
83+
escapedEntry := url.PathEscape(entry)
84+
path := fmt.Sprintf("%s/%s", basePath, escapedEntry)
85+
86+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
87+
if err != nil {
88+
return nil, nil, err
89+
}
90+
91+
root := new(ProjectIPAccessList)
92+
resp, err := s.Client.Do(ctx, req, root)
93+
if err != nil {
94+
return nil, resp, err
95+
}
96+
97+
return root, resp, err
98+
}
99+
100+
// Create adds one or more access list entries to the project associated to {PROJECT-ID}.
101+
//
102+
// See more: https://docs.atlas.mongodb.com/reference/api/ip-access-list/add-entries-to-access-list/
103+
func (s *ProjectIPAccessListServiceOp) Create(ctx context.Context, groupID string, createRequest []*ProjectIPAccessList) (*ProjectIPAccessLists, *Response, error) {
104+
if createRequest == nil {
105+
return nil, nil, NewArgError("createRequest", "cannot be nil")
106+
}
107+
108+
path := fmt.Sprintf(projectIPAccessListPath, groupID)
109+
110+
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createRequest)
111+
if err != nil {
112+
return nil, nil, err
113+
}
114+
115+
root := new(ProjectIPAccessLists)
116+
resp, err := s.Client.Do(ctx, req, root)
117+
if err != nil {
118+
return nil, resp, err
119+
}
120+
121+
if l := root.Links; l != nil {
122+
resp.Links = l
123+
}
124+
125+
return root, resp, err
126+
}
127+
128+
// Delete the access list entry specified to {ACCESS-LIST-ENTRY} from the project associated to {PROJECT-ID}.
129+
//
130+
// See more: https://docs.atlas.mongodb.com/reference/api/ip-access-list/delete-one-access-list-entry/
131+
func (s *ProjectIPAccessListServiceOp) Delete(ctx context.Context, groupID, entry string) (*Response, error) {
132+
if entry == "" {
133+
return nil, NewArgError("entry", "must be set")
134+
}
135+
136+
basePath := fmt.Sprintf(projectIPAccessListPath, groupID)
137+
escapedEntry := url.PathEscape(entry)
138+
path := fmt.Sprintf("%s/%s", basePath, escapedEntry)
139+
140+
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
resp, err := s.Client.Do(ctx, req, nil)
146+
147+
return resp, err
148+
}

0 commit comments

Comments
 (0)