diff --git a/internal/service/advancedclustertpf/model_ClusterDescription20240805.go b/internal/service/advancedclustertpf/model_ClusterDescription20240805.go index 58a2a9d369..f77a7dfbc5 100644 --- a/internal/service/advancedclustertpf/model_ClusterDescription20240805.go +++ b/internal/service/advancedclustertpf/model_ClusterDescription20240805.go @@ -26,7 +26,6 @@ type ExtraAPIInfo struct { ContainerIDs map[string]string UsingLegacySchema bool AsymmetricShardUnsupported bool - OverrideUsingLegacySchema bool } func NewTFModel(ctx context.Context, input *admin.ClusterDescription20240805, timeout timeouts.Value, diags *diag.Diagnostics, apiInfo ExtraAPIInfo) *TFModel { diff --git a/internal/service/advancedclustertpf/resource_compatibility_reuse.go b/internal/service/advancedclustertpf/resource_compatibility_reuse.go index e3dd10df5e..2ad6877b2f 100644 --- a/internal/service/advancedclustertpf/resource_compatibility_reuse.go +++ b/internal/service/advancedclustertpf/resource_compatibility_reuse.go @@ -105,23 +105,13 @@ func getAdvancedClusterContainerID(containers []admin.CloudProviderContainer, cl return "" } -func getReplicationSpecIDsFromOldAPI(ctx context.Context, projectID, clusterName string, api admin20240530.ClustersApi) (zoneNameSpecIDs map[string]string, asymmetricShardUnsupported bool, err error) { - var clusterOldAPI *admin20240530.AdvancedClusterDescription - clusterOldAPI, _, err = api.GetCluster(ctx, projectID, clusterName).Execute() - if err != nil { - if apiError, ok := admin20240530.AsError(err); ok { - if apiError.GetErrorCode() == "ASYMMETRIC_SHARD_UNSUPPORTED" { - 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. - } - } - return nil, false, fmt.Errorf("error reading advanced cluster with 2023-02-01 API (%s): %s", clusterName, err) - } - specs := clusterOldAPI.GetReplicationSpecs() - zoneNameSpecIDs = make(map[string]string, len(specs)) +func replicationSpecIDsFromOldAPI(clusterRespOld *admin20240530.AdvancedClusterDescription) map[string]string { + specs := clusterRespOld.GetReplicationSpecs() + zoneNameSpecIDs := make(map[string]string, len(specs)) for _, spec := range specs { zoneNameSpecIDs[spec.GetZoneName()] = spec.GetId() } - return zoneNameSpecIDs, false, nil + return zoneNameSpecIDs } func convertHardwareSpecToOldSDK(hwspec *admin.HardwareSpec20240805) *admin20240530.HardwareSpec { diff --git a/internal/service/advancedclustertpf/resource_compatiblity.go b/internal/service/advancedclustertpf/resource_compatiblity.go index e320c37c6c..ba209c0494 100644 --- a/internal/service/advancedclustertpf/resource_compatiblity.go +++ b/internal/service/advancedclustertpf/resource_compatiblity.go @@ -2,12 +2,14 @@ package advancedclustertpf import ( "context" + "fmt" "reflect" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" + admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin" "go.mongodb.org/atlas-sdk/v20241113003/admin" ) @@ -40,10 +42,18 @@ func findNumShardsUpdates(ctx context.Context, state, plan *TFModel, diags *diag func resolveAPIInfo(ctx context.Context, diags *diag.Diagnostics, client *config.MongoDBClient, plan *TFModel, clusterLatest *admin.ClusterDescription20240805, overrideUsingLegacySchema bool) *ExtraAPIInfo { rootDiskSize := conversion.NilForUnknown(plan.DiskSizeGB, plan.DiskSizeGB.ValueFloat64Pointer()) projectID := plan.ProjectID.ValueString() - zoneNameSpecIDs, asymmetricShardUnsupported, err := getReplicationSpecIDsFromOldAPI(ctx, projectID, plan.Name.ValueString(), client.AtlasV220240530.ClustersApi) + clusterName := plan.Name.ValueString() + asymmetricShardUnsupported := false + + api20240530 := client.AtlasV220240530.ClustersApi + clusterRespOld, _, err := api20240530.GetCluster(ctx, projectID, clusterName).Execute() if err != nil { - diags.AddError("getReplicationSpecIDsFromOldAPI", err.Error()) - return nil + if admin20240530.IsErrorCode(err, "ASYMMETRIC_SHARD_UNSUPPORTED") { + asymmetricShardUnsupported = true + } else { + diags.AddError("errorRead", fmt.Sprintf("error reading advanced cluster with 2024-08-05 API (%s): %s", clusterName, err)) + return nil + } } if rootDiskSize == nil { rootDiskSize = findRegionRootDiskSize(clusterLatest.ReplicationSpecs) @@ -53,12 +63,23 @@ func resolveAPIInfo(ctx context.Context, diags *diag.Diagnostics, client *config diags.AddError("resolveContainerIDs failed", err.Error()) return nil } + var ( + legacySchema bool + zoneNameNumShards map[string]int64 + ) + if overrideUsingLegacySchema { + legacySchema = true + zoneNameNumShards = numShardsMapFromOldAPI(clusterRespOld) + } else { + legacySchema = usingLegacySchema(ctx, plan.ReplicationSpecs, diags) + zoneNameNumShards = numShardsMap(ctx, plan.ReplicationSpecs, diags) + } return &ExtraAPIInfo{ ContainerIDs: containerIDs, - UsingLegacySchema: usingLegacySchema(ctx, plan.ReplicationSpecs, diags), - ZoneNameNumShards: numShardsMap(ctx, plan.ReplicationSpecs, diags), + UsingLegacySchema: legacySchema, + ZoneNameNumShards: zoneNameNumShards, RootDiskSize: rootDiskSize, - ZoneNameReplicationSpecIDs: zoneNameSpecIDs, + ZoneNameReplicationSpecIDs: replicationSpecIDsFromOldAPI(clusterRespOld), AsymmetricShardUnsupported: asymmetricShardUnsupported, } } @@ -182,6 +203,15 @@ func numShardsMap(ctx context.Context, input types.List, diags *diag.Diagnostics return counts } +func numShardsMapFromOldAPI(clusterRespOld *admin20240530.AdvancedClusterDescription) map[string]int64 { + ret := make(map[string]int64) + for i := range clusterRespOld.GetReplicationSpecs() { + spec := &clusterRespOld.GetReplicationSpecs()[i] + ret[spec.GetZoneName()] = int64(spec.GetNumShards()) + } + return ret +} + func isNumShardsGreaterThanOne(counts []int64) bool { for _, count := range counts { if count > 1 {