Skip to content

Commit

Permalink
fix: Improves error message when improperly setting provider_region_n…
Browse files Browse the repository at this point in the history
…ame field (#1815)
  • Loading branch information
maastha committed Jan 16, 2024
1 parent e8a16ff commit 273382c
Show file tree
Hide file tree
Showing 2 changed files with 321 additions and 9 deletions.
52 changes: 45 additions & 7 deletions internal/service/cluster/resource_cluster.go
Expand Up @@ -18,12 +18,13 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"

"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"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"
)

const (
Expand Down Expand Up @@ -463,10 +464,16 @@ func resourceMongoDBAtlasClusterCreate(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf(errorClusterCreate, err))
}

clusterType := cast.ToString(d.Get("cluster_type"))
err = ValidateProviderRegionName(clusterType, providerSettings.RegionName, replicationSpecs)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterCreate, err))
}

clusterRequest := &matlas.Cluster{
Name: d.Get("name").(string),
EncryptionAtRestProvider: d.Get("encryption_at_rest_provider").(string),
ClusterType: cast.ToString(d.Get("cluster_type")),
ClusterType: clusterType,
BackupEnabled: pointy.Bool(d.Get("backup_enabled").(bool)),
PitEnabled: pointy.Bool(d.Get("pit_enabled").(bool)),
AutoScaling: autoScaling,
Expand Down Expand Up @@ -803,13 +810,23 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa
}
}

replicationSpecs, err := expandReplicationSpecs(d)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err))
}
if d.HasChange("replication_specs") {
replicationSpecs, err := expandReplicationSpecs(d)
cluster.ReplicationSpecs = replicationSpecs
}

if v, ok := d.GetOk("provider_region_name"); ok {
err = ValidateProviderRegionName(d.Get("cluster_type").(string), v.(string), replicationSpecs)
// we swallow the error here as the user may not always be able to 'unset' provider_region_name value in the state,
// We then ensure ProviderSettings.RegionName is not set in case of a multi-region cluster, refer https://jira.mongodb.org/browse/HELP-51429
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err))
if cluster.ProviderSettings != nil {
cluster.ProviderSettings.RegionName = ""
}
}

cluster.ReplicationSpecs = replicationSpecs
}

cluster.AutoScaling = &matlas.AutoScaling{Compute: &matlas.Compute{}}
Expand Down Expand Up @@ -967,6 +984,27 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa
return resourceMongoDBAtlasClusterRead(ctx, d, meta)
}

func IsMultiRegionCluster(repSpecs []matlas.ReplicationSpec) bool {
if len(repSpecs) > 1 {
return true
}

for i := range repSpecs {
if len(repSpecs[i].RegionsConfig) > 1 {
return true
}
}
return false
}

func ValidateProviderRegionName(clusterType, providerRegionName string, repSpecs []matlas.ReplicationSpec) error {
if conversion.IsStringPresent(&providerRegionName) && (clusterType == "GEOSHARDED" || IsMultiRegionCluster(repSpecs)) {
return fmt.Errorf("provider_region_name attribute must be set ONLY for single-region clusters")
}

return nil
}

func didErrOnPausedCluster(err error) bool {
if err == nil {
return false
Expand Down

0 comments on commit 273382c

Please sign in to comment.