Skip to content

Commit

Permalink
refactor read operation of data sources to be like the resource read …
Browse files Browse the repository at this point in the history
…operation
  • Loading branch information
oarbusi committed Dec 4, 2024
1 parent 4a42192 commit fcca7cd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 579 deletions.
96 changes: 18 additions & 78 deletions internal/service/advancedcluster/data_source_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net/http"

admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -272,95 +270,37 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
clusterName := d.Get("name").(string)
useReplicationSpecPerShard := false
var replicationSpecs []map[string]any
var clusterID string

if v, ok := d.GetOk("use_replication_spec_per_shard"); ok {
useReplicationSpecPerShard = v.(bool)
}

if !useReplicationSpecPerShard {
clusterDescOld, resp, err := connV220240530.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil {
if resp.StatusCode == http.StatusNotFound {
return nil
}
if admin20240530.IsErrorCode(err, "ASYMMETRIC_SHARD_UNSUPPORTED") {
return diag.FromErr(fmt.Errorf("please add `use_replication_spec_per_shard = true` to your data source configuration to enable asymmetric shard support. Refer to documentation for more details. %s", err))
}
}
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
}

clusterID = clusterDescOld.GetId()

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))
}
if err := d.Set("redact_client_log_data", clusterDescNew.GetRedactClientLogData()); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "redact_client_log_data", clusterName, err))
}

zoneNameToZoneIDs, err := getZoneIDsFromNewAPI(clusterDescNew)
if err != nil {
return diag.FromErr(err)
clusterDesc, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
}
zoneNameToOldReplicationSpecMeta, err := GetReplicationSpecAttributesFromOldAPI(ctx, projectID, clusterName, connV220240530.ClustersApi)
if err != nil {
return diag.FromErr(err)
}
diags := setRootFields(d, clusterDesc, false)
if diags.HasError() {
return diags
}

replicationSpecs, err = FlattenAdvancedReplicationSpecsOldSDK(ctx, clusterDescOld.GetReplicationSpecs(), zoneNameToZoneIDs, clusterDescOld.GetDiskSizeGB(), d.Get("replication_specs").([]any), d, connV2)
if !useReplicationSpecPerShard {
replicationSpecs, err = FlattenAdvancedReplicationSpecsOldShardingConfig(ctx, clusterDesc.GetReplicationSpecs(), zoneNameToOldReplicationSpecMeta, d.Get("replication_specs").([]any), d, connV2)
if err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err))
}

clusterDesc := convertClusterDescToLatestExcludeRepSpecs(clusterDescOld)
clusterDesc.ConfigServerManagementMode = clusterDescNew.ConfigServerManagementMode
clusterDesc.ConfigServerType = clusterDescNew.ConfigServerType
diags := setRootFields(d, clusterDesc, false)
if diags.HasError() {
return diags
}
} else {
clusterDescLatest, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
}

clusterID = clusterDescLatest.GetId()

// root disk_size_gb defined for backwards compatibility avoiding breaking changes
if err := d.Set("disk_size_gb", GetDiskSizeGBFromReplicationSpec(clusterDescLatest)); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "disk_size_gb", clusterName, err))
}
if err := d.Set("replica_set_scaling_strategy", clusterDescLatest.GetReplicaSetScalingStrategy()); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replica_set_scaling_strategy", clusterName, err))
}
if err := d.Set("redact_client_log_data", clusterDescLatest.GetRedactClientLogData()); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "redact_client_log_data", clusterName, err))
}

zoneNameToOldReplicationSpecIDs, err := getReplicationSpecIDsFromOldAPI(ctx, projectID, clusterName, connV220240530)
if err != nil {
return diag.FromErr(err)
}

replicationSpecs, err = flattenAdvancedReplicationSpecsDS(ctx, clusterDescLatest.GetReplicationSpecs(), zoneNameToOldReplicationSpecIDs, d, connV2)
replicationSpecs, err = flattenAdvancedReplicationSpecsDS(ctx, clusterDesc.GetReplicationSpecs(), zoneNameToOldReplicationSpecMeta, d, connV2)
if err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err))
}

diags := setRootFields(d, clusterDescLatest, false)
if diags.HasError() {
return diags
}
}

if err := d.Set("replication_specs", replicationSpecs); err != nil {
Expand All @@ -380,6 +320,6 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "advanced_configuration", clusterName, err))
}

d.SetId(clusterID)
d.SetId(clusterDesc.GetId())
return nil
}
120 changes: 27 additions & 93 deletions internal/service/advancedcluster/data_source_advanced_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,41 +292,25 @@ func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any)
useReplicationSpecPerShard = v.(bool)
}

if !useReplicationSpecPerShard {
list, resp, err := connV220240530.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf(errorListRead, projectID, err))
}
results, diags := flattenAdvancedClustersOldSDK(ctx, connV220240530, connV2, list.GetResults(), d)
if len(diags) > 0 {
return diags
}
if err := d.Set("results", results); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "results", d.Id(), err))
}
} else {
list, resp, err := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf(errorListRead, projectID, err))
}
results, diags := flattenAdvancedClusters(ctx, connV220240530, connV2, list.GetResults(), d)
if len(diags) > 0 {
return diags
}
if err := d.Set("results", results); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "results", d.Id(), err))
list, resp, err := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil
}
return diag.FromErr(fmt.Errorf(errorListRead, projectID, err))
}
results, diags := flattenAdvancedClusters(ctx, connV220240530, connV2, list.GetResults(), d, useReplicationSpecPerShard)
if len(diags) > 0 {
return diags
}
if err := d.Set("results", results); err != nil {
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "results", d.Id(), err))
}

