Skip to content

Commit

Permalink
numShardsMapFromOldAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Dec 16, 2024
1 parent 4650006 commit a72969d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 36 additions & 6 deletions internal/service/advancedclustertpf/resource_compatiblity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a72969d

Please sign in to comment.