Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions internal/cli/deployments/options/deployment_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ import (
const (
spinnerSpeed = 100 * time.Millisecond
// based on https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Clusters/operation/createCluster
clusterNamePattern = "^[a-zA-Z0-9][a-zA-Z0-9-]*$"
PausedState = "PAUSED"
StoppedState = "STOPPED"
IdleState = "IDLE"
UpdatingState = "UPDATING"
DeletingState = "DELETING"
RestartingState = "RESTARTING"
LocalCluster = "local"
AtlasCluster = "atlas"
CompassConnect = "compass"
MongoshConnect = "mongosh"
VsCodeConnect = "vscode"
PromptTypeMessage = "What type of deployment would you like to work with?"
MaxItemsPerPage = 500
ContainerFilter = "mongodb-atlas-local=container"
bytesInGb = 1073741824
minimumRAM = 2 * bytesInGb
minimumCores = 2
clusterNamePattern = "^[a-zA-Z0-9][a-zA-Z0-9-]*$"
PausedState = "PAUSED"
StoppedState = "STOPPED"
IdleState = "IDLE"
UpdatingState = "UPDATING"
DeletingState = "DELETING"
RestartingState = "RESTARTING"
LocalCluster = "local"
AtlasCluster = "atlas"
CompassConnect = "compass"
MongoshConnect = "mongosh"
VsCodeConnect = "vscode"
PromptTypeMessage = "What type of deployment would you like to work with?"
MaxItemsPerPage = 500
ContainerFilter = "mongodb-atlas-local=container"
ClusterWideScaling = "CLUSTER_WIDE_SCALING"
IndependentShardScaling = "INDEPENDENT_SHARD_SCALING"
bytesInGb = 1073741824
minimumRAM = 2 * bytesInGb
minimumCores = 2
)

var (
Expand Down
15 changes: 14 additions & 1 deletion internal/cli/deployments/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
)

//go:generate go tool go.uber.org/mock/mockgen -typed -destination=pause_mock_test.go -package=deployments . ClusterPauser

type ClusterPauser interface {
PauseCluster(string, string) (*atlasClustersPinned.AdvancedClusterDescription, error)
PauseClusterLatest(string, string) (*atlasv2.ClusterDescription20240805, error)
GetClusterAutoScalingConfig(string, string) (*atlasv2.ClusterDescriptionAutoScalingModeConfiguration, error)
}

type PauseOpts struct {
Expand Down Expand Up @@ -104,7 +107,17 @@ func (opts *PauseOpts) RunAtlas() error {
opts.StartSpinner()
defer opts.StopSpinner()

r, err := opts.store.PauseCluster(opts.ConfigProjectID(), opts.DeploymentName)
clusterAutoScalingConfig, err := opts.store.GetClusterAutoScalingConfig(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil || clusterAutoScalingConfig.GetAutoScalingMode() == options.ClusterWideScaling {
r, err := opts.store.PauseCluster(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil {
return err
}
return opts.Print(r)
}

// If cluster is not cluster wide scaling, we use the latest API version
r, err := opts.store.PauseClusterLatest(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil {
return err
}
Expand Down
79 changes: 79 additions & 0 deletions internal/cli/deployments/pause_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 60 additions & 1 deletion internal/cli/deployments/pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"testing"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/deployments/options"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/deployments/test/fixture"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/stretchr/testify/assert"
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
"go.uber.org/mock/gomock"
)

Expand Down Expand Up @@ -70,7 +72,7 @@ func TestPause_RunLocal(t *testing.T) {
t.Log(buf.String())
}

func TestPause_RunAtlas(t *testing.T) {
func TestPause_RunAtlas_clusterWideScaling(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := NewMockClusterPauser(ctrl)
ctx := t.Context()
Expand All @@ -92,6 +94,15 @@ func TestPause_RunAtlas(t *testing.T) {

deploymentTest.CommonAtlasMocks(projectID)

mockStore.
EXPECT().
GetClusterAutoScalingConfig(projectID, deploymentName).
Return(
&atlasv2.ClusterDescriptionAutoScalingModeConfiguration{
AutoScalingMode: pointer.Get(options.ClusterWideScaling),
}, nil).
Times(1)

mockStore.
EXPECT().
PauseCluster(projectID, deploymentName).
Expand All @@ -109,6 +120,54 @@ func TestPause_RunAtlas(t *testing.T) {
t.Log(buf.String())
}

func TestPause_RunAtlas_independentShardScaling(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := NewMockClusterPauser(ctrl)
ctx := t.Context()

deploymentTest := fixture.NewMockAtlasDeploymentOpts(ctrl, deploymentName)

buf := new(bytes.Buffer)
listOpts := &PauseOpts{
store: mockStore,
DeploymentOpts: *deploymentTest.Opts,
ProjectOpts: cli.ProjectOpts{
ProjectID: projectID,
},
OutputOpts: cli.OutputOpts{
Template: pauseTemplate,
OutWriter: buf,
},
}

deploymentTest.CommonAtlasMocks(projectID)

mockStore.
EXPECT().
GetClusterAutoScalingConfig(projectID, deploymentName).
Return(
&atlasv2.ClusterDescriptionAutoScalingModeConfiguration{
AutoScalingMode: pointer.Get(string(options.IndependentShardScaling)),
}, nil).
Times(1)

mockStore.
EXPECT().
PauseClusterLatest(projectID, deploymentName).
Return(
&atlasv2.ClusterDescription20240805{
Name: pointer.Get(deploymentName),
}, nil).
Times(1)

if err := listOpts.Run(ctx); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

assert.Equal(t, fmt.Sprintf("Pausing deployment '%s'.\n", deploymentName), buf.String())
t.Log(buf.String())
}

func TestPauseOpts_PostRun(t *testing.T) {
ctrl := gomock.NewController(t)
deploymentTest := fixture.NewMockLocalDeploymentOpts(ctrl, deploymentName)
Expand Down
15 changes: 14 additions & 1 deletion internal/cli/deployments/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
)

//go:generate go tool go.uber.org/mock/mockgen -typed -destination=start_mock_test.go -package=deployments . ClusterStarter

type ClusterStarter interface {
StartCluster(string, string) (*atlasClustersPinned.AdvancedClusterDescription, error)
StartClusterLatest(string, string) (*atlasv2.ClusterDescription20240805, error)
GetClusterAutoScalingConfig(string, string) (*atlasv2.ClusterDescriptionAutoScalingModeConfiguration, error)
}

type StartOpts struct {
Expand Down Expand Up @@ -83,7 +86,17 @@ func (opts *StartOpts) RunAtlas() error {
opts.StartSpinner()
defer opts.StopSpinner()

r, err := opts.store.StartCluster(opts.ConfigProjectID(), opts.DeploymentName)
clusterAutoScalingConfig, err := opts.store.GetClusterAutoScalingConfig(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil || clusterAutoScalingConfig.GetAutoScalingMode() == options.ClusterWideScaling {
r, err := opts.store.StartCluster(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil {
return err
}
return opts.Print(r)
}

// If cluster is not cluster wide scaling, we use the latest API version
r, err := opts.store.StartClusterLatest(opts.ConfigProjectID(), opts.DeploymentName)
if err != nil {
return err
}
Expand Down
Loading
Loading