Skip to content

Commit fedfa1f

Browse files
author
Anton
authored
CLOUDP-80774: support enums in the spec (#97)
1 parent 5dd493f commit fedfa1f

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

pkg/api/v1/atlascluster_types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ func init() {
3030
SchemeBuilder.Register(&AtlasCluster{}, &AtlasClusterList{})
3131
}
3232

33+
type ProviderName string
34+
type ClusterType string
35+
36+
const (
37+
ProviderAWS ProviderName = "AWS"
38+
ProviderGCP ProviderName = "GCP"
39+
ProviderAzure ProviderName = "AZURE"
40+
ProviderTenant ProviderName = "TENANT"
41+
)
42+
43+
const (
44+
TypeReplicaSet ClusterType = "REPLICASET"
45+
TypeSharded ClusterType = "SHARDED"
46+
TypeGeoSharded ClusterType = "GEOSHARDED"
47+
)
48+
3349
// AtlasClusterSpec defines the desired state of AtlasCluster
3450
type AtlasClusterSpec struct {
3551
// Project is a reference to AtlasProject resource the cluster belongs to
@@ -49,7 +65,7 @@ type AtlasClusterSpec struct {
4965
// The parameter is required if replicationSpecs are set or if Global Clusters are deployed.
5066
// +kubebuilder:validation:Enum=REPLICASET;SHARDED;GEOSHARDED
5167
// +optional
52-
ClusterType string `json:"clusterType,omitempty"`
68+
ClusterType ClusterType `json:"clusterType,omitempty"`
5369

5470
// Capacity, in gigabytes, of the host's root volume.
5571
// Increase this number to add capacity, up to a maximum possible value of 4096 (i.e., 4 TB).
@@ -179,7 +195,7 @@ type ProviderSettingsSpec struct {
179195

180196
// Cloud service provider on which Atlas provisions the hosts.
181197
// +kubebuilder:validation:Enum=AWS;GCP;AZURE;TENANT
182-
ProviderName string `json:"providerName"`
198+
ProviderName ProviderName `json:"providerName"`
183199

184200
// Physical location of your MongoDB cluster.
185201
// The region you choose can affect network latency for clients accessing your databases.

pkg/api/v1/atlascluster_types_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ func TestCompatibility(t *testing.T) {
4040
compareStruct(AtlasClusterSpec{}, mongodbatlas.Cluster{}, t)
4141
}
4242

43+
// TestEnums verifies that replacing the strings with "enum" in Atlas Operator works correctly and is (de)serialized
44+
// into the correct Atlas Cluster
45+
func TestEnums(t *testing.T) {
46+
atlasCluster := mongodbatlas.Cluster{
47+
ProviderSettings: &mongodbatlas.ProviderSettings{
48+
ProviderName: "AWS",
49+
},
50+
ClusterType: "GEOSHARDED",
51+
}
52+
operatorCluster := AtlasClusterSpec{
53+
ProviderSettings: &ProviderSettingsSpec{
54+
ProviderName: ProviderAWS,
55+
},
56+
ClusterType: TypeGeoSharded,
57+
}
58+
transformedCluster, err := operatorCluster.Cluster()
59+
assert.NoError(t, err)
60+
assert.Equal(t, atlasCluster, *transformedCluster)
61+
}
62+
4363
func compareStruct(ours interface{}, their interface{}, t *testing.T) {
4464
ourFields := getAllFieldsSorted(ours, excludedClusterFieldsOurs)
4565
theirFields := getAllFieldsSorted(their, excludedClusterFieldsTheirs)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package atlascluster
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"go.mongodb.org/atlas/mongodbatlas"
8+
"go.uber.org/zap"
9+
10+
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
11+
)
12+
13+
func TestClusterMatchesSpec(t *testing.T) {
14+
t.Run("Clusters match (enums)", func(t *testing.T) {
15+
atlasClusterEnum := mongodbatlas.Cluster{
16+
ProviderSettings: &mongodbatlas.ProviderSettings{
17+
ProviderName: "AWS",
18+
},
19+
ClusterType: "GEOSHARDED",
20+
}
21+
operatorClusterEnum := mdbv1.AtlasClusterSpec{
22+
ProviderSettings: &mdbv1.ProviderSettingsSpec{
23+
ProviderName: mdbv1.ProviderAWS,
24+
},
25+
ClusterType: mdbv1.TypeGeoSharded,
26+
}
27+
28+
match, err := clusterMatchesSpec(zap.S(), &atlasClusterEnum, operatorClusterEnum)
29+
assert.NoError(t, err)
30+
assert.True(t, match)
31+
})
32+
t.Run("Clusters don't match (enums)", func(t *testing.T) {
33+
atlasClusterEnum := mongodbatlas.Cluster{ClusterType: "GEOSHARDED"}
34+
operatorClusterEnum := mdbv1.AtlasClusterSpec{ClusterType: mdbv1.TypeReplicaSet}
35+
36+
match, err := clusterMatchesSpec(zap.S(), &atlasClusterEnum, operatorClusterEnum)
37+
assert.NoError(t, err)
38+
assert.False(t, match)
39+
})
40+
}

test/e2e/configuration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var _ = Describe("Deploy simple cluster", func() {
109109
).Should(Equal(userClusterConfig.Spec.ProviderSettings.InstanceSizeName))
110110
Expect(
111111
cluster.ProviderSettings.ProviderName,
112-
).Should(Equal(userClusterConfig.Spec.ProviderSettings.ProviderName))
112+
).Should(Equal(string(userClusterConfig.Spec.ProviderSettings.ProviderName)))
113113
Expect(
114114
cluster.ProviderSettings.RegionName,
115115
).Should(Equal(userClusterConfig.Spec.ProviderSettings.RegionName))

test/int/cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func testAtlasCluster(namespace, name, projectName string) *mdbv1.AtlasCluster {
262262
Project: mdbv1.ResourceRef{Name: projectName},
263263
ProviderSettings: &mdbv1.ProviderSettingsSpec{
264264
InstanceSizeName: "M10",
265-
ProviderName: "GCP",
265+
ProviderName: mdbv1.ProviderGCP,
266266
RegionName: "EASTERN_US",
267267
},
268268
},

0 commit comments

Comments
 (0)