Skip to content

Commit 0c0dfdd

Browse files
authored
CLOUDP-61529: Pause and Start an atlas cluster (#132)
1 parent 8660b5e commit 0c0dfdd

10 files changed

+276
-34
lines changed

internal/cli/atlas_clusters.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ func AtlasClustersBuilder() *cobra.Command {
2727
Short: description.Clusters,
2828
Long: description.ClustersLong,
2929
}
30-
cmd.AddCommand(AtlasClustersCreateBuilder())
3130
cmd.AddCommand(AtlasClustersListBuilder())
3231
cmd.AddCommand(AtlasClustersDescribeBuilder())
33-
cmd.AddCommand(AtlasClustersDeleteBuilder())
32+
cmd.AddCommand(AtlasClustersCreateBuilder())
3433
cmd.AddCommand(AtlasClustersUpdateBuilder())
34+
cmd.AddCommand(AtlasClustersPauseBuilder())
35+
cmd.AddCommand(AtlasClustersStartBuilder())
36+
cmd.AddCommand(AtlasClustersDeleteBuilder())
3537
cmd.AddCommand(AtlasClustersIndexesBuilder())
3638

3739
return cmd

internal/cli/atlas_clusters_create_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import (
2626
func TestAtlasClustersCreate_Run(t *testing.T) {
2727
ctrl := gomock.NewController(t)
2828
mockStore := mocks.NewMockClusterCreator(ctrl)
29+
defer ctrl.Finish()
2930

3031
expected := &mongodbatlas.Cluster{}
3132

32-
defer ctrl.Finish()
33-
3433
t.Run("flags run", func(t *testing.T) {
3534
createOpts := &atlasClustersCreateOpts{
3635
name: "ProjectBar",

internal/cli/atlas_clusters_pause.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
19+
"github.com/mongodb/mongocli/internal/description"
20+
"github.com/mongodb/mongocli/internal/flags"
21+
"github.com/mongodb/mongocli/internal/json"
22+
"github.com/mongodb/mongocli/internal/store"
23+
"github.com/mongodb/mongocli/internal/usage"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type atlasClustersPauseOpts struct {
28+
globalOpts
29+
name string
30+
store store.ClusterUpdater
31+
}
32+
33+
func (opts *atlasClustersPauseOpts) initStore() error {
34+
var err error
35+
opts.store, err = store.New()
36+
return err
37+
}
38+
39+
func (opts *atlasClustersPauseOpts) Run() error {
40+
paused := true
41+
cluster := &atlas.Cluster{
42+
Paused: &paused,
43+
}
44+
result, err := opts.store.UpdateCluster(opts.ProjectID(), opts.name, cluster)
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
return json.PrettyPrint(result)
51+
}
52+
53+
// mongocli atlas cluster(s) pause [name] [--projectId projectId]
54+
func AtlasClustersPauseBuilder() *cobra.Command {
55+
opts := &atlasClustersPauseOpts{}
56+
cmd := &cobra.Command{
57+
Use: "pause [name]",
58+
Short: description.PauseCluster,
59+
Args: cobra.ExactArgs(1),
60+
PreRunE: func(cmd *cobra.Command, args []string) error {
61+
return opts.PreRunE(opts.initStore)
62+
},
63+
RunE: func(cmd *cobra.Command, args []string) error {
64+
opts.name = args[0]
65+
return opts.Run()
66+
},
67+
}
68+
69+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
70+
71+
return cmd
72+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
22+
"github.com/mongodb/mongocli/internal/mocks"
23+
)
24+
25+
func TestAtlasClustersPause_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockClusterUpdater(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
paused := true
32+
expected := &mongodbatlas.Cluster{
33+
Paused: &paused,
34+
}
35+
36+
updateOpts := &atlasClustersPauseOpts{
37+
name: "ProjectBar",
38+
store: mockStore,
39+
}
40+
41+
mockStore.
42+
EXPECT().
43+
UpdateCluster(updateOpts.ProjectID(), updateOpts.name, expected).
44+
Return(expected, nil).
45+
Times(1)
46+
47+
err := updateOpts.Run()
48+
if err != nil {
49+
t.Fatalf("Run() unexpected error: %v", err)
50+
}
51+
}

internal/cli/atlas_clusters_start.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
19+
"github.com/mongodb/mongocli/internal/description"
20+
"github.com/mongodb/mongocli/internal/flags"
21+
"github.com/mongodb/mongocli/internal/json"
22+
"github.com/mongodb/mongocli/internal/store"
23+
"github.com/mongodb/mongocli/internal/usage"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type atlasClustersStartOpts struct {
28+
globalOpts
29+
name string
30+
store store.ClusterUpdater
31+
}
32+
33+
func (opts *atlasClustersStartOpts) initStore() error {
34+
var err error
35+
opts.store, err = store.New()
36+
return err
37+
}
38+
39+
func (opts *atlasClustersStartOpts) Run() error {
40+
paused := false
41+
cluster := &atlas.Cluster{
42+
Paused: &paused,
43+
}
44+
result, err := opts.store.UpdateCluster(opts.ProjectID(), opts.name, cluster)
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
return json.PrettyPrint(result)
51+
}
52+
53+
// mongocli atlas cluster(s) start [name] [--projectId projectId]
54+
func AtlasClustersStartBuilder() *cobra.Command {
55+
opts := &atlasClustersStartOpts{}
56+
cmd := &cobra.Command{
57+
Use: "start [name]",
58+
Short: description.StartCluster,
59+
Args: cobra.ExactArgs(1),
60+
PreRunE: func(cmd *cobra.Command, args []string) error {
61+
return opts.PreRunE(opts.initStore)
62+
},
63+
RunE: func(cmd *cobra.Command, args []string) error {
64+
opts.name = args[0]
65+
return opts.Run()
66+
},
67+
}
68+
69+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
70+
71+
return cmd
72+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 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 cli
16+
17+
import (
18+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
22+
"github.com/mongodb/mongocli/internal/mocks"
23+
)
24+
25+
func TestAtlasClustersStart_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockClusterUpdater(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
paused := false
32+
expected := &mongodbatlas.Cluster{
33+
Paused: &paused,
34+
}
35+
36+
updateOpts := &atlasClustersStartOpts{
37+
name: "ProjectBar",
38+
store: mockStore,
39+
}
40+
41+
mockStore.
42+
EXPECT().
43+
UpdateCluster(updateOpts.ProjectID(), updateOpts.name, expected).
44+
Return(expected, nil).
45+
Times(1)
46+
47+
err := updateOpts.Run()
48+
if err != nil {
49+
t.Fatalf("Run() unexpected error: %v", err)
50+
}
51+
}

internal/cli/atlas_clusters_update.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ func (opts *atlasClustersUpdateOpts) Run() error {
5050
}
5151
if opts.filename == "" {
5252
opts.patchOpts(cluster)
53-
} else {
54-
cluster.GroupID = opts.ProjectID()
5553
}
5654

57-
result, err := opts.store.UpdateCluster(cluster)
55+
result, err := opts.store.UpdateCluster(opts.ProjectID(), opts.name, cluster)
5856

5957
if err != nil {
6058
return err
@@ -69,8 +67,8 @@ func (opts *atlasClustersUpdateOpts) cluster() (*atlas.Cluster, error) {
6967
if opts.filename != "" {
7068
cluster = new(atlas.Cluster)
7169
err = file.Load(opts.fs, opts.filename, cluster)
72-
if opts.name != "" {
73-
cluster.Name = opts.name
70+
if opts.name == "" {
71+
opts.name = cluster.Name
7472
}
7573
} else {
7674
cluster, err = opts.store.Cluster(opts.projectID, opts.name)
@@ -112,9 +110,6 @@ func AtlasClustersUpdateBuilder() *cobra.Command {
112110
Example: ` mongocli atlas cluster update myCluster --projectId=1 --instanceSize M2 --mdbVersion 4.2 --diskSizeGB 2`,
113111
Args: cobra.MaximumNArgs(1),
114112
PreRunE: func(cmd *cobra.Command, args []string) error {
115-
if opts.filename == "" && len(args) == 0 {
116-
return errMissingClusterName
117-
}
118113
if len(args) != 0 {
119114
opts.name = args[0]
120115
}

internal/cli/atlas_clusters_update_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAtlasClustersUpdate_Run(t *testing.T) {
3434

3535
t.Run("flags run", func(t *testing.T) {
3636

37-
createOpts := &atlasClustersUpdateOpts{
37+
updateOpts := &atlasClustersUpdateOpts{
3838
name: "ProjectBar",
3939
instanceSize: atlasM2,
4040
diskSizeGB: 10,
@@ -44,18 +44,18 @@ func TestAtlasClustersUpdate_Run(t *testing.T) {
4444

4545
mockStore.
4646
EXPECT().
47-
Cluster(createOpts.projectID, createOpts.name).
47+
Cluster(updateOpts.projectID, updateOpts.name).
4848
Return(expected, nil).
4949
Times(1)
5050

51-
createOpts.patchOpts(expected)
51+
updateOpts.patchOpts(expected)
5252

5353
mockStore.
5454
EXPECT().
55-
UpdateCluster(expected).Return(expected, nil).
55+
UpdateCluster(updateOpts.ProjectID(), updateOpts.name, expected).Return(expected, nil).
5656
Times(1)
5757

58-
err := createOpts.Run()
58+
err := updateOpts.Run()
5959
if err != nil {
6060
t.Fatalf("Run() unexpected error: %v", err)
6161
}
@@ -64,8 +64,7 @@ func TestAtlasClustersUpdate_Run(t *testing.T) {
6464
t.Run("file run", func(t *testing.T) {
6565
appFS := afero.NewMemMapFs()
6666
// create test file
67-
fileYML := `
68-
{
67+
fileYML := `{
6968
"name": "ProjectBar",
7069
"diskSizeGB": 10,
7170
"numShards": 1,
@@ -94,19 +93,20 @@ func TestAtlasClustersUpdate_Run(t *testing.T) {
9493
fileName := "test.json"
9594
_ = afero.WriteFile(appFS, fileName, []byte(fileYML), 0600)
9695

97-
createOpts := &atlasClustersUpdateOpts{
96+
updateOpts := &atlasClustersUpdateOpts{
9897
filename: fileName,
9998
fs: appFS,
10099
store: mockStore,
101100
}
102101

103-
cluster, _ := createOpts.cluster()
102+
cluster, _ := updateOpts.cluster()
104103
mockStore.
105104
EXPECT().
106-
UpdateCluster(cluster).Return(expected, nil).
105+
UpdateCluster(updateOpts.ProjectID(), "ProjectBar", cluster).
106+
Return(expected, nil).
107107
Times(1)
108108

109-
err := createOpts.Run()
109+
err := updateOpts.Run()
110110
if err != nil {
111111
t.Fatalf("Run() unexpected error: %v", err)
112112
}

0 commit comments

Comments
 (0)