-
Notifications
You must be signed in to change notification settings - Fork 89
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
84bdb22
CLOUDP-323473: Migrate watchers to latest SDK
blva 30cefe2
CLOUDP-322615: Add support for autoscaling mode in setup
blva 61e066b
add tests to validate requests are the same
blva 599550f
make local and autoscaling mutually exclusive
blva af943e2
add docs
blva b9ad493
update setup
blva 033d813
Add int tests
blva e8d63cb
generate random ip
blva 27e3e32
Update snapshots
apix-bot[bot] f333fc2
Revert "Update snapshots"
blva 21ed3b0
update test setup snapshots
blva 48b4aa3
address comment and add unit test
blva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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