From 21239d2ffc4119c6228a55c1c1ebefa16b19d32a Mon Sep 17 00:00:00 2001 From: maastha <122359335+maastha@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:52:02 +0100 Subject: [PATCH] chore: Groups common code in data source and resource for populating root fields (#2419) --- go.sum | 4 - .../data_source_advanced_cluster.go | 81 +------------------ .../resource_advanced_cluster.go | 19 ++--- 3 files changed, 12 insertions(+), 92 deletions(-) diff --git a/go.sum b/go.sum index 02058d0f31..54dc6c9576 100644 --- a/go.sum +++ b/go.sum @@ -782,10 +782,6 @@ go.mongodb.org/atlas v0.36.0 h1:m05S3AO7zkl+bcG1qaNsEKBnAqnKx2FDwLooHpIG3j4= go.mongodb.org/atlas v0.36.0/go.mod h1:nfPldE9dSama6G2IbIzmEza02Ly7yFZjMMVscaM0uEc= go.mongodb.org/atlas-sdk/v20231115014 v20231115014.0.0 h1:hN7x3m6THf03q/tE48up1j0U/26lJmx+s1LXB/qvHHc= go.mongodb.org/atlas-sdk/v20231115014 v20231115014.0.0/go.mod h1:pCl46YnWOIde8lq27whXDwUseNeUvtAy3vy5ZDeTcBA= -go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.0 h1:D+e3bpRwa9WH3HHs8bLjOdjTp1vdlp83ZYithzGbaQ8= -go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.0/go.mod h1:seuG5HpfG20/8FhJGyWi4yL7hqAcmq7pf/G0gipNOyM= -go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.1-0.20240703083409-9e77f6cad45f h1:p4oDdUBXj4hW/QUwQ6R3Uqx8tMJw1Z8g4b6/hfZBHfk= -go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.1-0.20240703083409-9e77f6cad45f/go.mod h1:seuG5HpfG20/8FhJGyWi4yL7hqAcmq7pf/G0gipNOyM= go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.1-0.20240710142852-8a1b5dd5d8f3 h1:Y2OD2wNisDWY/am92KmGGftOZxLOXSzr9+WyACRQ1Zw= go.mongodb.org/atlas-sdk/v20240530002 v20240530002.0.1-0.20240710142852-8a1b5dd5d8f3/go.mod h1:seuG5HpfG20/8FhJGyWi4yL7hqAcmq7pf/G0gipNOyM= go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M= diff --git a/internal/service/advancedcluster/data_source_advanced_cluster.go b/internal/service/advancedcluster/data_source_advanced_cluster.go index 64cd26cdec..bd005f7c57 100644 --- a/internal/service/advancedcluster/data_source_advanced_cluster.go +++ b/internal/service/advancedcluster/data_source_advanced_cluster.go @@ -6,13 +6,11 @@ import ( "net/http" admin20231115 "go.mongodb.org/atlas-sdk/v20231115014/admin" - "go.mongodb.org/atlas-sdk/v20240530002/admin" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" ) @@ -288,7 +286,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) } - diags := setRootFields(d, convertClusterDescToLatestExcludeRepSpecs(clusterDescOld)) + diags := setRootFields(d, convertClusterDescToLatestExcludeRepSpecs(clusterDescOld), false) if diags.HasError() { return diags } @@ -313,7 +311,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err)) } - diags := setRootFields(d, clusterDescLatest) + diags := setRootFields(d, clusterDescLatest, false) if diags.HasError() { return diags } @@ -336,78 +334,3 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag. d.SetId(clusterID) return nil } - -func setRootFields(d *schema.ResourceData, cluster *admin.ClusterDescription20250101) diag.Diagnostics { - clusterName := *cluster.Name - - if err := d.Set("backup_enabled", cluster.GetBackupEnabled()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "backup_enabled", clusterName, err)) - } - - if err := d.Set("bi_connector_config", flattenBiConnectorConfig(cluster.BiConnector)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "bi_connector_config", clusterName, err)) - } - - if err := d.Set("cluster_type", cluster.GetClusterType()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_type", clusterName, err)) - } - - if err := d.Set("connection_strings", flattenConnectionStrings(*cluster.ConnectionStrings)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "connection_strings", clusterName, err)) - } - - if err := d.Set("create_date", conversion.TimePtrToStringPtr(cluster.CreateDate)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "create_date", clusterName, err)) - } - - if err := d.Set("encryption_at_rest_provider", cluster.GetEncryptionAtRestProvider()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "encryption_at_rest_provider", clusterName, err)) - } - - if err := d.Set("labels", flattenLabels(*cluster.Labels)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "labels", clusterName, err)) - } - - if err := d.Set("tags", flattenTags(cluster.Tags)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "tags", clusterName, err)) - } - - if err := d.Set("mongo_db_major_version", cluster.GetMongoDBMajorVersion()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "mongo_db_major_version", clusterName, err)) - } - - if err := d.Set("mongo_db_version", cluster.GetMongoDBVersion()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "mongo_db_version", clusterName, err)) - } - - if err := d.Set("name", cluster.GetName()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "name", clusterName, err)) - } - - if err := d.Set("paused", cluster.GetPaused()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "paused", clusterName, err)) - } - - if err := d.Set("pit_enabled", cluster.GetPitEnabled()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err)) - } - - if err := d.Set("root_cert_type", cluster.GetRootCertType()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "state_name", clusterName, err)) - } - - if err := d.Set("state_name", cluster.GetStateName()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "state_name", clusterName, err)) - } - if err := d.Set("termination_protection_enabled", cluster.GetTerminationProtectionEnabled()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "termination_protection_enabled", clusterName, err)) - } - if err := d.Set("version_release_system", cluster.GetVersionReleaseSystem()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "version_release_system", clusterName, err)) - } - if err := d.Set("global_cluster_self_managed_sharding", cluster.GetGlobalClusterSelfManagedSharding()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "global_cluster_self_managed_sharding", clusterName, err)) - } - - return nil -} diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index 87fd235805..ac68a784f2 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -553,7 +553,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di clusterResp = cluster } - diags := setResourceRootFields(d, clusterResp) + diags := setRootFields(d, clusterResp, true) if diags.HasError() { return diags } @@ -574,12 +574,17 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di return nil } -// TODO: CLOUDP-259838 this can likely be unified with data source function setRootFields -func setResourceRootFields(d *schema.ResourceData, cluster *admin.ClusterDescription20250101) diag.Diagnostics { +func setRootFields(d *schema.ResourceData, cluster *admin.ClusterDescription20250101, isResourceSchema bool) diag.Diagnostics { clusterName := *cluster.Name - if err := d.Set("cluster_id", cluster.GetId()); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_id", clusterName, err)) + if isResourceSchema { + if err := d.Set("cluster_id", cluster.GetId()); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "cluster_id", clusterName, err)) + } + + if err := d.Set("accept_data_risks_and_force_replica_set_reconfig", conversion.TimePtrToStringPtr(cluster.AcceptDataRisksAndForceReplicaSetReconfig)); err != nil { + return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "accept_data_risks_and_force_replica_set_reconfig", clusterName, err)) + } } if err := d.Set("backup_enabled", cluster.GetBackupEnabled()); err != nil { @@ -650,10 +655,6 @@ func setResourceRootFields(d *schema.ResourceData, cluster *admin.ClusterDescrip return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "version_release_system", clusterName, err)) } - if err := d.Set("accept_data_risks_and_force_replica_set_reconfig", conversion.TimePtrToStringPtr(cluster.AcceptDataRisksAndForceReplicaSetReconfig)); err != nil { - return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "accept_data_risks_and_force_replica_set_reconfig", clusterName, err)) - } - if err := d.Set("global_cluster_self_managed_sharding", cluster.GetGlobalClusterSelfManagedSharding()); err != nil { return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "global_cluster_self_managed_sharding", clusterName, err)) }