-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Uses use_replication_spec_per_shard
in data sources
#2896
Changes from 3 commits
5d80ddc
4650006
a72969d
e9e73d0
d39cf2f
c0f4a9e
bab0bcf
97a74c3
75b5c25
1fb37c1
b0143be
c7b599b
e8f848e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -37,13 +39,21 @@ func findNumShardsUpdates(ctx context.Context, state, plan *TFModel, diags *diag | |
return planCounts | ||
} | ||
|
||
func resolveAPIInfo(ctx context.Context, plan *TFModel, diags *diag.Diagnostics, clusterLatest *admin.ClusterDescription20240805, client *config.MongoDBClient) *ExtraAPIInfo { | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice refactor! |
||
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, plan *TFModel, diags *diag.Diagnostics, | |
diags.AddError("resolveContainerIDs failed", err.Error()) | ||
return nil | ||
} | ||
var ( | ||
legacySchema bool | ||
zoneNameNumShards map[string]int64 | ||
) | ||
if overrideUsingLegacySchema { | ||
legacySchema = true | ||
zoneNameNumShards = numShardsMapFromOldAPI(clusterRespOld) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plan is empty in Read when overriding legacy, so we get it from the old API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this comment to the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added in: d39cf2f |
||
} 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 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit]
forceLegacySchema
less verbose?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed in: d39cf2f