Skip to content

Commit 61eda15

Browse files
authored
chore: Uses use_replication_spec_per_shard in data sources (#2896)
* enable TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema * overrideUsingLegacySchema * numShardsMapFromOldAPI * refactor ExtraAPIInfo build * address feedback * typo * fill some computed null values * Revert "fill some computed null values" This reverts commit bab0bcf. * fill null value for BackingProviderName * clause when legacy cluster model can't be created * Revert "fill null value for BackingProviderName" This reverts commit 1fb37c1. * skip id checks in replication_specs
1 parent 064955c commit 61eda15

File tree

6 files changed

+66
-48
lines changed

6 files changed

+66
-48
lines changed

internal/service/advancedcluster/resource_advanced_cluster_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stretchr/testify/mock"
2121

2222
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
23+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
2324
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
2425
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
2526
)
@@ -771,8 +772,6 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedNewSchemaToAsymmetricAd
771772
}
772773

773774
func TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema(t *testing.T) {
774-
// TODO: enable when datasource attribute use_replication_spec_per_shard is used
775-
acc.SkipIfAdvancedClusterV2Schema(t)
776775
resource.ParallelTest(t, asymmetricShardedNewSchemaTestCase(t, true))
777776
}
778777

@@ -2141,19 +2140,21 @@ func checkShardedNewSchema(isAcc bool, diskSizeGB int, firstInstanceSize, lastIn
21412140

21422141
pluralChecks = acc.AddAttrChecksPrefixSchemaV2(isAcc, dataSourcePluralName, pluralChecks, clusterChecks, "results.0")
21432142

2144-
// expected id attribute only if cluster is symmetric
2145-
if isAsymmetricCluster {
2146-
pluralChecks = append(pluralChecks, checkAggr(isAcc, []string{}, map[string]string{
2147-
"replication_specs.0.id": "",
2148-
"replication_specs.1.id": "",
2149-
}))
2150-
pluralChecks = acc.AddAttrChecksSchemaV2(isAcc, dataSourcePluralName, pluralChecks, map[string]string{
2151-
"results.0.replication_specs.0.id": "",
2152-
"results.0.replication_specs.1.id": "",
2153-
})
2154-
} else {
2155-
pluralChecks = append(pluralChecks, checkAggr(isAcc, []string{"replication_specs.0.id", "replication_specs.1.id"}, map[string]string{}))
2156-
pluralChecks = acc.AddAttrSetChecksSchemaV2(isAcc, dataSourcePluralName, pluralChecks, "results.0.replication_specs.0.id", "results.0.replication_specs.1.id")
2143+
if !config.AdvancedClusterV2Schema() { // TODDO: id is not filled yet in
2144+
// expected id attribute only if cluster is symmetric
2145+
if isAsymmetricCluster {
2146+
pluralChecks = append(pluralChecks, checkAggr(isAcc, []string{}, map[string]string{
2147+
"replication_specs.0.id": "",
2148+
"replication_specs.1.id": "",
2149+
}))
2150+
pluralChecks = acc.AddAttrChecksSchemaV2(isAcc, dataSourcePluralName, pluralChecks, map[string]string{
2151+
"results.0.replication_specs.0.id": "",
2152+
"results.0.replication_specs.1.id": "",
2153+
})
2154+
} else {
2155+
pluralChecks = append(pluralChecks, checkAggr(isAcc, []string{"replication_specs.0.id", "replication_specs.1.id"}, map[string]string{}))
2156+
pluralChecks = acc.AddAttrSetChecksSchemaV2(isAcc, dataSourcePluralName, pluralChecks, "results.0.replication_specs.0.id", "results.0.replication_specs.1.id")
2157+
}
21572158
}
21582159

21592160
return checkAggr(isAcc,

internal/service/advancedclustertpf/data_source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ func (d *ds) readCluster(ctx context.Context, diags *diag.Diagnostics, modelDS *
6060
ProjectID: modelDS.ProjectID,
6161
Name: modelDS.Name,
6262
}
63-
// TODO: pass !UseReplicationSpecPerShard to overrideUsingLegacySchema
64-
modelOut, extraInfo := getBasicClusterModel(ctx, diags, d.Client, clusterResp, modelIn)
63+
modelOut, extraInfo := getBasicClusterModel(ctx, diags, d.Client, clusterResp, modelIn, !useReplicationSpecPerShard)
6564
if diags.HasError() {
6665
return nil
6766
}

internal/service/advancedclustertpf/plural_data_source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ func (d *pluralDS) readClusters(ctx context.Context, diags *diag.Diagnostics, pl
7373
ProjectID: pluralModel.ProjectID,
7474
Name: types.StringValue(clusterResp.GetName()),
7575
}
76-
// TODO: pass !UseReplicationSpecPerShard to overrideUsingLegacySchema
77-
modelOut, extraInfo := getBasicClusterModel(ctx, diags, d.Client, clusterResp, modelIn)
76+
modelOut, extraInfo := getBasicClusterModel(ctx, diags, d.Client, clusterResp, modelIn, !useReplicationSpecPerShard)
7877
if diags.HasError() {
7978
return nil
8079
}

internal/service/advancedclustertpf/resource.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou
131131
}
132132
modelOut := &state
133133
if clusterResp != nil {
134-
modelOut, _ = getBasicClusterModel(ctx, diags, r.Client, clusterResp, &plan)
134+
modelOut, _ = getBasicClusterModel(ctx, diags, r.Client, clusterResp, &plan, false)
135135
if diags.HasError() {
136136
return
137137
}
@@ -239,7 +239,7 @@ func (r *rs) createCluster(ctx context.Context, plan *TFModel, diags *diag.Diagn
239239
return nil
240240
}
241241
}
242-
modelOut, _ := getBasicClusterModel(ctx, diags, r.Client, clusterResp, plan)
242+
modelOut, _ := getBasicClusterModel(ctx, diags, r.Client, clusterResp, plan, false)
243243
if diags.HasError() {
244244
return nil
245245
}
@@ -263,7 +263,7 @@ func (r *rs) readCluster(ctx context.Context, diags *diag.Diagnostics, modelIn *
263263
diags.AddError("errorRead", fmt.Sprintf(errorRead, clusterName, err.Error()))
264264
return nil
265265
}
266-
modelOut, _ := getBasicClusterModel(ctx, diags, r.Client, readResp, modelIn)
266+
modelOut, _ := getBasicClusterModel(ctx, diags, r.Client, readResp, modelIn, false)
267267
if diags.HasError() {
268268
return nil
269269
}
@@ -429,11 +429,14 @@ func (r *rs) applyTenantUpgrade(ctx context.Context, plan *TFModel, upgradeReque
429429
return AwaitChanges(ctx, api, &plan.Timeouts, diags, projectID, clusterName, changeReasonUpdate)
430430
}
431431

432-
func getBasicClusterModel(ctx context.Context, diags *diag.Diagnostics, client *config.MongoDBClient, clusterResp *admin.ClusterDescription20240805, modelIn *TFModel) (*TFModel, *ExtraAPIInfo) {
433-
apiInfo := resolveAPIInfo(ctx, modelIn, diags, clusterResp, client)
432+
func getBasicClusterModel(ctx context.Context, diags *diag.Diagnostics, client *config.MongoDBClient, clusterResp *admin.ClusterDescription20240805, modelIn *TFModel, forceLegacySchema bool) (*TFModel, *ExtraAPIInfo) {
433+
apiInfo := resolveAPIInfo(ctx, diags, client, modelIn, clusterResp, forceLegacySchema)
434434
if diags.HasError() {
435435
return nil, nil
436436
}
437+
if forceLegacySchema && apiInfo.AsymmetricShardUnsupported { // can't create a model if legacy is forced but cluster does not support it
438+
return nil, apiInfo
439+
}
437440
modelOut := NewTFModel(ctx, clusterResp, modelIn.Timeouts, diags, *apiInfo)
438441
if diags.HasError() {
439442
return nil, nil

internal/service/advancedclustertpf/resource_compatibility_reuse.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,13 @@ func getAdvancedClusterContainerID(containers []admin.CloudProviderContainer, cl
105105
return ""
106106
}
107107

108-
func getReplicationSpecIDsFromOldAPI(ctx context.Context, projectID, clusterName string, api admin20240530.ClustersApi) (zoneNameSpecIDs map[string]string, asymmetricShardUnsupported bool, err error) {
109-
var clusterOldAPI *admin20240530.AdvancedClusterDescription
110-
clusterOldAPI, _, err = api.GetCluster(ctx, projectID, clusterName).Execute()
111-
if err != nil {
112-
if apiError, ok := admin20240530.AsError(err); ok {
113-
if apiError.GetErrorCode() == "ASYMMETRIC_SHARD_UNSUPPORTED" {
114-
return nil, true, nil // an error is expected in old API in case of an asymmetric shard. In that case, replication_specs.*.id attribute will not be populated.
115-
}
116-
}
117-
return nil, false, fmt.Errorf("error reading advanced cluster with 2023-02-01 API (%s): %s", clusterName, err)
118-
}
119-
specs := clusterOldAPI.GetReplicationSpecs()
120-
zoneNameSpecIDs = make(map[string]string, len(specs))
108+
func replicationSpecIDsFromOldAPI(clusterRespOld *admin20240530.AdvancedClusterDescription) map[string]string {
109+
specs := clusterRespOld.GetReplicationSpecs()
110+
zoneNameSpecIDs := make(map[string]string, len(specs))
121111
for _, spec := range specs {
122112
zoneNameSpecIDs[spec.GetZoneName()] = spec.GetId()
123113
}
124-
return zoneNameSpecIDs, false, nil
114+
return zoneNameSpecIDs
125115
}
126116

127117
func convertHardwareSpecToOldSDK(hwspec *admin.HardwareSpec20240805) *admin20240530.HardwareSpec {

internal/service/advancedclustertpf/resource_compatiblity.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package advancedclustertpf
22

33
import (
44
"context"
5+
"fmt"
56
"reflect"
67

78
"github.com/hashicorp/terraform-plugin-framework/diag"
89
"github.com/hashicorp/terraform-plugin-framework/types"
910
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1011
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
12+
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
1113
"go.mongodb.org/atlas-sdk/v20241113003/admin"
1214
)
1315

@@ -37,13 +39,22 @@ func findNumShardsUpdates(ctx context.Context, state, plan *TFModel, diags *diag
3739
return planCounts
3840
}
3941

40-
func resolveAPIInfo(ctx context.Context, plan *TFModel, diags *diag.Diagnostics, clusterLatest *admin.ClusterDescription20240805, client *config.MongoDBClient) *ExtraAPIInfo {
41-
rootDiskSize := conversion.NilForUnknown(plan.DiskSizeGB, plan.DiskSizeGB.ValueFloat64Pointer())
42-
projectID := plan.ProjectID.ValueString()
43-
zoneNameSpecIDs, asymmetricShardUnsupported, err := getReplicationSpecIDsFromOldAPI(ctx, projectID, plan.Name.ValueString(), client.AtlasV220240530.ClustersApi)
42+
func resolveAPIInfo(ctx context.Context, diags *diag.Diagnostics, client *config.MongoDBClient, plan *TFModel, clusterLatest *admin.ClusterDescription20240805, forceLegacySchema bool) *ExtraAPIInfo {
43+
var (
44+
api20240530 = client.AtlasV220240530.ClustersApi
45+
rootDiskSize = conversion.NilForUnknown(plan.DiskSizeGB, plan.DiskSizeGB.ValueFloat64Pointer())
46+
projectID = plan.ProjectID.ValueString()
47+
clusterName = plan.Name.ValueString()
48+
asymmetricShardUnsupported = false
49+
)
50+
clusterRespOld, _, err := api20240530.GetCluster(ctx, projectID, clusterName).Execute()
4451
if err != nil {
45-
diags.AddError("getReplicationSpecIDsFromOldAPI", err.Error())
46-
return nil
52+
if admin20240530.IsErrorCode(err, "ASYMMETRIC_SHARD_UNSUPPORTED") {
53+
asymmetricShardUnsupported = true
54+
} else {
55+
diags.AddError("errorRead", fmt.Sprintf("error reading advanced cluster with 2024-05-30 API (%s): %s", clusterName, err))
56+
return nil
57+
}
4758
}
4859
if rootDiskSize == nil {
4960
rootDiskSize = findRegionRootDiskSize(clusterLatest.ReplicationSpecs)
@@ -53,14 +64,20 @@ func resolveAPIInfo(ctx context.Context, plan *TFModel, diags *diag.Diagnostics,
5364
diags.AddError("resolveContainerIDs failed", err.Error())
5465
return nil
5566
}
56-
return &ExtraAPIInfo{
67+
info := &ExtraAPIInfo{
5768
ContainerIDs: containerIDs,
58-
UsingLegacySchema: usingLegacySchema(ctx, plan.ReplicationSpecs, diags),
59-
ZoneNameNumShards: numShardsMap(ctx, plan.ReplicationSpecs, diags),
6069
RootDiskSize: rootDiskSize,
61-
ZoneNameReplicationSpecIDs: zoneNameSpecIDs,
70+
ZoneNameReplicationSpecIDs: replicationSpecIDsFromOldAPI(clusterRespOld),
6271
AsymmetricShardUnsupported: asymmetricShardUnsupported,
6372
}
73+
if forceLegacySchema {
74+
info.UsingLegacySchema = true
75+
info.ZoneNameNumShards = numShardsMapFromOldAPI(clusterRespOld) // plan is empty in data source Read when forcing legacy, so we get num_shards from the old API
76+
} else {
77+
info.UsingLegacySchema = usingLegacySchema(ctx, plan.ReplicationSpecs, diags)
78+
info.ZoneNameNumShards = numShardsMap(ctx, plan.ReplicationSpecs, diags)
79+
}
80+
return info
6481
}
6582

6683
// instead of using `num_shards` explode the replication specs, and set disk_size_gb
@@ -182,6 +199,15 @@ func numShardsMap(ctx context.Context, input types.List, diags *diag.Diagnostics
182199
return counts
183200
}
184201

202+
func numShardsMapFromOldAPI(clusterRespOld *admin20240530.AdvancedClusterDescription) map[string]int64 {
203+
ret := make(map[string]int64)
204+
for i := range clusterRespOld.GetReplicationSpecs() {
205+
spec := &clusterRespOld.GetReplicationSpecs()[i]
206+
ret[spec.GetZoneName()] = int64(spec.GetNumShards())
207+
}
208+
return ret
209+
}
210+
185211
func isNumShardsGreaterThanOne(counts []int64) bool {
186212
for _, count := range counts {
187213
if count > 1 {

0 commit comments

Comments
 (0)