Skip to content

Commit

Permalink
chore: Groups common code in data source and resource for populating …
Browse files Browse the repository at this point in the history
…root fields (#2419)
  • Loading branch information
maastha authored Jul 16, 2024
1 parent 19595be commit 21239d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 92 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
81 changes: 2 additions & 79 deletions internal/service/advancedcluster/data_source_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
19 changes: 10 additions & 9 deletions internal/service/advancedcluster/resource_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 21239d2

Please sign in to comment.