Skip to content

Commit 943ba33

Browse files
authored
CLOUDP-275224 Only show containers that can be stopped when running atlas deployments stop (#3282)
1 parent f7f9937 commit 943ba33

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

internal/cli/deployments/options/deployment_opts_pre_run.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ import (
1818
"context"
1919
"errors"
2020
"runtime"
21+
"slices"
2122

2223
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
2324
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
2425
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
2526
atlasv2 "go.mongodb.org/atlas-sdk/v20240805001/admin"
2627
)
2728

28-
func (opts *DeploymentOpts) SelectDeployments(ctx context.Context, projectID string) (Deployment, error) {
29+
func (opts *DeploymentOpts) listDeployments(ctx context.Context, projectID string) ([]Deployment, error) {
2930
var atlasDeployments, localDeployments []Deployment
3031
var atlasErr, localErr error
3132

3233
if opts.IsAtlasDeploymentType() || opts.NoDeploymentTypeSet() {
3334
if atlasDeployments, atlasErr = opts.AtlasDeployments(projectID); atlasErr != nil {
3435
if opts.IsAtlasDeploymentType() {
35-
return Deployment{}, atlasErr
36+
return nil, atlasErr
3637
}
3738
if !isUnauthenticatedErr(atlasErr) {
3839
_, _ = log.Warningf("Warning: failed to retrieve Atlas deployments because %q\n", atlasErr.Error())
@@ -43,29 +44,37 @@ func (opts *DeploymentOpts) SelectDeployments(ctx context.Context, projectID str
4344
if opts.IsLocalDeploymentType() || opts.NoDeploymentTypeSet() {
4445
if localErr = opts.LocalDeploymentPreRun(ctx); localErr != nil {
4546
if opts.IsLocalDeploymentType() {
46-
return Deployment{}, localErr
47+
return nil, localErr
4748
}
4849
_, _ = log.Debugf("Warning: failed to retrieve Local deployments because %q\n", localErr.Error())
4950
}
5051

5152
localDeployments, localErr = opts.GetLocalDeployments(ctx)
5253
if localErr != nil {
5354
if opts.IsLocalDeploymentType() {
54-
return Deployment{}, localErr
55+
return nil, localErr
5556
}
5657
_, _ = log.Warningf("Warning: failed to retrieve Local deployments because %q\n", localErr.Error())
5758
}
5859
}
5960

6061
if atlasErr != nil && localErr != nil {
61-
return Deployment{}, errors.New("failed to retrieve atlas and local deployments")
62+
return nil, errors.New("failed to retrieve atlas and local deployments")
6263
}
6364

64-
if opts.DeploymentName == "" {
65-
return opts.Select(append(localDeployments, atlasDeployments...))
65+
return append(localDeployments, atlasDeployments...), nil
66+
}
67+
68+
func (opts *DeploymentOpts) SelectDeployments(ctx context.Context, projectID string, states ...string) (Deployment, error) {
69+
deployments, err := opts.listDeployments(ctx, projectID)
70+
if err != nil {
71+
return Deployment{}, errors.New("failed to retrieve atlas and local deployments")
6672
}
6773

68-
return opts.findDeploymentByName(localDeployments, atlasDeployments)
74+
deployments = opts.filterDeploymentByName(deployments...)
75+
deployments = opts.filterDeploymentByState(deployments, states...)
76+
77+
return opts.Select(deployments)
6978
}
7079

7180
func isUnauthenticatedErr(err error) bool {
@@ -77,21 +86,36 @@ func isUnauthenticatedErr(err error) bool {
7786
return ok && target.GetReason() == "Unauthorized"
7887
}
7988

80-
func (opts *DeploymentOpts) findDeploymentByName(localDeployments []Deployment, atlasDeployments []Deployment) (Deployment, error) {
81-
deployments := make([]Deployment, 0)
82-
for _, d := range localDeployments {
89+
func (opts *DeploymentOpts) filterDeploymentByName(deployments ...Deployment) []Deployment {
90+
if opts.DeploymentName == "" {
91+
return deployments
92+
}
93+
94+
filteredDeployments := []Deployment{}
95+
96+
for _, d := range deployments {
8397
if d.Name == opts.DeploymentName {
84-
deployments = append(deployments, d)
98+
filteredDeployments = append(filteredDeployments, d)
8599
}
86100
}
87101

88-
for _, d := range atlasDeployments {
89-
if d.Name == opts.DeploymentName {
90-
deployments = append(deployments, d)
102+
return filteredDeployments
103+
}
104+
105+
func (*DeploymentOpts) filterDeploymentByState(deployments []Deployment, states ...string) []Deployment {
106+
if len(states) == 0 {
107+
return deployments
108+
}
109+
110+
filteredDeployments := []Deployment{}
111+
112+
for _, d := range deployments {
113+
if slices.Contains(states, d.StateName) {
114+
filteredDeployments = append(filteredDeployments, d)
91115
}
92116
}
93117

94-
return opts.Select(deployments)
118+
return filteredDeployments
95119
}
96120

97121
func (opts *DeploymentOpts) AtlasDeployments(projectID string) ([]Deployment, error) {

internal/cli/deployments/pause.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (opts *PauseOpts) initStore(ctx context.Context) func() error {
5757
}
5858

5959
func (opts *PauseOpts) Run(ctx context.Context) error {
60-
deployment, err := opts.SelectDeployments(ctx, opts.ConfigProjectID())
60+
deployment, err := opts.SelectDeployments(ctx, opts.ConfigProjectID(), options.IdleState)
6161
if err != nil {
6262
return err
6363
}

internal/cli/deployments/test/fixture/deployment_atlas.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,25 @@ func NewMockAtlasDeploymentOpts(ctrl *gomock.Controller, deploymentName string)
4242
}
4343
}
4444

45-
func (m *MockDeploymentOpts) MockPaginatedAdvancedClusterDescription() *atlasClustersPinned.PaginatedAdvancedClusterDescription {
45+
func (m *MockDeploymentOpts) MockPaginatedAdvancedClusterDescription(state string) *atlasClustersPinned.PaginatedAdvancedClusterDescription {
4646
return &atlasClustersPinned.PaginatedAdvancedClusterDescription{
4747
Results: &[]atlasClustersPinned.AdvancedClusterDescription{
4848
{
4949
Name: pointer.Get(m.Opts.DeploymentName),
5050
Id: pointer.Get("123"),
5151
MongoDBVersion: pointer.Get("7.0.0"),
52-
StateName: pointer.Get("IDLE"),
52+
StateName: &state,
5353
Paused: pointer.Get(false),
5454
},
5555
},
5656
}
5757
}
5858

5959
func (m *MockDeploymentOpts) CommonAtlasMocks(projectID string) {
60+
m.CommonAtlasMocksWithState(projectID, "IDLE")
61+
}
62+
63+
func (m *MockDeploymentOpts) CommonAtlasMocksWithState(projectID string, state string) {
6064
m.MockCredentialsGetter.
6165
EXPECT().
6266
AuthType().
@@ -66,7 +70,7 @@ func (m *MockDeploymentOpts) CommonAtlasMocks(projectID string) {
6670
m.MockAtlasClusterListStore.
6771
EXPECT().
6872
ProjectClusters(projectID, gomock.Any()).
69-
Return(m.MockPaginatedAdvancedClusterDescription(), nil).
73+
Return(m.MockPaginatedAdvancedClusterDescription(state), nil).
7074
Times(1)
7175

7276
m.MockDeploymentTelemetry.

0 commit comments

Comments
 (0)