Skip to content

Commit ac6bf94

Browse files
authored
INTMDB-364: [Terraform] Add support for serverless private endpoints (#314)
* Add serverless private endpoints * Add update payload * Remove extra parameter * Remove extra parameter * Edit struct for id * Add serverless update payload * Add ProviderName to serverless endpoint * Add test for serverless endpoints * Fix lint * Update links for API references * Split out serverless endpoints to separate service * Refactor Serverless endpoint naming convention
1 parent 3b4166c commit ac6bf94

File tree

3 files changed

+299
-0
lines changed

3 files changed

+299
-0
lines changed

mongodbatlas/mongodbatlas.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type Client struct {
116116
Auditing AuditingsService
117117
AlertConfigurations AlertConfigurationsService
118118
PrivateEndpoints PrivateEndpointsService
119+
ServerlessPrivateEndpoints ServerlessPrivateEndpointsService
119120
PrivateEndpointsDeprecated PrivateEndpointsServiceDeprecated
120121
X509AuthDBUsers X509AuthDBUsersService
121122
ContinuousSnapshots ContinuousSnapshotsService
@@ -263,6 +264,7 @@ func NewClient(httpClient *http.Client) *Client {
263264
c.Auditing = &AuditingsServiceOp{Client: c}
264265
c.AlertConfigurations = &AlertConfigurationsServiceOp{Client: c}
265266
c.PrivateEndpoints = &PrivateEndpointsServiceOp{Client: c}
267+
c.ServerlessPrivateEndpoints = &ServerlessPrivateEndpointsServiceOp{Client: c}
266268
c.PrivateEndpointsDeprecated = &PrivateEndpointsServiceOpDeprecated{Client: c}
267269
c.X509AuthDBUsers = &X509AuthDBUsersServiceOp{Client: c}
268270
c.ContinuousRestoreJobs = &ContinuousRestoreJobsServiceOp{Client: c}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright 2021 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mongodbatlas
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"net/http"
21+
"net/url"
22+
)
23+
24+
const (
25+
serverlessPrivateEndpointsPath = "api/atlas/v1.0/groups/%s/privateEndpoint/serverless/instance/%s/endpoint"
26+
)
27+
28+
// ServerlessPrivateEndpointsService is an interface for interfacing with the Private Endpoints
29+
// of the MongoDB Atlas API.
30+
//
31+
// See more: See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Serverless-Private-Endpoints
32+
type ServerlessPrivateEndpointsService interface {
33+
List(context.Context, string, string, *ListOptions) ([]ServerlessPrivateEndpointConnection, *Response, error)
34+
Create(context.Context, string, string, *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error)
35+
Get(context.Context, string, string, string) (*ServerlessPrivateEndpointConnection, *Response, error)
36+
Delete(context.Context, string, string, string) (*Response, error)
37+
Update(context.Context, string, string, string, *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error)
38+
}
39+
40+
// PrivateServerlessEndpointsServiceOp handles communication with the PrivateServerlessEndpoints related methods
41+
// of the MongoDB Atlas API.
42+
type ServerlessPrivateEndpointsServiceOp service
43+
44+
var _ ServerlessPrivateEndpointsService = &ServerlessPrivateEndpointsServiceOp{}
45+
46+
// PrivateEndpointServerlessConnection represents MongoDB Private Endpoint Connection.
47+
type ServerlessPrivateEndpointConnection struct {
48+
ID string `json:"_id,omitempty"` // Unique identifier of the Serverless PrivateLink Service.
49+
CloudProviderEndpointID string `json:"cloudProviderEndpointId,omitempty"`
50+
Comment string `json:"comment,omitempty"`
51+
EndpointServiceName string `json:"endpointServiceName,omitempty"` // Name of the PrivateLink endpoint service in AWS. Returns null while the endpoint service is being created.
52+
ErrorMessage string `json:"errorMessage,omitempty"` // Error message pertaining to the AWS Service Connect. Returns null if there are no errors.
53+
Status string `json:"status,omitempty"` // Status of the AWS Serverless PrivateLink connection: INITIATING, WAITING_FOR_USER, FAILED, DELETING, AVAILABLE.
54+
ProviderName string `json:"providerName,omitempty"` // Human-readable label that identifies the cloud provider. Values include AWS or AZURE.
55+
PrivateEndpointIPAddress string `json:"privateEndpointIpAddress,omitempty"` // IPv4 address of the private endpoint in your Azure VNet that someone added to this private endpoint service.
56+
PrivateLinkServiceResourceID string `json:"privateLinkServiceResourceId,omitempty"` // Root-relative path that identifies the Azure Private Link Service that MongoDB Cloud manages. MongoDB Cloud returns null while it creates the endpoint service.
57+
}
58+
59+
// List retrieve details for all private Serverless endpoint services in one Atlas project.
60+
//
61+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/returnAllPrivateEndpointsForOneServerlessInstance
62+
func (s *ServerlessPrivateEndpointsServiceOp) List(ctx context.Context, groupID, instanceName string, listOptions *ListOptions) ([]ServerlessPrivateEndpointConnection, *Response, error) {
63+
if groupID == "" {
64+
return nil, nil, NewArgError("groupID", "must be set")
65+
}
66+
if instanceName == "" {
67+
return nil, nil, NewArgError("instanceID", "must be set")
68+
}
69+
70+
path := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName) // Add query params from listOptions
71+
path, err := setListOptions(path, listOptions)
72+
if err != nil {
73+
return nil, nil, err
74+
}
75+
76+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
77+
if err != nil {
78+
return nil, nil, err
79+
}
80+
81+
root := new([]ServerlessPrivateEndpointConnection)
82+
resp, err := s.Client.Do(ctx, req, root)
83+
if err != nil {
84+
return nil, resp, err
85+
}
86+
87+
return *root, resp, nil
88+
}
89+
90+
// Delete one private serverless endpoint service in an Atlas project.
91+
//
92+
// See more https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/removeOnePrivateEndpointFromOneServerlessInstance
93+
func (s *ServerlessPrivateEndpointsServiceOp) Delete(ctx context.Context, groupID, instanceName, privateEndpointID string) (*Response, error) {
94+
if groupID == "" {
95+
return nil, NewArgError("groupID", "must be set")
96+
}
97+
if privateEndpointID == "" {
98+
return nil, NewArgError("PrivateEndpointID", "must be set")
99+
}
100+
if instanceName == "" {
101+
return nil, NewArgError("instanceName", "must be set")
102+
}
103+
104+
basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName)
105+
path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID))
106+
107+
req, err := s.Client.NewRequest(ctx, http.MethodDelete, path, nil)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
return s.Client.Do(ctx, req, nil)
113+
}
114+
115+
// Create Adds one serverless private endpoint in an Atlas project.
116+
//
117+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/createOnePrivateEndpointForOneServerlessInstance
118+
func (s *ServerlessPrivateEndpointsServiceOp) Create(ctx context.Context, groupID, instanceName string, createRequest *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) {
119+
if groupID == "" {
120+
return nil, nil, NewArgError("groupID", "must be set")
121+
}
122+
123+
if instanceName == "" {
124+
return nil, nil, NewArgError("instanceName", "must be set")
125+
}
126+
if createRequest == nil {
127+
return nil, nil, NewArgError("createRequest", "cannot be nil")
128+
}
129+
130+
path := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName)
131+
132+
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, createRequest)
133+
if err != nil {
134+
return nil, nil, err
135+
}
136+
137+
root := new(ServerlessPrivateEndpointConnection)
138+
resp, err := s.Client.Do(ctx, req, root)
139+
if err != nil {
140+
return nil, resp, err
141+
}
142+
143+
return root, resp, err
144+
}
145+
146+
// Get retrieve details for one private serverless endpoint in an Atlas project.
147+
//
148+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/returnOnePrivateEndpointForOneServerlessInstance
149+
func (s *ServerlessPrivateEndpointsServiceOp) Get(ctx context.Context, groupID, instanceName, privateEndpointID string) (*ServerlessPrivateEndpointConnection, *Response, error) {
150+
if groupID == "" {
151+
return nil, nil, NewArgError("groupID", "must be set")
152+
}
153+
154+
if instanceName == "" {
155+
return nil, nil, NewArgError("instanceName", "must be set")
156+
}
157+
if privateEndpointID == "" {
158+
return nil, nil, NewArgError("privateEndpointID", "must be set")
159+
}
160+
161+
basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName)
162+
path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID))
163+
164+
req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
165+
if err != nil {
166+
return nil, nil, err
167+
}
168+
169+
root := new(ServerlessPrivateEndpointConnection)
170+
resp, err := s.Client.Do(ctx, req, root)
171+
if err != nil {
172+
return nil, resp, err
173+
}
174+
175+
return root, resp, err
176+
}
177+
178+
// Update updates the private serverless endpoint setting for one Atlas project.
179+
//
180+
// See more: https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#operation/updateOnePrivateEndpointForOneServerlessInstance
181+
func (s *ServerlessPrivateEndpointsServiceOp) Update(ctx context.Context, groupID, instanceName, privateEndpointID string, updateRequest *ServerlessPrivateEndpointConnection) (*ServerlessPrivateEndpointConnection, *Response, error) {
182+
if groupID == "" {
183+
return nil, nil, NewArgError("groupID", "must be set")
184+
}
185+
if instanceName == "" {
186+
return nil, nil, NewArgError("instanceName", "must be set")
187+
}
188+
if privateEndpointID == "" {
189+
return nil, nil, NewArgError("privateEndpointID", "must be set")
190+
}
191+
192+
basePath := fmt.Sprintf(serverlessPrivateEndpointsPath, groupID, instanceName)
193+
path := fmt.Sprintf("%s/%s", basePath, url.PathEscape(privateEndpointID))
194+
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
195+
if err != nil {
196+
return nil, nil, err
197+
}
198+
199+
root := new(ServerlessPrivateEndpointConnection)
200+
resp, err := s.Client.Do(ctx, req, root)
201+
if err != nil {
202+
return nil, resp, err
203+
}
204+
205+
return root, resp, err
206+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2021 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mongodbatlas
16+
17+
import (
18+
"fmt"
19+
"net/http"
20+
"reflect"
21+
"testing"
22+
)
23+
24+
func TestPrivateEndpoints_GetOnePrivateServerlessEndpoint(t *testing.T) {
25+
client, mux, teardown := setup()
26+
defer teardown()
27+
28+
groupID := "1"
29+
instanceName := "serverless"
30+
endpointID := "3658569708087079"
31+
32+
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/privateEndpoint/serverless/instance/%s/endpoint/%s", groupID, instanceName, endpointID), func(w http.ResponseWriter, r *http.Request) {
33+
testMethod(t, r, http.MethodGet)
34+
fmt.Fprint(w, `{
35+
"ID":"", "CloudProviderEndpointID":"vpce-12356", "Comment":"Test Serverless", "EndpointServiceName":"", "Status":"AVAILABLE", "ProviderName":"AWS"
36+
}`)
37+
})
38+
39+
interfaceEndpoint, _, err := client.ServerlessPrivateEndpoints.Get(ctx, groupID, instanceName, endpointID)
40+
if err != nil {
41+
t.Errorf("PrivateEndpoints.Get returned error: %v", err)
42+
}
43+
44+
expected := &ServerlessPrivateEndpointConnection{
45+
Comment: "Test Serverless",
46+
ProviderName: "AWS",
47+
Status: "AVAILABLE",
48+
CloudProviderEndpointID: "vpce-12356",
49+
}
50+
51+
if !reflect.DeepEqual(interfaceEndpoint, expected) {
52+
t.Errorf("PrivateEndpoints.Get \n got=%#v\nwant=%#v", interfaceEndpoint, expected)
53+
}
54+
}
55+
56+
func TestPrivateEndpoints_UpdateOnePrivateServerlessEndpoint(t *testing.T) {
57+
client, mux, teardown := setup()
58+
defer teardown()
59+
60+
groupID := "1"
61+
instanceName := "serverless"
62+
endpointID := "3658569708087079"
63+
64+
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/privateEndpoint/serverless/instance/%s/endpoint/%s", groupID, instanceName, endpointID), func(w http.ResponseWriter, r *http.Request) {
65+
testMethod(t, r, http.MethodPatch)
66+
fmt.Fprint(w, `{
67+
"ID":"", "CloudProviderEndpointID":"vpce1234567", "Comment":"Test Serverless", "EndpointServiceName":"", "Status":"AVAILABLE", "ProviderName":"AWS"
68+
}`)
69+
})
70+
71+
update := &ServerlessPrivateEndpointConnection{
72+
CloudProviderEndpointID: "vpce1234567",
73+
ProviderName: "AWS",
74+
}
75+
76+
interfaceEndpoint, _, err := client.ServerlessPrivateEndpoints.Update(ctx, groupID, instanceName, endpointID, update)
77+
if err != nil {
78+
t.Errorf("PrivateEndpoints.Update returned error: %v", err)
79+
}
80+
81+
expected := &ServerlessPrivateEndpointConnection{
82+
Comment: "Test Serverless",
83+
ProviderName: "AWS",
84+
Status: "AVAILABLE",
85+
CloudProviderEndpointID: "vpce1234567",
86+
}
87+
88+
if !reflect.DeepEqual(interfaceEndpoint, expected) {
89+
t.Errorf("PrivateEndpoints.Update\n got=%#v\nwant=%#v", interfaceEndpoint, expected)
90+
}
91+
}

0 commit comments

Comments
 (0)