Skip to content

Commit b1fb056

Browse files
authored
CLOUDP-57324: Keep polling the automation status and show when changes are done (#153)
1 parent 79cbe61 commit b1fb056

7 files changed

+255
-0
lines changed

internal/cli/atlas_clusters.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func AtlasClustersBuilder() *cobra.Command {
3030
cmd.AddCommand(AtlasClustersListBuilder())
3131
cmd.AddCommand(AtlasClustersDescribeBuilder())
3232
cmd.AddCommand(AtlasClustersCreateBuilder())
33+
cmd.AddCommand(AtlasClustersWatchBuilder())
3334
cmd.AddCommand(AtlasClustersUpdateBuilder())
3435
cmd.AddCommand(AtlasClustersPauseBuilder())
3536
cmd.AddCommand(AtlasClustersStartBuilder())

internal/cli/atlas_clusters_watch.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
"fmt"
19+
"time"
20+
21+
"github.com/mongodb/mongocli/internal/description"
22+
"github.com/mongodb/mongocli/internal/flags"
23+
"github.com/mongodb/mongocli/internal/store"
24+
"github.com/mongodb/mongocli/internal/usage"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
type atlasClustersWatchOpts struct {
29+
globalOpts
30+
name string
31+
store store.ClusterDescriber
32+
}
33+
34+
func (opts *atlasClustersWatchOpts) initStore() error {
35+
var err error
36+
opts.store, err = store.New()
37+
return err
38+
}
39+
40+
func (opts *atlasClustersWatchOpts) Run() error {
41+
for {
42+
result, err := opts.store.Cluster(opts.ProjectID(), opts.name)
43+
if err != nil {
44+
return err
45+
}
46+
if result.StateName == "IDLE" {
47+
fmt.Printf("\nCluster available at: %s\n", result.MongoURIWithOptions)
48+
break
49+
}
50+
fmt.Print(".")
51+
time.Sleep(4 * time.Second)
52+
}
53+
54+
return nil
55+
}
56+
57+
// mongocli atlas cluster(s) watch [name] [--projectId projectId]
58+
func AtlasClustersWatchBuilder() *cobra.Command {
59+
opts := &atlasClustersWatchOpts{}
60+
cmd := &cobra.Command{
61+
Use: "watch [name]",
62+
Short: description.WatchCluster,
63+
Args: cobra.ExactArgs(1),
64+
PreRunE: func(cmd *cobra.Command, args []string) error {
65+
return opts.PreRunE(opts.initStore)
66+
},
67+
RunE: func(cmd *cobra.Command, args []string) error {
68+
opts.name = args[0]
69+
return opts.Run()
70+
},
71+
}
72+
73+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
74+
75+
return cmd
76+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 TestAtlasClustersWatch_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockClusterDescriber(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := &mongodbatlas.Cluster{StateName: "IDLE"}
32+
33+
describeOpts := &atlasClustersWatchOpts{
34+
name: "test",
35+
store: mockStore,
36+
}
37+
38+
mockStore.
39+
EXPECT().
40+
Cluster(describeOpts.projectID, describeOpts.name).
41+
Return(expected, nil).
42+
Times(1)
43+
44+
err := describeOpts.Run()
45+
if err != nil {
46+
t.Fatalf("Run() unexpected error: %v", err)
47+
}
48+
}

internal/cli/ops_manager_automation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func OpsManagerAutomationBuilder() *cobra.Command {
2828
cmd.AddCommand(OpsManagerAutomationStatusBuilder())
2929
cmd.AddCommand(OpsManagerAutomationDescribeBuilder())
3030
cmd.AddCommand(OpsManagerAutomationUpdateBuilder())
31+
cmd.AddCommand(OpsManagerAutomationWatchBuilder())
3132

3233
return cmd
3334
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
"fmt"
19+
"time"
20+
21+
"github.com/mongodb/mongocli/internal/description"
22+
"github.com/mongodb/mongocli/internal/flags"
23+
"github.com/mongodb/mongocli/internal/store"
24+
"github.com/mongodb/mongocli/internal/usage"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
type opsManagerAutomationWatchOpts struct {
29+
globalOpts
30+
store store.AutomationStatusGetter
31+
}
32+
33+
func (opts *opsManagerAutomationWatchOpts) initStore() error {
34+
var err error
35+
opts.store, err = store.New()
36+
return err
37+
}
38+
39+
func (opts *opsManagerAutomationWatchOpts) Run() error {
40+
41+
for {
42+
result, err := opts.store.GetAutomationStatus(opts.ProjectID())
43+
if err != nil {
44+
return err
45+
}
46+
reachedGoal := true
47+
for _, p := range result.Processes {
48+
if p.LastGoalVersionAchieved != result.GoalVersion {
49+
reachedGoal = false
50+
break
51+
}
52+
}
53+
if reachedGoal {
54+
break
55+
}
56+
fmt.Print(".")
57+
time.Sleep(4 * time.Second)
58+
}
59+
fmt.Printf("\nChanges deployed successfully\n")
60+
return nil
61+
}
62+
63+
// mongocli ops-manager automation watch [--projectId projectId]
64+
func OpsManagerAutomationWatchBuilder() *cobra.Command {
65+
opts := &opsManagerAutomationWatchOpts{}
66+
cmd := &cobra.Command{
67+
Use: "watch",
68+
Short: description.WatchAutomationStatus,
69+
Args: cobra.NoArgs,
70+
PreRunE: func(cmd *cobra.Command, args []string) error {
71+
return opts.PreRunE(opts.initStore)
72+
},
73+
RunE: func(cmd *cobra.Command, args []string) error {
74+
return opts.Run()
75+
},
76+
}
77+
78+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
79+
80+
return cmd
81+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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/mongocli/internal/fixtures"
22+
"github.com/mongodb/mongocli/internal/mocks"
23+
)
24+
25+
func TestOpsManagerAutomationWatch_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockAutomationStatusGetter(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := fixtures.AutomationStatus()
32+
33+
opts := &opsManagerAutomationWatchOpts{
34+
store: mockStore,
35+
}
36+
37+
mockStore.
38+
EXPECT().
39+
GetAutomationStatus(opts.projectID).
40+
Return(expected, nil).
41+
Times(1)
42+
43+
err := opts.Run()
44+
if err != nil {
45+
t.Fatalf("Run() unexpected error: %v", err)
46+
}
47+
}

internal/description/description.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const (
9292
ListGlobalAlerts = "List global alerts."
9393
Automation = "Manage Ops Manager automation config."
9494
ShowAutomationStatus = "Show the current status of the automation config."
95+
WatchAutomationStatus = "Watch for automation changes."
9596
Global = "Manage Ops Manager global properties."
9697
Owner = "Manage Ops Manager owners."
9798
CreateOwner = "Create the first user for Ops Manager."

0 commit comments

Comments
 (0)