return nil
}

func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.APIClient, connV2 *admin.APIClient, clusters []admin.ClusterDescription20240805, d *schema.ResourceData) ([]map[string]any, diag.Diagnostics) {
func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.APIClient, connV2 *admin.APIClient, clusters []admin.ClusterDescription20240805, d *schema.ResourceData, useReplicationSpecPerShard bool) ([]map[string]any, diag.Diagnostics) {
results := make([]map[string]any, 0, len(clusters))
for i := range clusters {
cluster := &clusters[i]
Expand All @@ -339,14 +323,23 @@ func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
}

zoneNameToOldReplicationSpecIDs, err := getReplicationSpecIDsFromOldAPI(ctx, cluster.GetGroupId(), cluster.GetName(), connV220240530)
zoneNameToOldReplicationSpecMeta, err := GetReplicationSpecAttributesFromOldAPI(ctx, cluster.GetGroupId(), cluster.GetName(), connV220240530.ClustersApi)
if err != nil {
return nil, diag.FromErr(err)
}

replicationSpecs, err := flattenAdvancedReplicationSpecsDS(ctx, cluster.GetReplicationSpecs(), zoneNameToOldReplicationSpecIDs, d, connV2)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
var replicationSpecs []map[string]any

if !useReplicationSpecPerShard {
replicationSpecs, err = FlattenAdvancedReplicationSpecsOldShardingConfig(ctx, cluster.GetReplicationSpecs(), zoneNameToOldReplicationSpecMeta, d.Get("replication_specs").([]any), d, connV2)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
}
} else {
replicationSpecs, err = flattenAdvancedReplicationSpecsDS(ctx, cluster.GetReplicationSpecs(), zoneNameToOldReplicationSpecMeta, d, connV2)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
}
}

result := map[string]any{
Expand Down Expand Up @@ -380,62 +373,3 @@ func flattenAdvancedClusters(ctx context.Context, connV220240530 *admin20240530.
}
return results, nil
}

func flattenAdvancedClustersOldSDK(ctx context.Context, connV20240530 *admin20240530.APIClient, connV2 *admin.APIClient, clusters []admin20240530.AdvancedClusterDescription, d *schema.ResourceData) ([]map[string]any, diag.Diagnostics) {
results := make([]map[string]any, 0, len(clusters))
for i := range clusters {
cluster := &clusters[i]
processArgs20240530, _, err := connV20240530.ClustersApi.GetClusterAdvancedConfiguration(ctx, cluster.GetGroupId(), cluster.GetName()).Execute()
if err != nil {
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
}
processArgs, _, err := connV2.ClustersApi.GetClusterAdvancedConfiguration(ctx, cluster.GetGroupId(), cluster.GetName()).Execute()
if err != nil {
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
}

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(), zoneNameToZoneIDs, cluster.GetDiskSizeGB(), nil, d, connV2)
if err != nil {
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
}

result := map[string]any{
"advanced_configuration": flattenProcessArgs(processArgs20240530, processArgs),
"backup_enabled": cluster.GetBackupEnabled(),
"bi_connector_config": flattenBiConnectorConfig(convertBiConnectToLatest(cluster.BiConnector)),
"cluster_type": cluster.GetClusterType(),
"create_date": conversion.TimePtrToStringPtr(cluster.CreateDate),
"connection_strings": flattenConnectionStrings(*convertConnectionStringToLatest(cluster.ConnectionStrings)),
"disk_size_gb": cluster.GetDiskSizeGB(),
"encryption_at_rest_provider": cluster.GetEncryptionAtRestProvider(),
"labels": flattenLabels(*convertLabelsToLatest(cluster.Labels)),
"tags": conversion.FlattenTags(convertTagsToLatest(cluster.GetTags())),
"mongo_db_major_version": cluster.GetMongoDBMajorVersion(),
"mongo_db_version": cluster.GetMongoDBVersion(),
"name": cluster.GetName(),
"paused": cluster.GetPaused(),
"pit_enabled": cluster.GetPitEnabled(),
"replication_specs": replicationSpecs,
"root_cert_type": cluster.GetRootCertType(),
"state_name": cluster.GetStateName(),
"termination_protection_enabled": cluster.GetTerminationProtectionEnabled(),
"version_release_system": cluster.GetVersionReleaseSystem(),
"global_cluster_self_managed_sharding": cluster.GetGlobalClusterSelfManagedSharding(),
"replica_set_scaling_strategy": clusterDescNew.GetReplicaSetScalingStrategy(),
"redact_client_log_data": clusterDescNew.GetRedactClientLogData(),
"config_server_management_mode": clusterDescNew.GetConfigServerManagementMode(),
"config_server_type": clusterDescNew.GetConfigServerType(),
}
results = append(results, result)
}
return results, nil
}
Loading

0 comments on commit fcca7cd

Please sign in to comment.