Skip to content

Commit 38dd975

Browse files
committed
update create command
1 parent fb737db commit 38dd975

File tree

4 files changed

+212
-157
lines changed

4 files changed

+212
-157
lines changed

docs/command/atlas-clusters-create.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Options
6363
- Type
6464
- Required
6565
- Description
66+
* - --autoScalingMode
67+
- string
68+
- false
69+
- The mode in which the cluster scales. Valid values are clusterWideScaling or independentShardScaling.
70+
71+
Mutually exclusive with --file. This value defaults to "clusterWideScaling".
6672
* - --backup
6773
-
6874
- false
@@ -88,7 +94,7 @@ Options
8894
- false
8995
- Path to an optional JSON configuration file that defines cluster settings. Note: Unsupported fields in the JSON file are ignored. To learn more about cluster configuration files for the Atlas CLI, see https://dochub.mongodb.org/core/cluster-config-file-atlascli.
9096

91-
Mutually exclusive with --tier, --provider, --members, --region, --diskSizeGB, --mdbVersion, --biConnector, --type, --shards, --tag.
97+
Mutually exclusive with --tier, --provider, --members, --region, --diskSizeGB, --mdbVersion, --biConnector, --type, --shards, --tag, --autoScalingMode.
9298
* - -h, --help
9399
-
94100
- false

internal/cli/clusters/create.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
2626
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
27-
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/clusterconfig"
2827
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
2928
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/file"
3029
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
@@ -51,6 +50,8 @@ const (
5150
regionName = "regionName"
5251
priority = 7
5352
readOnlyNode = 0
53+
independentShardScalingFlag = "independentShardScaling"
54+
clusterWideScalingFlag = "clusterWideScaling"
5455
)
5556

5657
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=create_mock_test.go -package=clusters . ClusterCreator
@@ -102,7 +103,7 @@ var flexCluster *atlasv2.FlexClusterDescription20241113
102103
var clusterObjLatest *atlasv2.ClusterDescription20240805
103104

