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

mongodb / mongodb-atlas-cli / 16670223591

01 Aug 2025 08:25AM UTC coverage: 57.953% (-7.1%) from 65.017%
16670223591

Pull #4071

github

fmenezes
chore: remove unit tag from tests
Pull Request #4071: chore: remove unit tag from tests

23613 of 40745 relevant lines covered (57.95%)

2.74 hits per line

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

9.21
/internal/cli/setup/cluster_setup.go
1
// Copyright 2021 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 setup
16

17
import (
18
        "errors"
19
        "fmt"
20
        "os"
21
        "slices"
22
        "strings"
23

24
        "github.com/AlecAivazis/survey/v2"
25
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
26
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/search"
27
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
28
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
29
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
30
        "golang.org/x/mod/semver"
31
)
32

33
var ErrNoRegions = errors.New("no regions found for the cloud provider")
34
var ErrNoVersions = errors.New("no mongodb versions found for the cloud provider")
35
var deprecatedClusterWideScalingMessage = "Detected clusterWideScaling mode. If you require more flexibility in shard scaling, consider using --autoScalingMode independentShardScaling."
36

37
const (
38
        tenant  = "TENANT"
39
        atlasM2 = "M2"
40
        atlasM5 = "M5"
41
)
42

43
func (opts *Opts) createCluster() error {
1✔
44
        if opts.AutoScalingMode == clusterWideScaling {
2✔
45
                _, _ = fmt.Fprintln(os.Stderr, deprecatedClusterWideScalingMessage)
1✔
46

1✔
47
                if _, err := opts.store.CreateCluster(opts.newCluster()); err != nil {
1✔
48
                        return err
×
49
                }
×
50
                return nil
1✔
51
        }
52

53
        if _, err := opts.store.CreateClusterLatest(opts.newClusterLatest()); err != nil {
×
54
                return err
×
55
        }
×
56

57
        return nil
×
58
}
59

60
func (opts *Opts) askClusterOptions() error {
×
61
        var qs []*survey.Question
×
62

×
63
        if opts.shouldAskForValue(flag.ClusterName) {
×
64
                if opts.ClusterName == "" {
×
65
                        opts.ClusterName = opts.defaultName
×
66
                }
×
67
                qs = append(qs, newClusterNameQuestion(opts.ClusterName))
×
68
        }
69

70
        if opts.shouldAskForValue(flag.Provider) {
×
71
                qs = append(qs, newClusterProviderQuestion())
×
72
        }
×
73

74
        if opts.shouldAskForValue(flag.ClusterName) || opts.shouldAskForValue(flag.Provider) || opts.shouldAskForValue(flag.Region) || opts.shouldAskForValue(flag.MDBVersion) {
×
75
                fmt.Print(`
×
76
[Set up your Atlas cluster]
×
77
`)
×
78
        }
×
79

80
        if err := telemetry.TrackAsk(qs, opts); err != nil {
×
81
                return err
×
82
        }
×
83

84
        // We need the provider to ask for the region
85
        if opts.shouldAskForValue(flag.Region) {
×
86
                if err := opts.askClusterRegion(); err != nil {
×
87
                        return err
×
88
                }
×
89
        }
90

91
        if opts.shouldAskForValue(flag.MDBVersion) {
×
92
                return opts.askClusterMDBVersion()
×
93
        }
×
94

95
        return nil
×
96
}
97

98
func (opts *Opts) askClusterRegion() error {
×
99
        regions, err := opts.defaultRegions()
×
100
        if err != nil {
×
101
                return err
×
102
        }
×
103

104
        if len(regions) == 0 {
×
105
                return fmt.Errorf("%w: %v", ErrNoRegions, opts.Provider)
×
106
        }
×
107

108
        regionQ := newRegionQuestions(regions)
×
109
        return telemetry.TrackAskOne(regionQ, &opts.Region, survey.WithValidator(survey.Required))
×
110
}
111

112
func (opts *Opts) askClusterMDBVersion() error {
×
113
        if opts.providerName() == tenant {
×
114
                return nil
×
115
        }
×
116

117
        versions, defaultVersion, err := opts.mdbVersions(opts.Tier)
×
118
        if err != nil {
×
119
                return err
×
120
        }
×
121

122
        if len(versions) == 0 {
×
123
                return nil
×
124
        }
×
125

126
        return telemetry.TrackAskOne(newMDBVersionQuestion(versions, defaultVersion), &opts.MDBVersion, survey.WithValidator(survey.Required))
×
127
}
128

129
func newRegionQuestions(defaultRegions []string) survey.Prompt {
×
130
        return &survey.Select{
×
131
                Message: "Cloud Provider Region",
×
132
                Help:    usage.Region,
×
133
                Options: defaultRegions,
×
134
        }
×
135
}
×
136

137
func newMDBVersionQuestion(versions []string, defaultVersion string) survey.Prompt {
×
138
        return &survey.Select{
×
139
                Message: "Mongodb Version",
×
140
                Help:    usage.MDBVersion,
×
141
                Options: versions,
×
142
                Default: defaultVersion,
×
143
        }
×
144
}
×
145

146
func providerName(tier, provider string) string {
80✔
147
        if tier == DefaultAtlasTier || tier == atlasM2 || tier == atlasM5 {
99✔
148
                return tenant
19✔
149
        }
19✔
150
        return strings.ToUpper(provider)
63✔
151
}
152

153
func (opts *Opts) providerName() string {
80✔
154
        return providerName(opts.Tier, opts.Provider)
80✔
155
}
80✔
156

157
func (opts *clusterSettings) providerName() string {
×
158
        return providerName(opts.Tier, opts.Provider)
×
159
}
×
160

161
// Regions overlap.
162
func (opts *Opts) defaultRegions() ([]string, error) {
×
163
        cloudProviders, err := opts.store.CloudProviderRegions(
×
164
                opts.ConfigProjectID(),
×
165
                opts.Tier,
×
166
                []string{opts.Provider},
×
167
        )
×
168

×
169
        if err != nil {
×
170
                return nil, err
×
171
        }
×
172

173
        if len(cloudProviders.GetResults()) == 0 || len(cloudProviders.GetResults()[0].GetInstanceSizes()) == 0 {
×
174
                return nil, errors.New("no regions available")
×
175
        }
×
176

177
        availableRegions := cloudProviders.GetResults()[0].GetInstanceSizes()[0].GetAvailableRegions()
×
178

×
179
        defaultRegions := make([]string, 0, len(availableRegions))
×
180
        popularRegionIndex := search.DefaultRegion(availableRegions)
×
181

×
182
        if popularRegionIndex != -1 {
×
183
                // the most popular region must be the first in the list
×
184
                popularRegion := availableRegions[popularRegionIndex]
×
185
                defaultRegions = append(defaultRegions, popularRegion.GetName())
×
186

×
187
                // remove popular region from availableRegions
×
188
                availableRegions = append(availableRegions[:popularRegionIndex], availableRegions[popularRegionIndex+1:]...)
×
189
        }
×
190

191
        for _, v := range availableRegions {
×
192
                defaultRegions = append(defaultRegions, v.GetName())
×
193
        }
×
194

195
        return defaultRegions, nil
×
196
}
197

198
func (opts *Opts) mdbVersions(instanceSize string) ([]string, string, error) {
×
199
        const maxItems = 500
×
200
        versions, err := opts.store.MDBVersions(
×
201
                opts.ConfigProjectID(),
×
202
                &store.MDBVersionListOptions{
×
203
                        ListOptions: store.ListOptions{
×
204
                                ItemsPerPage: maxItems,
×
205
                        },
×
206
                        InstanceSize: &instanceSize,
×
207
                },
×
208
        )
×
209

×
210
        if err != nil {
×
211
                return nil, "", err
×
212
        }
×
213

214
        if len(versions.GetResults()) == 0 {
×
215
                return nil, "", errors.New("no versions available")
×
216
        }
×
217

218
        results := map[string]bool{}
×
219
        defaultResult := ""
×
220
        for _, v := range versions.GetResults() {
×
221
                if v.GetVersion() == "" {
×
222
                        continue
×
223
                }
224
                results[v.GetVersion()] = true
×
225
                if v.GetDefaultStatus() == "DEFAULT" {
×
226
                        defaultResult = v.GetVersion()
×
227
                }
×
228
        }
229

230
        keys := make([]string, 0, len(results))
×
231
        for k := range results {
×
232
                keys = append(keys, k)
×
233
        }
×
234

235
        slices.SortFunc(keys, func(a string, b string) int {
×
236
                return semver.Compare("v"+a, "v"+b)
×
237
        })
×
238

239
        return keys, defaultResult, nil
×
240
}
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