Skip to content

Commit 87a5b77

Browse files
authored
CLOUDP-87553: Add infrastructure labels to clusters created via quickstart (#667)
1 parent 6d7012b commit 87a5b77

File tree

5 files changed

+130
-21
lines changed

5 files changed

+130
-21
lines changed

internal/cli/atlas/clusters/clusters.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/mongodb/mongocli/internal/cli/atlas/onlinearchive"
2222
"github.com/mongodb/mongocli/internal/cli/atlas/search"
2323
"github.com/spf13/cobra"
24+
atlas "go.mongodb.org/atlas/mongodbatlas"
2425
)
2526

2627
func Builder() *cobra.Command {
@@ -49,3 +50,20 @@ func Builder() *cobra.Command {
4950

5051
return cmd
5152
}
53+
54+
func AddLabel(out *atlas.Cluster, l atlas.Label) {
55+
if LabelExists(out.Labels, l) {
56+
return
57+
}
58+
59+
out.Labels = append(out.Labels, l)
60+
}
61+
62+
func LabelExists(labels []atlas.Label, l atlas.Label) bool {
63+
for _, v := range labels {
64+
if v.Key == l.Key && v.Value == l.Value {
65+
return true
66+
}
67+
}
68+
return false
69+
}

internal/cli/atlas/clusters/clusters_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/mongodb/mongocli/internal/test"
23+
"go.mongodb.org/atlas/mongodbatlas"
2324
)
2425

2526
func TestBuilder(t *testing.T) {
@@ -30,3 +31,97 @@ func TestBuilder(t *testing.T) {
3031
[]string{},
3132
)
3233
}
34+
35+
func TestAddLabel(t *testing.T) {
36+
type args struct {
37+
out *mongodbatlas.Cluster
38+
label mongodbatlas.Label
39+
}
40+
tests := []struct {
41+
name string
42+
args args
43+
wantsAdd bool
44+
}{
45+
{
46+
name: "adds",
47+
args: args{
48+
out: &mongodbatlas.Cluster{
49+
Labels: []mongodbatlas.Label{},
50+
},
51+
label: mongodbatlas.Label{Key: "test", Value: "test"},
52+
},
53+
wantsAdd: true,
54+
},
55+
{
56+
name: "doesn't adds",
57+
args: args{
58+
out: &mongodbatlas.Cluster{
59+
Labels: []mongodbatlas.Label{{Key: "test", Value: "test"}},
60+
},
61+
label: mongodbatlas.Label{Key: "test", Value: "test"},
62+
},
63+
wantsAdd: true,
64+
},
65+
}
66+
for _, tt := range tests {
67+
name := tt.name
68+
args := tt.args
69+
wantsAdd := tt.wantsAdd
70+
t.Run(name, func(t *testing.T) {
71+
AddLabel(args.out, args.label)
72+
if exists := LabelExists(args.out.Labels, args.label); exists != wantsAdd {
73+
t.Errorf("wants to add %v, got %v\n", wantsAdd, exists)
74+
}
75+
})
76+
}
77+
}
78+
79+
func TestLabelExists(t *testing.T) {
80+
type args struct {
81+
labels []mongodbatlas.Label
82+
l mongodbatlas.Label
83+
}
84+
tests := []struct {
85+
name string
86+
args args
87+
want bool
88+
}{
89+
{
90+
name: "label doesn't exist",
91+
args: args{
92+
labels: []mongodbatlas.Label{},
93+
l: mongodbatlas.Label{
94+
Key: "test",
95+
Value: "test",
96+
},
97+
},
98+
want: false,
99+
},
100+
{
101+
name: "label exist",
102+
args: args{
103+
labels: []mongodbatlas.Label{
104+
{
105+
Key: "test",
106+
Value: "test",
107+
},
108+
},
109+
l: mongodbatlas.Label{
110+
Key: "test",
111+
Value: "test",
112+
},
113+
},
114+
want: true,
115+
},
116+
}
117+
for _, tt := range tests {
118+
name := tt.name
119+
args := tt.args
120+
want := tt.want
121+
t.Run(name, func(t *testing.T) {
122+
if got := LabelExists(args.labels, args.l); got != want {
123+
t.Errorf("LabelExists() = %v, want %v", got, want)
124+
}
125+
})
126+
}
127+
}

internal/cli/atlas/clusters/create.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,15 @@ func (opts *CreateOpts) newCluster() (*atlas.Cluster, error) {
104104
cluster.Name = opts.name
105105
}
106106

107-
updateLabels(cluster)
107+
AddLabel(cluster, atlas.Label{
108+
Key: labelKey,
109+
Value: labelValue,
110+
})
108111

109112
cluster.GroupID = opts.ConfigProjectID()
110113
return cluster, nil
111114
}
112115

113-
func updateLabels(out *atlas.Cluster) {
114-
found := false
115-
for _, v := range out.Labels {
116-
if v.Key == labelKey && v.Value == labelValue {
117-
found = true
118-
break
119-
}
120-
}
121-
122-
if !found {
123-
out.Labels = append(out.Labels, atlas.Label{
124-
Key: labelKey,
125-
Value: labelValue,
126-
})
127-
}
128-
}
129-
130116
func (opts *CreateOpts) applyOpts(out *atlas.Cluster) {
131117
replicationSpec := opts.newReplicationSpec()
132118
if opts.backup {

internal/cli/atlas/clusters/update.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ func (opts *UpdateOpts) patchOpts(out *atlas.Cluster) {
101101
if opts.tier != "" {
102102
out.ProviderSettings.InstanceSizeName = opts.tier
103103
}
104-
105-
updateLabels(out)
104+
AddLabel(out, atlas.Label{
105+
Key: labelKey,
106+
Value: labelValue,
107+
})
106108
}
107109

108110
// mongocli atlas cluster(s) update [name] --projectId projectId [--tier M#] [--diskSizeGB N] [--mdbVersion]

internal/cli/atlas/quickstart/quick_start.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,23 @@ func (opts *Opts) newProjectIPAccessList() []*atlas.ProjectIPAccessList {
260260

261261
func (opts *Opts) newCluster() *atlas.Cluster {
262262
diskSizeGB := atlas.DefaultDiskSizeGB[strings.ToUpper(tenant)][tier]
263-
return &atlas.Cluster{
263+
cluster := &atlas.Cluster{
264264
GroupID: opts.ConfigProjectID(),
265265
ClusterType: replicaSet,
266266
ReplicationSpecs: []atlas.ReplicationSpec{opts.newReplicationSpec()},
267267
ProviderSettings: opts.newProviderSettings(),
268268
MongoDBMajorVersion: mdbVersion,
269269
DiskSizeGB: &diskSizeGB,
270270
Name: opts.ClusterName,
271+
Labels: []atlas.Label{
272+
{
273+
Key: "Infrastructure Tool",
274+
Value: "MongoDB CLI Quickstart",
275+
},
276+
},
271277
}
278+
279+
return cluster
272280
}
273281

274282
func (opts *Opts) newReplicationSpec() atlas.ReplicationSpec {

0 commit comments

Comments
 (0)