• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb / mongodb-atlas-cli / 25848964073

14 May 2026 07:59AM UTC coverage: 22.479% (-41.3%) from 63.771%
25848964073

push

github

web-flow
build(deps): bump test-summary/action from 2.4 to 2.6 (#4576)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

8987 of 39979 relevant lines covered (22.48%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.29
/internal/cli/clusters/clusters.go
1
// Copyright 2020 MongoDB Inc
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package clusters
16

17
import (
18
        "errors"
19
        "strings"
20

21
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
22
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/advancedsettings"
23
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/availableregions"
24
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/connectionstring"
25
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/indexes"
26
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/onlinearchive"
27
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/clusters/sampledata"
28
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/search"
29
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/file"
30
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
31
        "github.com/spf13/afero"
32
        "github.com/spf13/cobra"
33
        atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
34
        atlasv2 "go.mongodb.org/atlas-sdk/v20250312018/admin"
35
        atlas "go.mongodb.org/atlas/mongodbatlas"
36
)
37

38
var errFailedToLoadClusterFileMessage = errors.New("failed to parse JSON file")
39

40
const (
41
        cannotUseFlexWithClusterApisErrorCode = "CANNOT_USE_FLEX_CLUSTER_IN_CLUSTER_API"
42
        deprecateMessageSharedTier            = "Deprecation note: the M2 and M5 tiers are now deprecated ('%s' was selected); when selecting M2 or M5, a FLEX tier will be created instead. For the migration guide, visit: https://dochub.mongodb.org/core/flex-migration.\n"
43
        independentShardScalingFlag           = "independentShardScaling"
44
        clusterWideScalingFlag                = "clusterWideScaling"
45
        clusterWideScalingResponse            = "CLUSTER_WIDE_SCALING"
46
        independentShardScalingResponse       = "INDEPENDENT_SHARD_SCALING"
47
)
48

49
func Builder() *cobra.Command {
×
50
        const use = "clusters"
×
51
        cmd := &cobra.Command{
×
52
                Use:        use,
×
53
                Aliases:    cli.GenerateAliases(use),
×
54
                SuggestFor: []string{"replicasets"},
×
55
                Short:      "Manage clusters for your project.",
×
56
                Long:       `The clusters command provides access to your cluster configurations. You can create, edit, and delete clusters.`,
×
57
        }
×
58
        cmd.AddCommand(
×
59
                ListBuilder(),
×
60
                DescribeBuilder(),
×
61
                advancedsettings.Builder(),
×
62
                CreateBuilder(),
×
63
                ConnectBuilder(),
×
64
                WatchBuilder(),
×
65
                UpdateBuilder(),
×
66
                PauseBuilder(),
×
67
                StartBuilder(),
×
68
                DeleteBuilder(),
×
69
                LoadSampleDataBuilder(),
×
70
                UpgradeBuilder(),
×
71
                FailoverBuilder(),
×
72
                indexes.Builder(),
×
73
                search.Builder(),
×
74
                onlinearchive.Builder(),
×
75
                connectionstring.Builder(),
×
76
                availableregions.Builder(),
×
77
                sampledata.Builder(),
×
78
                GetAutoscalingConfigBuilder(),
×
79
        )
×
80

×
81
        return cmd
×
82
}
×
83

84
func addTags(out *atlasClustersPinned.AdvancedClusterDescription, tags map[string]string) {
1✔
85
        resourceTagsAtlasV2 := newResourceTags(tags)
1✔
86
        if resourceTagsAtlasV2 == nil {
2✔
87
                return
1✔
88
        }
1✔
89

90
        resourceTags := make([]atlasClustersPinned.ResourceTag, len(*resourceTagsAtlasV2))
×
91
        for i, v := range *resourceTagsAtlasV2 {
×
92
                resourceTags[i] = atlasClustersPinned.ResourceTag{
×
93
                        Key:   v.Key,
×
94
                        Value: v.Value,
×
95
                }
×
96
        }
×
97

98
        out.Tags = &resourceTags
×
99
}
100

101
func newResourceTags(tags map[string]string) *[]atlasv2.ResourceTag {
1✔
102
        if len(tags) == 0 {
2✔
103
                return nil
1✔
104
        }
1✔
105
        t := make([]atlasv2.ResourceTag, len(tags))
1✔
106
        i := 0
1✔
107
        for k, v := range tags {
2✔
108
                if k == "" || v == "" {
1✔
109
                        continue
×
110
                }
111
                key, value := k, v
1✔
112
                tag := atlasv2.ResourceTag{
1✔
113
                        Key:   key,
1✔
114
                        Value: value,
1✔
115
                }
1✔
116
                t[i] = tag
1✔
117
                i++
1✔
118
        }
119

120
        return &t
1✔
121
}
122

123
func removeReadOnlyAttributes(out *atlasClustersPinned.AdvancedClusterDescription) {
1✔
124
        out.Id = nil
1✔
125
        out.CreateDate = nil
1✔
126
        out.StateName = nil
1✔
127
        out.MongoDBVersion = nil
1✔
128
        out.ConnectionStrings = nil
1✔
129
        isTenant := false
1✔
130

1✔
131
        for i, spec := range out.GetReplicationSpecs() {
2✔
132
                (*out.ReplicationSpecs)[i].Id = nil
1✔
133
                for _, c := range spec.GetRegionConfigs() {
1✔
134
                        if c.GetProviderName() == tenant {
×
135
                                isTenant = true
×
136
                                break
×
137
                        }
138
                }
139
        }
140

141
        if isTenant {
1✔
142
                out.BiConnector = nil
×
143
                out.EncryptionAtRestProvider = nil
×
144
                out.DiskSizeGB = nil
×
145
                out.MongoDBMajorVersion = nil
×
146
                out.PitEnabled = nil
×
147
                out.BackupEnabled = nil
×
148
        }
×
149
}
150

151
func removeReadOnlyAttributesLatest(out *atlasv2.ClusterDescription20240805) {
1✔
152
        out.Id = nil
1✔
153
        out.CreateDate = nil
1✔
154
        out.StateName = nil
1✔
155
        out.MongoDBVersion = nil
1✔
156
        out.ConnectionStrings = nil
1✔
157
        isTenant := false
1✔
158

1✔
159
        for i, spec := range out.GetReplicationSpecs() {
2✔
160
                (*out.ReplicationSpecs)[i].Id = nil
1✔
161
                for _, c := range spec.GetRegionConfigs() {
2✔
162
                        if c.GetProviderName() == tenant {
2✔
163
                                isTenant = true
1✔
164
                                // Set disksize to nil for tenant clusters
1✔
165
                                for _, c := range spec.GetRegionConfigs() {
2✔
166
                                        c.ElectableSpecs.DiskSizeGB = nil
1✔
167
                                }
1✔
168
                        }
169
                }
170
        }
171

172
        if isTenant {
2✔
173
                out.BiConnector = nil
1✔
174
                out.EncryptionAtRestProvider = nil
1✔
175
                out.MongoDBMajorVersion = nil
1✔
176
                out.PitEnabled = nil
1✔
177
                out.BackupEnabled = nil
1✔
178
        }
1✔
179
}
180

181
func removeReadOnlyAttributesSharedCluster(out *atlas.Cluster) {
1✔
182
        out.ID = ""
1✔
183
        out.CreateDate = ""
1✔
184
        out.StateName = ""
1✔
185
        out.MongoDBVersion = ""
1✔
186
        out.ConnectionStrings = nil
1✔
187
        out.ReplicationSpec = nil
1✔
188
        out.MongoURI = ""
1✔
189
        out.MongoURIUpdated = ""
1✔
190
        out.MongoURIWithOptions = ""
1✔
191
        if out.ProviderSettings != nil {
1✔
192
                out.ProviderSettings.AutoScaling = nil
×
193
        }
×
194

195
        for _, spec := range out.ReplicationSpecs {
1✔
196
                spec.ID = ""
×
197
        }
×
198
}
199

200
func isIndependentShardScaling(mode string) bool {
1✔
201
        return strings.EqualFold(mode, independentShardScalingFlag) || strings.EqualFold(mode, independentShardScalingResponse)
1✔
202
}
1✔
203

204
func isClusterWideScaling(mode string) bool {
1✔
205
        return strings.EqualFold(mode, clusterWideScalingFlag) || strings.EqualFold(mode, clusterWideScalingResponse)
1✔
206
}
1✔
207

208
func detectAutoScalingModeFromFile(fs afero.Fs, filename string) string {
1✔
209
        // First try to load as a default dedicated cluster in strict mode.
1✔
210
        // If it succeeds, it is a default dedicated cluster.
1✔
211
        oldCluster := new(atlasClustersPinned.AdvancedClusterDescription)
1✔
212
        oldLoadErr := file.StrictLoad(fs, filename, oldCluster)
1✔
213
        if oldLoadErr == nil {
1✔
214
                return clusterWideScalingFlag
×
215
        }
×
216

217
        // Then try to load as an ISS cluster in strict mode.
218
        // If it succeeds, it is an ISS cluster. If it fails, it is a default dedicated cluster.
219
        cluster := new(atlasv2.ClusterDescription20240805)
1✔
220
        latestLoadErr := file.StrictLoad(fs, filename, cluster)
1✔
221
        if latestLoadErr == nil {
2✔
222
                return independentShardScalingFlag
1✔
223
        }
1✔
224

225
        // default to cluster wide scaling
226
        return clusterWideScalingFlag
1✔
227
}
228

229
func appendAutoScalingModeTelemetry(mode string) {
1✔
230
        if mode == "" {
2✔
231
                return
1✔
232
        }
1✔
233

234
        if isIndependentShardScaling(mode) {
1✔
235
                telemetry.AppendOption(telemetry.WithDetectedAutoScalingMode("independentShardScaling"))
×
236
        } else if isClusterWideScaling(mode) {
2✔
237
                telemetry.AppendOption(telemetry.WithDetectedAutoScalingMode("clusterWideScaling"))
1✔
238
        }
1✔
239
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc