Skip to content

Commit 19c703f

Browse files
authored
refactor: Reuses advanced_cluster logic in SDKv2 & TPF initial single file common.go (#2947)
* refactor: Moves TestAccMockableAdvancedCluster_basicTenant to SDKv2 * test(unit): use IsTfLogDebug and support logging config in both REPLAY & CAPTURE * refactor: move TPF resource_tests to sdkv2 * refactor: move testdata for TestAccMockableAdvancedCluster_replicasetAdvConfigUpdate.yaml * refactor: move testdata for TestAccMockableAdvancedCluster_shardedBasic.yaml * refactor: move testdata for TestAccMockableAdvancedCluster_symmetricShardedOldSchemaDiskSizeGBAtElectableLevel.yaml * chore: log also query string in request matching and add QueryVars to mockConfig * chore: Support testmact in makefile and use it in github action * feat: add timeouts attribute support in advanced cluster schema conversion * refactor: consolidate data source definitions in advanced cluster tests * feat: add support for HTTP mock capture in testmact-capture target * refactor: move testdata for TestAccMockableAdvancedCluster_tenantUpgrade.yaml * refactor: move testdata for TestAccMockableAdvancedCluster_symmetricShardedOldSchema.yaml * test: ensure timeouts.create attribute is only checked on resource * fix: enforce explicit setting of ACCTEST_PACKAGES for testmact targets * chore: regenerate test files * chore: Add ORG_ID env var when using replay * fix: update minimum_enabled_tls_protocol to TLS1.2 and refactor timeout check in advanced cluster tests * test: Add more checks to new acceptance tests * fix: update advanced cluster test data sources to include new schema * chore: regen replicasetAdvConfigUpdate test * fix: update acceptance tests configuration to use correct package path and add regex for test selection * test: Refactor tests to use consistent TF config for data sources * fix: update advanced cluster test to correct timeout attribute and refactor update for TestAccMockableAdvancedCluster_replicasetAdvConfigUpdate * refactor: remove test cases functions for new tests * chore: use new schema for testmact if it is not set * chore: regenerate mock data * refactor: Moves AddIDsToReplicationSpecs to TPF and reuse FormatMongoDBMajorVersion * refactor: Replace getAdvancedClusterContainerID with advancedclustertpf.GetAdvancedClusterContainerID for improved code reuse * refactor: Reuse advanced configuration handling in createCluster function * refactor: Update acceptance tests runner to include common.go in advanced cluster filters * refactor: rename test function and make update compatible with SDKv2 * refactor: remove redundant VERSION assignments in GNUmakefile [ci skip] * refactor: remove redundant VERSION assignment in GNUmakefile * apply PR suggestion
1 parent c13ae3b commit 19c703f

File tree

9 files changed

+187
-218
lines changed

9 files changed

+187
-218
lines changed

.github/workflows/acceptance-tests-runner.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ jobs:
244244
filters: |
245245
advanced_cluster:
246246
- 'internal/service/advancedcluster/!(*_test).go' # matches any adv_cluster file change except test files
247+
- 'internal/service/advancedclustertpf/common.go'
247248
advanced_cluster_tpf:
248249
- 'internal/service/advancedclustertpf/*.go'
249250
- 'internal/service/advancedcluster/*_test.go'

GNUmakefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ test: fmtcheck
4242

4343
.PHONY: testmact
4444
testmact:
45-
@$(eval VERSION=macct)
4645
@$(eval ACCTEST_REGEX_RUN?=^TestAccMockable)
4746
@$(eval export HTTP_MOCKER_REPLAY?=true)
4847
@$(eval export HTTP_MOCKER_CAPTURE?=false)
@@ -57,7 +56,6 @@ testmact:
5756

5857
.PHONY: testmact-capture
5958
testmact-capture:
60-
@$(eval VERSION=macct)
6159
@$(eval export ACCTEST_REGEX_RUN?=^TestAccMockable)
6260
@$(eval export HTTP_MOCKER_REPLAY?=false)
6361
@$(eval export HTTP_MOCKER_CAPTURE?=true)
@@ -69,13 +67,11 @@ testmact-capture:
6967

7068
.PHONY: testacc
7169
testacc: fmtcheck
72-
@$(eval VERSION=acc)
7370
@$(eval ACCTEST_REGEX_RUN?=^TestAcc)
7471
TF_ACC=1 go test $(ACCTEST_PACKAGES) -run '$(ACCTEST_REGEX_RUN)' -v -parallel $(PARALLEL_GO_TEST) $(TESTARGS) -timeout $(ACCTEST_TIMEOUT) -ldflags="$(LINKER_FLAGS)"
7572

7673
.PHONY: testaccgov
7774
testaccgov: fmtcheck
78-
@$(eval VERSION=acc)
7975
TF_ACC=1 go test ./... -run 'TestAccProjectRSGovProject_CreateWithProjectOwner' -v -parallel 1 "$(TESTARGS) -timeout $(ACCTEST_TIMEOUT) -ldflags=$(LINKER_FLAGS) "
8076

8177
.PHONY: fmt

internal/service/advancedcluster/model_advanced_cluster.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
2323
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
24+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedclustertpf"
2425
)
2526

2627
const minVersionForChangeStreamOptions = 6.0
@@ -404,10 +405,7 @@ func ResourceClusterListAdvancedRefreshFunc(ctx context.Context, projectID strin
404405
}
405406

406407
func FormatMongoDBMajorVersion(val any) string {
407-
if strings.Contains(val.(string), ".") {
408-
return val.(string)
409-
}
410-
return fmt.Sprintf("%.1f", cast.ToFloat32(val))
408+
return advancedclustertpf.FormatMongoDBMajorVersion(val.(string))
411409
}
412410

413411
func flattenLabels(l []admin.ComponentLabel) []map[string]string {
@@ -711,7 +709,7 @@ func flattenAdvancedReplicationSpecRegionConfigs(ctx context.Context, apiObjects
711709
if err != nil {
712710
return nil, nil, err
713711
}
714-
if result := getAdvancedClusterContainerID(containers.GetResults(), &apiObject); result != "" {
712+
if result := advancedclustertpf.GetAdvancedClusterContainerID(containers.GetResults(), &apiObject); result != "" {
715713
// Will print as "providerName:regionName" = "containerId" in terraform show
716714
containerIDs[fmt.Sprintf("%s:%s", apiObject.GetProviderName(), apiObject.GetRegionName())] = result
717715
}
@@ -845,23 +843,6 @@ func flattenAdvancedReplicationSpecAutoScaling(apiObject *admin.AdvancedAutoScal
845843
return tfList
846844
}
847845

848-
func getAdvancedClusterContainerID(containers []admin.CloudProviderContainer, cluster *admin.CloudRegionConfig20240805) string {
849-
if len(containers) == 0 {
850-
return ""
851-
}
852-
for i := range containers {
853-
if cluster.GetProviderName() == constant.GCP {
854-
return containers[i].GetId()
855-
}
856-
if containers[i].GetProviderName() == cluster.GetProviderName() &&
857-
containers[i].GetRegion() == cluster.GetRegionName() || // For Azure
858-
containers[i].GetRegionName() == cluster.GetRegionName() { // For AWS
859-
return containers[i].GetId()
860-
}
861-
}
862-
return ""
863-
}
864-
865846
func expandProcessArgs(d *schema.ResourceData, p map[string]any, mongodbMajorVersion *string) (admin20240530.ClusterDescriptionProcessArgs, admin.ClusterDescriptionProcessArgs20240805) {
866847
res20240530 := admin20240530.ClusterDescriptionProcessArgs{}
867848
res := admin.ClusterDescriptionProcessArgs20240805{}

internal/service/advancedcluster/resource_update_logic.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
9+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedclustertpf"
910
"go.mongodb.org/atlas-sdk/v20241113004/admin"
1011
)
1112

@@ -31,27 +32,10 @@ func populateIDValuesUsingNewAPI(ctx context.Context, projectID, clusterName str
3132
}
3233

3334
zoneToReplicationSpecsIDs := groupIDsByZone(cluster.GetReplicationSpecs())
34-
result := AddIDsToReplicationSpecs(*replicationSpecs, zoneToReplicationSpecsIDs)
35+
result := advancedclustertpf.AddIDsToReplicationSpecs(*replicationSpecs, zoneToReplicationSpecsIDs)
3536
return &result, nil
3637
}
3738

38-
func AddIDsToReplicationSpecs(replicationSpecs []admin.ReplicationSpec20240805, zoneToReplicationSpecsIDs map[string][]string) []admin.ReplicationSpec20240805 {
39-
for zoneName, availableIDs := range zoneToReplicationSpecsIDs {
40-
var indexOfIDToUse = 0
41-
for i := range replicationSpecs {
42-
if indexOfIDToUse >= len(availableIDs) {
43-
break // all available ids for this zone have been used
44-
}
45-
if replicationSpecs[i].GetZoneName() == zoneName {
46-
newID := availableIDs[indexOfIDToUse]
47-
indexOfIDToUse++
48-
replicationSpecs[i].Id = &newID
49-
}
50-
}
51-
}
52-
return replicationSpecs
53-
}
54-
5539
func groupIDsByZone(specs []admin.ReplicationSpec20240805) map[string][]string {
5640
result := make(map[string][]string)
5741
for _, spec := range specs {

internal/service/advancedcluster/resource_update_logic_test.go

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -8,122 +8,6 @@ import (
88
"go.mongodb.org/atlas-sdk/v20241113004/admin"
99
)
1010

11-
func TestAddIDsToReplicationSpecs(t *testing.T) {
12-
testCases := map[string]struct {
13-
ReplicationSpecs []admin.ReplicationSpec20240805
14-
ZoneToReplicationSpecsIDs map[string][]string
15-
ExpectedReplicationSpecs []admin.ReplicationSpec20240805
16-
}{
17-
"two zones with same amount of available ids and replication specs to populate": {
18-
ReplicationSpecs: []admin.ReplicationSpec20240805{
19-
{
20-
ZoneName: admin.PtrString("Zone 1"),
21-
},
22-
{
23-
ZoneName: admin.PtrString("Zone 2"),
24-
},
25-
{
26-
ZoneName: admin.PtrString("Zone 1"),
27-
},
28-
{
29-
ZoneName: admin.PtrString("Zone 2"),
30-
},
31-
},
32-
ZoneToReplicationSpecsIDs: map[string][]string{
33-
"Zone 1": {"zone1-id1", "zone1-id2"},
34-
"Zone 2": {"zone2-id1", "zone2-id2"},
35-
},
36-
ExpectedReplicationSpecs: []admin.ReplicationSpec20240805{
37-
{
38-
ZoneName: admin.PtrString("Zone 1"),
39-
Id: admin.PtrString("zone1-id1"),
40-
},
41-
{
42-
ZoneName: admin.PtrString("Zone 2"),
43-
Id: admin.PtrString("zone2-id1"),
44-
},
45-
{
46-
ZoneName: admin.PtrString("Zone 1"),
47-
Id: admin.PtrString("zone1-id2"),
48-
},
49-
{
50-
ZoneName: admin.PtrString("Zone 2"),
51-
Id: admin.PtrString("zone2-id2"),
52-
},
53-
},
54-
},
55-
"less available ids than replication specs to populate": {
56-
ReplicationSpecs: []admin.ReplicationSpec20240805{
57-
{
58-
ZoneName: admin.PtrString("Zone 1"),
59-
},
60-
{
61-
ZoneName: admin.PtrString("Zone 1"),
62-
},
63-
{
64-
ZoneName: admin.PtrString("Zone 1"),
65-
},
66-
{
67-
ZoneName: admin.PtrString("Zone 2"),
68-
},
69-
},
70-
ZoneToReplicationSpecsIDs: map[string][]string{
71-
"Zone 1": {"zone1-id1"},
72-
"Zone 2": {"zone2-id1"},
73-
},
74-
ExpectedReplicationSpecs: []admin.ReplicationSpec20240805{
75-
{
76-
ZoneName: admin.PtrString("Zone 1"),
77-
Id: admin.PtrString("zone1-id1"),
78-
},
79-
{
80-
ZoneName: admin.PtrString("Zone 1"),
81-
Id: nil,
82-
},
83-
{
84-
ZoneName: admin.PtrString("Zone 1"),
85-
Id: nil,
86-
},
87-
{
88-
ZoneName: admin.PtrString("Zone 2"),
89-
Id: admin.PtrString("zone2-id1"),
90-
},
91-
},
92-
},
93-
"more available ids than replication specs to populate": {
94-
ReplicationSpecs: []admin.ReplicationSpec20240805{
95-
{
96-
ZoneName: admin.PtrString("Zone 1"),
97-
},
98-
{
99-
ZoneName: admin.PtrString("Zone 2"),
100-
},
101-
},
102-
ZoneToReplicationSpecsIDs: map[string][]string{
103-
"Zone 1": {"zone1-id1", "zone1-id2"},
104-
"Zone 2": {"zone2-id1", "zone2-id2"},
105-
},
106-
ExpectedReplicationSpecs: []admin.ReplicationSpec20240805{
107-
{
108-
ZoneName: admin.PtrString("Zone 1"),
109-
Id: admin.PtrString("zone1-id1"),
110-
},
111-
{
112-
ZoneName: admin.PtrString("Zone 2"),
113-
Id: admin.PtrString("zone2-id1"),
114-
},
115-
},
116-
},
117-
}
118-
119-
for name, tc := range testCases {
120-
t.Run(name, func(t *testing.T) {
121-
resultSpecs := advancedcluster.AddIDsToReplicationSpecs(tc.ReplicationSpecs, tc.ZoneToReplicationSpecsIDs)
122-
assert.Equal(t, tc.ExpectedReplicationSpecs, resultSpecs)
123-
})
124-
}
125-
}
126-
12711
func TestSyncAutoScalingConfigs(t *testing.T) {
12812
testCases := map[string]struct {
12913
ReplicationSpecs []admin.ReplicationSpec20240805
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package advancedclustertpf
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
8+
"github.com/spf13/cast"
9+
"go.mongodb.org/atlas-sdk/v20241113004/admin"
10+
)
11+
12+
func FormatMongoDBMajorVersion(version string) string {
13+
if strings.Contains(version, ".") {
14+
return version
15+
}
16+
return fmt.Sprintf("%.1f", cast.ToFloat32(version))
17+
}
18+
19+
func AddIDsToReplicationSpecs(replicationSpecs []admin.ReplicationSpec20240805, zoneToReplicationSpecsIDs map[string][]string) []admin.ReplicationSpec20240805 {
20+
for zoneName, availableIDs := range zoneToReplicationSpecsIDs {
21+
indexOfIDToUse := 0
22+
for i := range replicationSpecs {
23+
if indexOfIDToUse >= len(availableIDs) {
24+
break // all available ids for this zone have been used
25+
}
26+
if replicationSpecs[i].GetZoneName() == zoneName {
27+
newID := availableIDs[indexOfIDToUse]
28+
indexOfIDToUse++
29+
replicationSpecs[i].Id = &newID
30+
}
31+
}
32+
}
33+
return replicationSpecs
34+
}
35+
36+
func GetAdvancedClusterContainerID(containers []admin.CloudProviderContainer, cluster *admin.CloudRegionConfig20240805) string {
37+
for i, container := range containers {
38+
gpc := cluster.GetProviderName() == constant.GCP
39+
azure := container.GetProviderName() == cluster.GetProviderName() && container.GetRegion() == cluster.GetRegionName()
40+
aws := container.GetRegionName() == cluster.GetRegionName()
41+
if gpc || azure || aws {
42+
return containers[i].GetId()
43+
}
44+
}
45+
return ""
46+
}

0 commit comments

Comments
 (0)