104105
func (opts *CreateOpts) Run() error {
105-
if opts.autoScalingMode == clusterconfig.IndependentShardScalingShard {
106+
if opts.autoScalingMode == independentShardScalingFlag {
106107
return opts.RunDedicatedClusterLatest()
107108
}
108109

@@ -159,7 +160,6 @@ func (opts *CreateOpts) RunDedicatedClusterLatest() error {
159160
}
160161

161162
clusterObjLatest, err = opts.store.CreateClusterLatest(cluster)
162-
163163
apiError, ok := atlasv2.AsError(err)
164164
code := apiError.GetErrorCode()
165165
if ok {
@@ -196,12 +196,12 @@ func (opts *CreateOpts) RunDedicatedCluster() error {
196196
}
197197

198198
func (opts *CreateOpts) PostRun() error {
199-
if opts.isFlexCluster {
200-
return opts.PostRunFlexCluster()
199+
if opts.autoScalingMode == independentShardScalingFlag {
200+
return opts.PostRunDedicatedClusterLatest()
201201
}
202202

203-
if opts.autoScalingMode == clusterconfig.IndependentShardScalingShard {
204-
return opts.PostRunDedicatedClusterLatest()
203+
if opts.isFlexCluster {
204+
return opts.PostRunFlexCluster()
205205
}
206206

207207
return opts.PostRunDedicatedCluster()
@@ -338,7 +338,7 @@ func (opts *CreateOpts) applyOptsAdvancedCluster(out *atlasClustersPinned.Advanc
338338

339339
out.ReplicationSpecs = &[]atlasClustersPinned.ReplicationSpec{replicationSpec}
340340

341-
clusterconfig.SetTags(out, opts.tag)
341+
addTags(out, opts.tag)
342342
}
343343

344344
func (opts *CreateOpts) applyOptsClusterLatest(out *atlasv2.ClusterDescription20240805) {
@@ -360,7 +360,9 @@ func (opts *CreateOpts) applyOptsClusterLatest(out *atlasv2.ClusterDescription20
360360
out.BiConnector = &atlasv2.BiConnector{Enabled: &opts.biConnector}
361361
}
362362

363-
clusterconfig.SetTagsLatest(out, opts.tag)
363+
if len(opts.tag) > 0 {
364+
out.Tags = newResourceTags(opts.tag)
365+
}
364366
}
365367

366368
func (opts *CreateOpts) newFlexClusterDescriptionCreate20241113() *atlasv2.FlexClusterDescriptionCreate20241113 {
@@ -397,7 +399,7 @@ func (opts *CreateOpts) newAdvanceReplicationSpec() atlasClustersPinned.Replicat
397399

398400
func (opts *CreateOpts) newAdvanceReplicationSpecsLatest() *[]atlasv2.ReplicationSpec20240805 {
399401
replicationSpecs := make([]atlasv2.ReplicationSpec20240805, opts.shards)
400-
for i := 0; i < opts.shards; i++ {
402+
for i := range opts.shards {
401403
replicationSpecs[i] = atlasv2.ReplicationSpec20240805{
402404
ZoneName: pointer.Get(zoneName),
403405
RegionConfigs: &[]atlasv2.CloudRegionConfig20240805{
@@ -415,13 +417,22 @@ func (opts *CreateOpts) newAdvancedRegionConfig() atlasClustersPinned.CloudRegio
415417
Priority: pointer.Get(priority),
416418
RegionName: &opts.region,
417419
ProviderName: &providerName,
420+
ElectableSpecs: &atlasClustersPinned.HardwareSpec{
421+
InstanceSize: &opts.tier,
422+
},
418423
}
419424

420425
if providerName == tenant {
421426
regionConfig.BackingProviderName = &opts.provider
422427
} else {
423-
regionConfig.ElectableSpecs.NodeCount = pointer.Get(opts.members)
428+
regionConfig.ElectableSpecs.NodeCount = &opts.members
429+
}
430+
431+
readOnlySpec := &atlasClustersPinned.DedicatedHardwareSpec{
432+
InstanceSize: &opts.tier,
433+
NodeCount: pointer.Get(readOnlyNode),
424434
}
435+
regionConfig.ReadOnlySpecs = readOnlySpec
425436

426437
return regionConfig
427438
}
@@ -480,11 +491,11 @@ func (opts *CreateOpts) validateTier() error {
480491
}
481492

482493
func (opts *CreateOpts) validateAutoScalingMode() error {
483-
if opts.isFlexCluster && opts.autoScalingMode != clusterconfig.ClusterWideScalingFlag {
494+
if opts.isFlexCluster && opts.autoScalingMode != clusterWideScalingFlag {
484495
return fmt.Errorf("flex is incompatible with %s auto scaling mode", opts.autoScalingMode)
485496
}
486497

487-
if opts.autoScalingMode != clusterconfig.ClusterWideScalingFlag && opts.autoScalingMode != clusterconfig.IndependentShardScalingShard {
498+
if opts.autoScalingMode != clusterWideScalingFlag && opts.autoScalingMode != independentShardScalingFlag {
488499
return fmt.Errorf("invalid auto scaling mode: %s", opts.autoScalingMode)
489500
}
490501

@@ -499,15 +510,15 @@ func (opts *CreateOpts) validateAutoScalingMode() error {
499510
oldCluster := new(atlasClustersPinned.AdvancedClusterDescription)
500511
oldLoadErr := file.Load(opts.fs, opts.filename, oldCluster)
501512
if oldLoadErr == nil {
502-
opts.autoScalingMode = clusterconfig.ClusterWideScalingFlag
513+
opts.autoScalingMode = clusterWideScalingFlag
503514
return nil
504515
}
505516

506517
// Then try to load as an ISS cluster
507518
cluster := new(atlasv2.ClusterDescription20240805)
508519
latestLoadErr := file.Load(opts.fs, opts.filename, cluster)
509520
if latestLoadErr == nil {
510-
opts.autoScalingMode = clusterconfig.IndependentShardScalingShard
521+
opts.autoScalingMode = independentShardScalingFlag
511522
return nil
512523
}
513524

@@ -571,6 +582,7 @@ Deprecation note: the M2 and M5 tiers are now deprecated; when selecting M2 or M
571582
return opts.PreRunE(
572583
opts.validateTier,
573584
opts.newIsFlexCluster,
585+
opts.validateAutoScalingMode,
574586
opts.ValidateProjectID,
575587
opts.initStore(cmd.Context()),
576588
opts.InitOutput(cmd.OutOrStdout(), createTemplate),
@@ -608,7 +620,7 @@ Deprecation note: the M2 and M5 tiers are now deprecated; when selecting M2 or M
608620
cmd.Flags().IntVarP(&opts.shards, flag.Shards, flag.ShardsShort, defaultShardSize, usage.Shards)
609621
cmd.Flags().BoolVar(&opts.enableTerminationProtection, flag.EnableTerminationProtection, false, usage.EnableTerminationProtection)
610622
cmd.Flags().StringToStringVar(&opts.tag, flag.Tag, nil, usage.Tag)
611-
cmd.Flags().StringVar(&opts.autoScalingMode, flag.AutoScalingMode, clusterconfig.ClusterWideScalingFlag, usage.AutoScalingMode)
623+
cmd.Flags().StringVar(&opts.autoScalingMode, flag.AutoScalingMode, clusterWideScalingFlag, usage.AutoScalingMode)
612624

613625
cmd.Flags().BoolVarP(&opts.EnableWatch, flag.EnableWatch, flag.EnableWatchShort, false, usage.EnableWatch)
614626
cmd.Flags().Int64Var(&opts.Timeout, flag.WatchTimeout, 0, usage.WatchTimeout)
@@ -639,7 +651,7 @@ Deprecation note: the M2 and M5 tiers are now deprecated; when selecting M2 or M
639651
})
640652

641653
_ = cmd.RegisterFlagCompletionFunc(flag.AutoScalingMode, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
642-
return []string{clusterconfig.ClusterWideScalingFlag, clusterconfig.IndependentShardScalingShard}, cobra.ShellCompDirectiveDefault
654+
return []string{clusterWideScalingFlag, independentShardScalingFlag}, cobra.ShellCompDirectiveDefault
643655
})
644656

645657
autocomplete := &autoCompleteOpts{}

0 commit comments

Comments
 (0)