Skip to content

Commit 819fb54

Browse files
authored
CLOUDP-132512: [Atlas CLI] Add support for updating a serverless cluster (#310)
1 parent c76ae52 commit 819fb54

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

mongodbatlas/clusters.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type Cluster struct {
165165
ReplicationSpecs []ReplicationSpec `json:"replicationSpecs,omitempty"`
166166
SrvAddress string `json:"srvAddress,omitempty"`
167167
StateName string `json:"stateName,omitempty"`
168+
ServerlessBackupOptions *ServerlessBackupOptions `json:"serverlessBackupOptions,omitempty"`
168169
ConnectionStrings *ConnectionStrings `json:"connectionStrings,omitempty"`
169170
Links []*Link `json:"links,omitempty"`
170171
VersionReleaseSystem string `json:"versionReleaseSystem,omitempty"`

mongodbatlas/serverless_instances.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type ServerlessInstancesService interface {
2929
List(context.Context, string, *ListOptions) (*ClustersResponse, *Response, error)
3030
Get(context.Context, string, string) (*Cluster, *Response, error)
3131
Create(context.Context, string, *ServerlessCreateRequestParams) (*Cluster, *Response, error)
32+
Update(context.Context, string, string, *ServerlessUpdateRequestParams) (*Cluster, *Response, error)
3233
Delete(context.Context, string, string) (*Response, error)
3334
}
3435

@@ -46,8 +47,13 @@ type ClustersResponse struct {
4647

4748
// ServerlessCreateRequestParams represents the Request Body Parameters of ServerlessInstancesService.Create.
4849
type ServerlessCreateRequestParams struct {
49-
Name string `json:"name,omitempty"`
50-
ProviderSettings *ServerlessProviderSettings `json:"providerSettings,omitempty"`
50+
Name string `json:"name,omitempty"`
51+
ProviderSettings *ServerlessProviderSettings `json:"providerSettings,omitempty"`
52+
ServerlessBackupOptions *ServerlessBackupOptions `json:"serverlessBackupOptions,omitempty"`
53+
}
54+
55+
type ServerlessUpdateRequestParams struct {
56+
ServerlessBackupOptions *ServerlessBackupOptions `json:"serverlessBackupOptions"`
5157
}
5258

5359
// ServerlessProviderSettings represents the Provider Settings of serverless instances.
@@ -57,6 +63,11 @@ type ServerlessProviderSettings struct {
5763
RegionName string `json:"regionName,omitempty"`
5864
}
5965

66+
// ServerlessBackupOptions Serverless Continuous Backup.
67+
type ServerlessBackupOptions struct {
68+
ServerlessContinuousBackupEnabled *bool `json:"serverlessContinuousBackupEnabled,omitempty"`
69+
}
70+
6071
// List gets all serverless instances in the specified project.
6172
//
6273
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/return-all-serverless-instances/
@@ -138,6 +149,34 @@ func (s *ServerlessInstancesServiceOp) Create(ctx context.Context, projectID str
138149
return root, resp, err
139150
}
140151

152+
// Update one serverless instance in the specified project..
153+
//
154+
// See more: https://www.mongodb.com/docs/atlas/reference/api/serverless/update-one-serverless-instance/
155+
func (s *ServerlessInstancesServiceOp) Update(ctx context.Context, projectID, instanceName string, bodyParams *ServerlessUpdateRequestParams) (*Cluster, *Response, error) {
156+
if projectID == "" {
157+
return nil, nil, NewArgError("projectID", "must be set")
158+
}
159+
if instanceName == "" {
160+
return nil, nil, NewArgError("instanceName", "must be set")
161+
}
162+
163+
basePath := fmt.Sprintf(serverlessInstancesPath, projectID)
164+
path := fmt.Sprintf("%s/%s", basePath, instanceName)
165+
166+
req, err := s.Client.NewRequest(ctx, http.MethodPatch, path, bodyParams)
167+
if err != nil {
168+
return nil, nil, err
169+
}
170+
171+
root := new(Cluster)
172+
resp, err := s.Client.Do(ctx, req, root)
173+
if err != nil {
174+
return nil, resp, err
175+
}
176+
177+
return root, resp, err
178+
}
179+
141180
// Delete deletes one serverless instance in the specified project.
142181
//
143182
// See more: https://docs.atlas.mongodb.com/reference/api/serverless/remove-one-serverless-instance/

mongodbatlas/serverless_instances_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/go-test/deep"
23+
"github.com/openlyinc/pointy"
2324
)
2425

2526
const (
@@ -252,6 +253,83 @@ func TestServerlessInstances_Create(t *testing.T) {
252253
}
253254
}
254255

256+
func TestServerlessInstances_Update(t *testing.T) {
257+
client, mux, teardown := setup()
258+
defer teardown()
259+
260+
mux.HandleFunc(fmt.Sprintf("/"+serverlessInstancesPath+"/%s", projectID, "sample"), func(w http.ResponseWriter, r *http.Request) {
261+
testMethod(t, r, http.MethodPatch)
262+
fmt.Fprint(w, `{
263+
"connectionStrings" : {
264+
"standardSrv" : "mongodb+srv://instanceName1.example.com"
265+
},
266+
"createDate" : "2021-06-25T21:31:10Z",
267+
"groupId" : "PROJECT-ID",
268+
"id" : "1",
269+
"links" : [ {
270+
"href" : "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}",
271+
"rel" : "self"
272+
}, {
273+
"href" : "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}/backup/restoreJobs",
274+
"rel" : "http://cloud.mongodb.com/restoreJobs"
275+
}, {
276+
"href" : "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}/backup/snapshots",
277+
"rel" : "http://cloud.mongodb.com/snapshots"
278+
}],
279+
"mongoDBVersion" : "5.0.0",
280+
"name" : "sample",
281+
"providerSettings" : {
282+
"providerName" : "SERVERLESS",
283+
"backingProviderName" : "AWS",
284+
"regionName" : "US_EAST_1"
285+
},
286+
"serverlessBackupOptions" : {
287+
"serverlessContinuousBackupEnabled" : true
288+
},
289+
"stateName" : "IDLE"
290+
}`)
291+
})
292+
293+
bodyParam := &ServerlessUpdateRequestParams{
294+
ServerlessBackupOptions: &ServerlessBackupOptions{ServerlessContinuousBackupEnabled: pointy.Bool(true)},
295+
}
296+
297+
serverlessInstance, _, err := client.ServerlessInstances.Update(ctx, projectID, "sample", bodyParam)
298+
if err != nil {
299+
t.Fatalf("ServerlessInstances.Get returned error: %v", err)
300+
}
301+
302+
expected := &Cluster{
303+
ID: id,
304+
GroupID: projectID,
305+
MongoDBVersion: "5.0.0",
306+
Name: "sample",
307+
ProviderSettings: &ProviderSettings{RegionName: "US_EAST_1", BackingProviderName: "AWS", ProviderName: "SERVERLESS"},
308+
StateName: "IDLE",
309+
ConnectionStrings: &ConnectionStrings{StandardSrv: "mongodb+srv://instanceName1.example.com"},
310+
CreateDate: "2021-06-25T21:31:10Z",
311+
ServerlessBackupOptions: &ServerlessBackupOptions{ServerlessContinuousBackupEnabled: pointy.Bool(true)},
312+
Links: []*Link{
313+
{
314+
Rel: "self",
315+
Href: "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}",
316+
},
317+
{
318+
Rel: "http://cloud.mongodb.com/restoreJobs",
319+
Href: "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}/backup/restoreJobs",
320+
},
321+
{
322+
Rel: "http://cloud.mongodb.com/snapshots",
323+
Href: "http://cloud.mongodb.com/api/atlas/v1.0/groups/{groupId}/serverless/{instanceName1}/backup/snapshots",
324+
},
325+
},
326+
}
327+
328+
if diff := deep.Equal(serverlessInstance, expected); diff != nil {
329+
t.Error(diff)
330+
}
331+
}
332+
255333
func TestServerlessInstances_Delete(t *testing.T) {
256334
client, mux, teardown := setup()
257335
defer teardown()

0 commit comments

Comments
 (0)