Skip to content
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

fix: Sets correct zone_id when use_replication_spec_per_shard is false and refactors replica_set_scaling_strategy handling with old schema of advanced cluster #2568

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2568.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
data-source/mongodbatlas_advanced_clusters: Sets correct `zone_id` when `use_replication_spec_per_shard` is false
```
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,15 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
if err := d.Set("disk_size_gb", clusterDescOld.GetDiskSizeGB()); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err))
}
clusterDescNew, _, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
}
if err := d.Set("replica_set_scaling_strategy", clusterDescNew.GetReplicaSetScalingStrategy()); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replica_set_scaling_strategy", clusterName, err))
}

zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(ctx, projectID, clusterName, connV2)
zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(clusterDescNew)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,16 @@ func flattenAdvancedClustersOldSDK(ctx context.Context, connV20240530 *admin2024
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
}

zoneNameToOldReplicationSpecIDs, err := getReplicationSpecIDsFromOldAPI(ctx, cluster.GetGroupId(), cluster.GetName(), connV20240530)
clusterDescNew, _, err := connV2.ClustersApi.GetCluster(ctx, cluster.GetGroupId(), cluster.GetName()).Execute()
if err != nil {
return nil, diag.FromErr(err)
}
zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(clusterDescNew)
if err != nil {
return nil, diag.FromErr(err)
}

replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), zoneNameToOldReplicationSpecIDs, cluster.GetDiskSizeGB(), nil, d, connV2)
replicationSpecs, err := FlattenAdvancedReplicationSpecsOldSDK(ctx, cluster.GetReplicationSpecs(), zoneNameToZoneIDs, cluster.GetDiskSizeGB(), nil, d, connV2)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
}
Expand All @@ -405,6 +409,7 @@ func flattenAdvancedClustersOldSDK(ctx context.Context, connV20240530 *admin2024
"termination_protection_enabled": cluster.GetTerminationProtectionEnabled(),
"version_release_system": cluster.GetVersionReleaseSystem(),
"global_cluster_self_managed_sharding": cluster.GetGlobalClusterSelfManagedSharding(),
"replica_set_scaling_strategy": clusterDescNew.GetReplicaSetScalingStrategy(),
}
results = append(results, result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replica_set_scaling_strategy", clusterName, err))
}

zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(ctx, projectID, clusterName, connV2)
zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(cluster)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -630,11 +630,7 @@ func getReplicationSpecIDsFromOldAPI(ctx context.Context, projectID, clusterName
}

// getZoneIDsFromNewAPI returns the zone id values of replication specs coming from new API. This is used to populate zone_id when old API is called in the read.
func getZoneIDsFromNewAPI(ctx context.Context, projectID, clusterName string, connV2 *admin.APIClient) (map[string]string, error) {
cluster, _, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
return nil, fmt.Errorf("error reading advanced cluster for fetching zone ids (%s): %s", clusterName, err)
}
func getZoneIDsFromNewAPI(cluster *admin.ClusterDescription20240805) (map[string]string, error) {
specs := cluster.GetReplicationSpecs()
result := make(map[string]string, len(specs))
for _, spec := range specs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2057,12 +2057,10 @@ func configReplicaSetScalingStrategyOldSchema(orgID, projectName, name, replicaS
data "mongodbatlas_advanced_cluster" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
name = mongodbatlas_advanced_cluster.test.name
use_replication_spec_per_shard = true
lantoli marked this conversation as resolved.
Show resolved Hide resolved
}

data "mongodbatlas_advanced_clusters" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
use_replication_spec_per_shard = true
}
`, orgID, projectName, name, replicaSetScalingStrategy)
}
Expand Down