Skip to content

CLOUDP-322615: Add support for autoscaling mode in setup commands #3949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 10, 2025
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
4 changes: 4 additions & 0 deletions docs/command/atlas-deployments-setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Options
- IP address to grant access to the deployment.

Mutually exclusive with --currentIp.
* - --autoScalingMode
- string
- false
- The mode in which the cluster scales. Valid values are clusterWideScaling or independentShardScaling. This value defaults to "clusterWideScaling".
* - --bindIpAll
-
- false
Expand Down
4 changes: 4 additions & 0 deletions docs/command/atlas-setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Options
- IP address to grant access to the deployment.

Mutually exclusive with --currentIp.
* - --autoScalingMode
- string
- false
- The mode in which the cluster scales. Valid values are clusterWideScaling or independentShardScaling. This value defaults to "clusterWideScaling".
* - --clusterName
- string
- false
Expand Down
16 changes: 11 additions & 5 deletions internal/cli/clusters/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,17 @@ func TestCreateOpts_PostRun_EnableWatch(t *testing.T) {
mocks.NewMockClusterDescriber(ctrl),
}

expected := &atlasClustersPinned.AdvancedClusterDescription{
expected := &atlasv2.ClusterDescription20240805{
Name: pointer.Get("ProjectBar"),
StateName: pointer.Get("CREATING"),
}
expectedIdle := &atlasClustersPinned.AdvancedClusterDescription{

expectedCreatedCluster := &atlasClustersPinned.AdvancedClusterDescription{
Name: expected.Name,
StateName: expected.StateName,
}

expectedIdle := &atlasv2.ClusterDescription20240805{
Name: expected.Name,
StateName: pointer.Get("IDLE"),
}
Expand All @@ -184,20 +190,20 @@ func TestCreateOpts_PostRun_EnableWatch(t *testing.T) {
MockClusterCreator.
EXPECT().
CreateCluster(cluster).
Return(expected, nil).
Return(expectedCreatedCluster, nil).
Times(1)

gomock.InOrder(
mockStore.
MockClusterDescriber.
EXPECT().
AtlasCluster(createOpts.ProjectID, expected.GetName()).
LatestAtlasCluster(createOpts.ProjectID, expected.GetName()).
Return(expected, nil).
Times(1),
mockStore.
MockClusterDescriber.
EXPECT().
AtlasCluster(createOpts.ProjectID, expected.GetName()).
LatestAtlasCluster(createOpts.ProjectID, expected.GetName()).
Return(expectedIdle, nil).
Times(1),
)
Expand Down
198 changes: 198 additions & 0 deletions internal/cli/setup/cluster_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package setup

import (
"strings"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

const (
defaultNodeCount = 3
defaultPriority = 7
)

func defaultDiskSizeGB(provider, tier string) float64 {
return atlas.DefaultDiskSizeGB[strings.ToUpper(provider)][tier]
}

// newCluster creates a new cluster for the pinned API version.
func (opts *Opts) newCluster() *atlasClustersPinned.AdvancedClusterDescription {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is from cluster_setup.go, I updated it a bit to simplify but added unit tests to make sure it's behaving the same

cluster := &atlasClustersPinned.AdvancedClusterDescription{
GroupId: pointer.Get(opts.ConfigProjectID()),
ClusterType: pointer.Get(replicaSet),
Name: &opts.ClusterName,
TerminationProtectionEnabled: &opts.EnableTerminationProtection,
ReplicationSpecs: &[]atlasClustersPinned.ReplicationSpec{
{
NumShards: pointer.Get(1),
ZoneName: pointer.Get("Zone 1"),
RegionConfigs: &[]atlasClustersPinned.CloudRegionConfig{
opts.newAdvancedRegionConfig(),
},
},
},
}

opts.setTags(cluster)

if diskSizeGB := opts.getDiskSizeOverride(); diskSizeGB != nil {
cluster.DiskSizeGB = diskSizeGB
}
if version := opts.getVersionOverride(); version != nil {
cluster.MongoDBMajorVersion = version
}

return cluster
}

// setTags sets the tags for a cluster of the pinned API version.
func (opts *Opts) setTags(cluster *atlasClustersPinned.AdvancedClusterDescription) {
if len(opts.Tag) > 0 {
var tags []atlasClustersPinned.ResourceTag
for k, v := range opts.Tag {
if k != "" && v != "" {
tags = append(tags, atlasClustersPinned.ResourceTag{Key: k, Value: v})
}
}
cluster.Tags = &tags
}
}

// newAdvancedRegionConfig creates a new advanced region config for the pinned API version.
func (opts *Opts) newAdvancedRegionConfig() atlasClustersPinned.CloudRegionConfig {
providerName := opts.providerName()

regionConfig := atlasClustersPinned.CloudRegionConfig{
ProviderName: &providerName,
Priority: pointer.Get(defaultPriority),
RegionName: &opts.Region,
}

regionConfig.ElectableSpecs = &atlasClustersPinned.HardwareSpec{
InstanceSize: &opts.Tier,
}

if providerName == tenant {
regionConfig.BackingProviderName = &opts.Provider
} else {
regionConfig.ElectableSpecs.NodeCount = pointer.Get(defaultNodeCount)
}

return regionConfig
}

// getDiskSizeOverride returns the disk size override, defaults to nil if the provider is tenant or the disk size is 0.
func (opts *Opts) getDiskSizeOverride() *float64 {
if opts.providerName() == tenant {
return nil
}

diskSizeGB := defaultDiskSizeGB(opts.providerName(), opts.Tier)
if diskSizeGB == 0 {
return nil
}

return &diskSizeGB
}

// getVersionOverride returns the MongoDB major version override which is only applicable for non-tenant clusters.
// if the user has not provided a version override, the default MongoDB major version is returned.
func (opts *Opts) getVersionOverride() *string {
if opts.providerName() == tenant {
return nil
}

if opts.MDBVersion != "" {
return &opts.MDBVersion
}

if mdbVersion, err := cli.DefaultMongoDBMajorVersion(); err == nil && mdbVersion != "" {
return &mdbVersion
}

return nil
}

// newClusterLatest creates a new cluster for the latest API version.
func (opts *Opts) newClusterLatest() *atlasv2.ClusterDescription20240805 {
cluster := &atlasv2.ClusterDescription20240805{
GroupId: pointer.Get(opts.ConfigProjectID()),
ClusterType: pointer.Get(replicaSet),
Name: pointer.Get(opts.ClusterName),
TerminationProtectionEnabled: &opts.EnableTerminationProtection,
ReplicationSpecs: &[]atlasv2.ReplicationSpec20240805{
{
ZoneName: pointer.Get("Zone 1"),
RegionConfigs: &[]atlasv2.CloudRegionConfig20240805{
opts.newAdvancedRegionConfigLatest(),
},
},
},
}

opts.setTagsLatest(cluster)

version := opts.getVersionOverride()
if version != nil {
cluster.MongoDBMajorVersion = version
}

return cluster
}

// newAdvancedRegionConfigLatest creates a new advanced region config for the latest API version.
func (opts *Opts) newAdvancedRegionConfigLatest() atlasv2.CloudRegionConfig20240805 {
regionConfig := atlasv2.CloudRegionConfig20240805{
ProviderName: pointer.Get(opts.providerName()),
Priority: pointer.Get(defaultPriority),
RegionName: pointer.Get(opts.Region),
ElectableSpecs: &atlasv2.HardwareSpec20240805{
InstanceSize: pointer.Get(opts.Tier),
},
}

if opts.providerName() == tenant {
regionConfig.BackingProviderName = &opts.Provider
} else {
regionConfig.ElectableSpecs.NodeCount = pointer.Get(defaultNodeCount)
}

// diskSize is now a field in the HardwareSpec20240805 struct
diskSizeGB := opts.getDiskSizeOverride()
if diskSizeGB != nil {
regionConfig.ElectableSpecs.DiskSizeGB = diskSizeGB
}

return regionConfig
}

// setTagsLatest sets the tags for a cluster of the latest API version.
func (opts *Opts) setTagsLatest(cluster *atlasv2.ClusterDescription20240805) {
if len(opts.Tag) > 0 {
var tags []atlasv2.ResourceTag
for k, v := range opts.Tag {
if k != "" && v != "" {
tags = append(tags, atlasv2.ResourceTag{Key: k, Value: v})
}
}
cluster.Tags = &tags
}
}
Loading
Loading