From 4a93435405d7fcceab7da26b9421f96215324397 Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 18 Oct 2022 13:13:26 -0500 Subject: [PATCH 01/27] Initial commit for cluster upgrade --- mongodbatlas/resource_mongodbatlas_cluster.go | 70 ++++++++++++++++--- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 980f25c1bc..5f398f3e9e 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -14,6 +14,7 @@ import ( "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -161,7 +162,6 @@ func resourceMongoDBAtlasCluster() *schema.Resource { }, "provider_name": { Type: schema.TypeString, - ForceNew: true, Required: true, }, "pit_enabled": { @@ -353,6 +353,12 @@ func resourceMongoDBAtlasCluster() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"LTS", "CONTINUOUS"}, false), }, }, + CustomizeDiff: customdiff.All( + customdiff.ForceNewIfChange("provider_name", func(ctx context.Context, old, new, meta any) bool { + // If going from TENANT to non-tenant, attempt an upgrade (not new) + return old != new && old != "TENANT" + }), + ), } } @@ -902,8 +908,16 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa // Has changes if !reflect.DeepEqual(cluster, matlas.Cluster{}) { - err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - _, _, err := updateCluster(ctx, conn, cluster, projectID, clusterName) + var err error + + err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { + willUpgrade := isUpgradeRequired(d) + + if willUpgrade { + _, _, err = upgradeCluster(ctx, conn, cluster, projectID, clusterName) + } else { + _, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) + } if err != nil { var target *matlas.ErrorResponse if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { @@ -1140,12 +1154,17 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er } providerSettings := &matlas.ProviderSettings{ - BackingProviderName: cast.ToString(d.Get("backing_provider_name")), - InstanceSizeName: cast.ToString(d.Get("provider_instance_size_name")), - ProviderName: cast.ToString(d.Get("provider_name")), - RegionName: region, - VolumeType: cast.ToString(d.Get("provider_volume_type")), - DiskTypeName: cast.ToString(d.Get("provider_disk_type_name")), + InstanceSizeName: cast.ToString(d.Get("provider_instance_size_name")), + ProviderName: cast.ToString(d.Get("provider_name")), + RegionName: region, + VolumeType: cast.ToString(d.Get("provider_volume_type")), + DiskTypeName: cast.ToString(d.Get("provider_disk_type_name")), + } + + b, bOk := d.GetOk("backing_provider_name") + + if bOk && b != "" { + providerSettings.BackingProviderName = cast.ToString(b) } if autoScalingEnabled { @@ -1209,6 +1228,12 @@ func flattenProviderSettings(d *schema.ResourceData, settings *matlas.ProviderSe } } +func isUpgradeRequired(d *schema.ResourceData) bool { + pName, nName := d.GetChange("provider_name") + + return pName != nName && pName == "TENANT" +} + func expandReplicationSpecs(d *schema.ResourceData) ([]matlas.ReplicationSpec, error) { rSpecs := make([]matlas.ReplicationSpec, 0) @@ -1668,3 +1693,30 @@ func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Clu return cluster, resp, nil } + +func upgradeCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string) (*matlas.Cluster, *matlas.Response, error) { + request.Name = name + request.ProviderSettings.BackingProviderName = nil + + cluster, resp, err := conn.Clusters.Upgrade(ctx, projectID, request) + if err != nil { + return nil, nil, err + } + + stateConf := &resource.StateChangeConf{ + Pending: []string{"CREATING", "UPDATING", "REPAIRING"}, + Target: []string{"IDLE"}, + Refresh: resourceClusterRefreshFunc(ctx, name, projectID, conn), + Timeout: 3 * time.Hour, + MinTimeout: 30 * time.Second, + Delay: 1 * time.Minute, + } + + // Wait, catching any errors + _, err = stateConf.WaitForStateContext(ctx) + if err != nil { + return nil, nil, err + } + + return cluster, resp, nil +} From 0d8b28a1235e618de88cda1ec8c33c00e777f36c Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 18 Oct 2022 13:28:23 -0500 Subject: [PATCH 02/27] Updated to use a custom customizeDiff func --- mongodbatlas/resource_mongodbatlas_cluster.go | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 5f398f3e9e..669204be29 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -14,7 +14,6 @@ import ( "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -353,12 +352,7 @@ func resourceMongoDBAtlasCluster() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"LTS", "CONTINUOUS"}, false), }, }, - CustomizeDiff: customdiff.All( - customdiff.ForceNewIfChange("provider_name", func(ctx context.Context, old, new, meta any) bool { - // If going from TENANT to non-tenant, attempt an upgrade (not new) - return old != new && old != "TENANT" - }), - ), + CustomizeDiff: resourceClusterCustomizeDiff, } } @@ -1442,6 +1436,20 @@ func resourceClusterRefreshFunc(ctx context.Context, name, projectID string, cli } } +func resourceClusterCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { + pName, nName := d.GetChange("provider_name") + + isUpgrade := pName != nName && pName == "TENANT" + + if isUpgrade { + d.SetNew("backing_provider_name", nil) + } else if pName != nName { + d.ForceNew("provider_name") + } + + return nil +} + func formatMongoDBMajorVersion(val interface{}) string { if strings.Contains(val.(string), ".") { return val.(string) @@ -1696,7 +1704,6 @@ func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Clu func upgradeCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string) (*matlas.Cluster, *matlas.Response, error) { request.Name = name - request.ProviderSettings.BackingProviderName = nil cluster, resp, err := conn.Clusters.Upgrade(ctx, projectID, request) if err != nil { From 7cbd85ccd2377bba03ac18933f7d0d2b6a31f103 Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 18 Oct 2022 15:40:15 -0500 Subject: [PATCH 03/27] Setting cluster_id and resource ID to the updated values in the case of a cluster upgrade --- mongodbatlas/resource_mongodbatlas_cluster.go | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 669204be29..63ecfe1acd 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -903,14 +903,14 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa // Has changes if !reflect.DeepEqual(cluster, matlas.Cluster{}) { var err error + var updatedCluster *matlas.Cluster + willUpgrade := isUpgradeRequired(d) err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - willUpgrade := isUpgradeRequired(d) - if willUpgrade { - _, _, err = upgradeCluster(ctx, conn, cluster, projectID, clusterName) + updatedCluster, _, err = upgradeCluster(ctx, conn, cluster, projectID, clusterName) } else { - _, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) + updatedCluster, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) } if err != nil { var target *matlas.ErrorResponse @@ -932,6 +932,16 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa if err != nil { return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err)) } + + if willUpgrade { + d.Set("cluster_id", updatedCluster.ID) + d.SetId(encodeStateID(map[string]string{ + "cluster_id": updatedCluster.ID, + "project_id": projectID, + "cluster_name": cluster.Name, + "provider_name": cluster.ProviderSettings.ProviderName, + })) + } } /* @@ -1124,6 +1134,7 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er instanceSize = getInstanceSizeToInt(d.Get("provider_instance_size_name").(string)) compute *matlas.Compute autoScalingEnabled = d.Get("auto_scaling_compute_enabled").(bool) + providerName = cast.ToString(d.Get("provider_name")) ) if minInstanceSize != 0 && autoScalingEnabled { @@ -1149,16 +1160,14 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er providerSettings := &matlas.ProviderSettings{ InstanceSizeName: cast.ToString(d.Get("provider_instance_size_name")), - ProviderName: cast.ToString(d.Get("provider_name")), + ProviderName: providerName, RegionName: region, VolumeType: cast.ToString(d.Get("provider_volume_type")), DiskTypeName: cast.ToString(d.Get("provider_disk_type_name")), } - b, bOk := d.GetOk("backing_provider_name") - - if bOk && b != "" { - providerSettings.BackingProviderName = cast.ToString(b) + if providerName == "TENANT" { + providerSettings.BackingProviderName = cast.ToString(d.Get("backing_provider_name")) } if autoScalingEnabled { @@ -1185,8 +1194,10 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er } func flattenProviderSettings(d *schema.ResourceData, settings *matlas.ProviderSettings, clusterName string) { - if err := d.Set("backing_provider_name", settings.BackingProviderName); err != nil { - log.Printf(errorClusterSetting, "backing_provider_name", clusterName, err) + if settings.ProviderName == "TENANT" { + if err := d.Set("backing_provider_name", settings.BackingProviderName); err != nil { + log.Printf(errorClusterSetting, "backing_provider_name", clusterName, err) + } } if settings.DiskIOPS != nil && *settings.DiskIOPS != 0 { @@ -1442,7 +1453,7 @@ func resourceClusterCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, m isUpgrade := pName != nName && pName == "TENANT" if isUpgrade { - d.SetNew("backing_provider_name", nil) + d.SetNewComputed("backing_provider_name") } else if pName != nName { d.ForceNew("provider_name") } From a8838f4b143228f11359f5c82d8642ba9f22160b Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 20 Oct 2022 15:13:46 -0500 Subject: [PATCH 04/27] Updated logic for determining whether upgrade is required --- mongodbatlas/resource_mongodbatlas_cluster.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 63ecfe1acd..9a6c50f114 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -938,8 +938,8 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa d.SetId(encodeStateID(map[string]string{ "cluster_id": updatedCluster.ID, "project_id": projectID, - "cluster_name": cluster.Name, - "provider_name": cluster.ProviderSettings.ProviderName, + "cluster_name": updatedCluster.Name, + "provider_name": updatedCluster.ProviderSettings.ProviderName, })) } } @@ -1234,9 +1234,13 @@ func flattenProviderSettings(d *schema.ResourceData, settings *matlas.ProviderSe } func isUpgradeRequired(d *schema.ResourceData) bool { - pName, nName := d.GetChange("provider_name") + pInstance, nInstance := d.GetChange("provider_instance_size_name") + + if pInstance == nInstance { + return false + } - return pName != nName && pName == "TENANT" + return pInstance == "M0" || pInstance == "M2" || pInstance == "M5" } func expandReplicationSpecs(d *schema.ResourceData) ([]matlas.ReplicationSpec, error) { From c520728174fc3570144cd24eb4d74dac16ae86be Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 20 Oct 2022 16:47:10 -0500 Subject: [PATCH 05/27] Removed conflation on willUpgrade and tenant changes. Added error checking --- mongodbatlas/resource_mongodbatlas_cluster.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 9a6c50f114..09cde8fba7 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -934,7 +934,9 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } if willUpgrade { - d.Set("cluster_id", updatedCluster.ID) + if err := d.Set("cluster_id", updatedCluster.ID); err != nil { + return diag.FromErr(fmt.Errorf(errorClusterSetting, "cluster_id", clusterName, err)) + } d.SetId(encodeStateID(map[string]string{ "cluster_id": updatedCluster.ID, "project_id": projectID, @@ -1452,17 +1454,19 @@ func resourceClusterRefreshFunc(ctx context.Context, name, projectID string, cli } func resourceClusterCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { + var err error = nil pName, nName := d.GetChange("provider_name") - isUpgrade := pName != nName && pName == "TENANT" + willProviderChange := pName != nName + willLeaveTenant := willProviderChange && pName == "TENANT" - if isUpgrade { - d.SetNewComputed("backing_provider_name") - } else if pName != nName { - d.ForceNew("provider_name") + if willLeaveTenant { + err = d.SetNewComputed("backing_provider_name") + } else if willProviderChange { + err = d.ForceNew("provider_name") } - return nil + return err } func formatMongoDBMajorVersion(val interface{}) string { From 1359a1817f89cf89370a63be6e70d596e3d8e06c Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 24 Oct 2022 09:39:41 -0500 Subject: [PATCH 06/27] Updated cluster docs to denote upgrade support --- website/docs/r/cluster.html.markdown | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/website/docs/r/cluster.html.markdown b/website/docs/r/cluster.html.markdown index 442f571f75..131cd24cdf 100644 --- a/website/docs/r/cluster.html.markdown +++ b/website/docs/r/cluster.html.markdown @@ -17,7 +17,17 @@ description: |- ~> **IMPORTANT:**
• New Users: If you are not already using `mongodbatlas_cluster` for your deployment we recommend starting with the [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster). `mongodbatlas_advanced_cluster` has all the same functionality as `mongodbatlas_cluster` but also supports multi-cloud clusters.
• Free tier cluster creation (M0) is supported. -
• Shared tier clusters (M0, M2, M5) cannot be upgraded to higher tiers via API or by this Provider. WARNING! If you attempt to upgrade from an existing shared tier cluster that you manage with this Provider to a dedicated cluster (M10+) Terraform will see it as a request to destroy the shared tier cluster and as a request to create a dedicated tier cluster, i.e. Terraform will not see it as a request to upgrade. If you accept the plan in this case the shared tier cluster would be destroyed and you would lose the data on that cluster. Do not attempt to upgrade from the shared to dedicated tier via this Provider, it is not supported! +
• Shared tier clusters (M0, M2, M5) are supported. +``` +provider_instance_size_name = "M0" +provider_name = "TENANT" +backing_provider_name = "AWS" +``` +
• Shared tier clusters can now be upgraded to higher tiers via API or by this Provider. WARNING! Any change from shared tier to a different instance size will be considered a tenant upgrade. If upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, ex: +``` +provider_instance_size_name = "M10" +provider_name = "AWS" +```
• Changes to cluster configurations can affect costs. Before making changes, please see [Billing](https://docs.atlas.mongodb.com/billing/).
• If your Atlas project contains a custom role that uses actions introduced in a specific MongoDB version, you cannot create a cluster with a MongoDB version less than that version unless you delete the custom role. From 49554e9fbbfbbbce7d5610624424438bd89fe0d0 Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 24 Oct 2022 10:58:32 -0500 Subject: [PATCH 07/27] Added an example tenant upgrade --- .../v146/tenant-upgrade/README.md | 32 +++++++++++++++++++ .../test-upgrade/v146/tenant-upgrade/main.tf | 19 +++++++++++ .../v146/tenant-upgrade/variables.tf | 22 +++++++++++++ .../v146/tenant-upgrade/versions.tf | 8 +++++ 4 files changed, 81 insertions(+) create mode 100644 examples/test-upgrade/v146/tenant-upgrade/README.md create mode 100644 examples/test-upgrade/v146/tenant-upgrade/main.tf create mode 100644 examples/test-upgrade/v146/tenant-upgrade/variables.tf create mode 100644 examples/test-upgrade/v146/tenant-upgrade/versions.tf diff --git a/examples/test-upgrade/v146/tenant-upgrade/README.md b/examples/test-upgrade/v146/tenant-upgrade/README.md new file mode 100644 index 0000000000..55d1a7d52b --- /dev/null +++ b/examples/test-upgrade/v146/tenant-upgrade/README.md @@ -0,0 +1,32 @@ +# MongoDB Atlas Provider -- Cluster Tenant Upgrade +This example creates a project and cluster. It is intended to show how to upgrade from shared to dedicated tier. + +Variables Required: +- `atlas_org_id`: ID of atlas organization +- `public_key`: Atlas provider public_key +- `private_key`: Atlas provider private key +- `provider_name`: Name of provider to use for cluster (TENANT, AWS, GCP) +- `backing_provider_name`: If provider_name is tenant, the backing provider (AWS, GCP) +- `provider_instance_size_name`: Size of the cluster (M0, M2, M5, M10, etc...) + +For this example, first we'll start out on the shared tier, then upgrade to a dedicated tier. + +As such, utilize the following example `terraform.tfvars` and pseudo-code to execute a working example: + +Apply with the following `terraform.tfvars` for shared tier cluster: +``` +atlas_org_id = "627a9687f7f7f7f774de306f14" +public_key = +private_key = +provider_name = "TENANT" +backing_provider_name = "AWS" +provider_instance_size_name = "M2" +``` + +Apply with the following `terraform.tfvars` to upgrade to dedicated tier: +``` +atlas_org_id = "627a9687f7f7f7f774de306f14" +public_key = +private_key = +provider_name = "GCP" +provider_instance_size_name = "M10" \ No newline at end of file diff --git a/examples/test-upgrade/v146/tenant-upgrade/main.tf b/examples/test-upgrade/v146/tenant-upgrade/main.tf new file mode 100644 index 0000000000..e8d9517b1d --- /dev/null +++ b/examples/test-upgrade/v146/tenant-upgrade/main.tf @@ -0,0 +1,19 @@ +provider "mongodbatlas" { + public_key = var.public_key + private_key = var.private_key +} + +resource "mongodbatlas_cluster" "cluster" { + project_id = mongodbatlas_project.project.id + name = "ClusterToUpgrade" + cluster_type = "REPLICASET" + provider_name = var.provider_name + backing_provider_name = var.backing_provider_name + provider_region_name = "US_EAST_1" + provider_instance_size_name = var.provider_instance_size_name +} + +resource "mongodbatlas_project" "project" { + name = "ClusterTenantUpgrade" + org_id = var.atlas_org_id +} diff --git a/examples/test-upgrade/v146/tenant-upgrade/variables.tf b/examples/test-upgrade/v146/tenant-upgrade/variables.tf new file mode 100644 index 0000000000..1634d7db9b --- /dev/null +++ b/examples/test-upgrade/v146/tenant-upgrade/variables.tf @@ -0,0 +1,22 @@ +variable "atlas_org_id" { + description = "Atlas orgnization id" + default = "" +} +variable "public_key" { + description = "Public API key to authenticate to Atlas" +} +variable "private_key" { + description = "Private API key to authenticate to Atlas" +} +variable "provider_name" { + description = "Atlas cluster provider name" + default = "AWS" +} +variable "backing_provider_name" { + description = "Atlas cluster backing provider name" + default = null +} +variable "provider_instance_size_name" { + description = "Atlas cluster provider instance name" + default = "M10" +} diff --git a/examples/test-upgrade/v146/tenant-upgrade/versions.tf b/examples/test-upgrade/v146/tenant-upgrade/versions.tf new file mode 100644 index 0000000000..1a4117cfda --- /dev/null +++ b/examples/test-upgrade/v146/tenant-upgrade/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + mongodbatlas = { + source = "mongodb/mongodbatlas" + } + } + required_version = ">= 0.14" +} \ No newline at end of file From 618e09d04323f959b6e72945910b2076f94b56fc Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 24 Oct 2022 16:46:41 -0500 Subject: [PATCH 08/27] Fixed formatting issues --- examples/test-upgrade/v146/tenant-upgrade/variables.tf | 10 +++++----- mongodbatlas/resource_mongodbatlas_cluster.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/test-upgrade/v146/tenant-upgrade/variables.tf b/examples/test-upgrade/v146/tenant-upgrade/variables.tf index 1634d7db9b..0cfb025cda 100644 --- a/examples/test-upgrade/v146/tenant-upgrade/variables.tf +++ b/examples/test-upgrade/v146/tenant-upgrade/variables.tf @@ -1,6 +1,6 @@ variable "atlas_org_id" { description = "Atlas orgnization id" - default = "" + default = "" } variable "public_key" { description = "Public API key to authenticate to Atlas" @@ -10,13 +10,13 @@ variable "private_key" { } variable "provider_name" { description = "Atlas cluster provider name" - default = "AWS" + default = "AWS" } variable "backing_provider_name" { description = "Atlas cluster backing provider name" - default = null + default = null } variable "provider_instance_size_name" { description = "Atlas cluster provider instance name" - default = "M10" -} + default = "M10" +} \ No newline at end of file diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 09cde8fba7..a57b9d6fdd 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -1454,7 +1454,7 @@ func resourceClusterRefreshFunc(ctx context.Context, name, projectID string, cli } func resourceClusterCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { - var err error = nil + var err error pName, nName := d.GetChange("provider_name") willProviderChange := pName != nName From d3c6edb7691a673666eb03277423365ba4ce1418 Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 25 Oct 2022 14:34:53 -0500 Subject: [PATCH 09/27] Added code to upgrade advanced_cluster --- .../resource_mongodbatlas_advanced_cluster.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index b1d90e1d19..752e135bb3 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -568,6 +568,33 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re cluster.VersionReleaseSystem = d.Get("version_release_system").(string) } + upgradeRequest := getUpgradeRequest(d) + + if upgradeRequest != nil { + err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { + _, _, err := upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) + if err != nil { + var target *matlas.ErrorResponse + if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { + clusterRequest := &matlas.AdvancedCluster{ + Paused: pointy.Bool(false), + } + _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) + if err != nil { + return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + if errors.As(err, &target) && target.HTTPCode == 400 { + return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + return nil + }) + if err != nil { + return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + // Has changes if !reflect.DeepEqual(cluster, matlas.Cluster{}) { err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { @@ -1079,6 +1106,37 @@ func replicationSpecsHashSet(v interface{}) int { return schema.HashString(buf.String()) } +func getUpgradeRequest(d *schema.ResourceData) *matlas.Cluster { + if !d.HasChange("replication_specs") { + return nil + } + + crs, nrs := d.GetChange("replication_specs") + cReplicationSpecs := expandAdvancedReplicationSpecs(crs.(*schema.Set).List()) + nReplicationSpecs := expandAdvancedReplicationSpecs(nrs.(*schema.Set).List()) + + if len(cReplicationSpecs) != 1 || len(nReplicationSpecs) != 1 || len(cReplicationSpecs[0].RegionConfigs) != 1 || len(nReplicationSpecs[0].RegionConfigs) != 1 { + return nil + } + + cRegionConfig := cReplicationSpecs[0].RegionConfigs[0] + nRegionConfig := nReplicationSpecs[0].RegionConfigs[0] + cInstanceSize := cRegionConfig.ElectableSpecs.InstanceSize + + if cRegionConfig.ElectableSpecs.InstanceSize == nRegionConfig.ElectableSpecs.InstanceSize || !(cInstanceSize == "M0" || + cInstanceSize == "M2" || + cInstanceSize == "M5") { + return nil + } + + return &matlas.Cluster{ + ProviderSettings: &matlas.ProviderSettings{ + ProviderName: nRegionConfig.ProviderName, + InstanceSizeName: nRegionConfig.ElectableSpecs.InstanceSize, + }, + } +} + func updateAdvancedCluster(ctx context.Context, conn *matlas.Client, request *matlas.AdvancedCluster, projectID, name string) (*matlas.AdvancedCluster, *matlas.Response, error) { cluster, resp, err := conn.AdvancedClusters.Update(ctx, projectID, name, request) if err != nil { From 0d2cf1f5155eedd512a1f21b8ca58795ce2d536d Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 25 Oct 2022 15:46:12 -0500 Subject: [PATCH 10/27] Moved example again, for some reason --- .../point-in-time}/README.md | 0 .../point-in-time}/main.tf | 0 .../point-in-time}/variables.tf | 0 .../point-in-time}/versions.tf | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename examples/{test-upgrade/v146/tenant-upgrade => atlas-backup-snapshot-restore-job/point-in-time}/README.md (100%) rename examples/{test-upgrade/v146/tenant-upgrade => atlas-backup-snapshot-restore-job/point-in-time}/main.tf (100%) rename examples/{test-upgrade/v146/tenant-upgrade => atlas-backup-snapshot-restore-job/point-in-time}/variables.tf (100%) rename examples/{test-upgrade/v146/tenant-upgrade => atlas-backup-snapshot-restore-job/point-in-time}/versions.tf (100%) diff --git a/examples/test-upgrade/v146/tenant-upgrade/README.md b/examples/atlas-backup-snapshot-restore-job/point-in-time/README.md similarity index 100% rename from examples/test-upgrade/v146/tenant-upgrade/README.md rename to examples/atlas-backup-snapshot-restore-job/point-in-time/README.md diff --git a/examples/test-upgrade/v146/tenant-upgrade/main.tf b/examples/atlas-backup-snapshot-restore-job/point-in-time/main.tf similarity index 100% rename from examples/test-upgrade/v146/tenant-upgrade/main.tf rename to examples/atlas-backup-snapshot-restore-job/point-in-time/main.tf diff --git a/examples/test-upgrade/v146/tenant-upgrade/variables.tf b/examples/atlas-backup-snapshot-restore-job/point-in-time/variables.tf similarity index 100% rename from examples/test-upgrade/v146/tenant-upgrade/variables.tf rename to examples/atlas-backup-snapshot-restore-job/point-in-time/variables.tf diff --git a/examples/test-upgrade/v146/tenant-upgrade/versions.tf b/examples/atlas-backup-snapshot-restore-job/point-in-time/versions.tf similarity index 100% rename from examples/test-upgrade/v146/tenant-upgrade/versions.tf rename to examples/atlas-backup-snapshot-restore-job/point-in-time/versions.tf From 0bad5b21e74c1ba5c45a6199acba8e6a67d9bb37 Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 27 Oct 2022 14:54:59 -0500 Subject: [PATCH 11/27] Moved examples, got advanced-cluster tenantUpgrade working --- .../tenant-upgrade/README.md | 32 +++++++ .../tenant-upgrade/main.tf | 35 ++++++++ .../tenant-upgrade/variables.tf | 26 ++++++ .../tenant-upgrade}/versions.tf | 0 .../tenant-upgrade}/README.md | 0 .../tenant-upgrade}/main.tf | 2 +- .../tenant-upgrade}/variables.tf | 0 .../atlas-cluster/tenant-upgrade/versions.tf | 8 ++ .../resource_mongodbatlas_advanced_cluster.go | 89 +++++++++++++------ website/docs/r/advanced_cluster.html.markdown | 24 +++++ website/docs/r/cluster.html.markdown | 2 +- 11 files changed, 188 insertions(+), 30 deletions(-) create mode 100644 examples/atlas-advanced-cluster/tenant-upgrade/README.md create mode 100644 examples/atlas-advanced-cluster/tenant-upgrade/main.tf create mode 100644 examples/atlas-advanced-cluster/tenant-upgrade/variables.tf rename examples/{atlas-backup-snapshot-restore-job/point-in-time => atlas-advanced-cluster/tenant-upgrade}/versions.tf (100%) rename examples/{atlas-backup-snapshot-restore-job/point-in-time => atlas-cluster/tenant-upgrade}/README.md (100%) rename examples/{atlas-backup-snapshot-restore-job/point-in-time => atlas-cluster/tenant-upgrade}/main.tf (94%) rename examples/{atlas-backup-snapshot-restore-job/point-in-time => atlas-cluster/tenant-upgrade}/variables.tf (100%) create mode 100644 examples/atlas-cluster/tenant-upgrade/versions.tf diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/README.md b/examples/atlas-advanced-cluster/tenant-upgrade/README.md new file mode 100644 index 0000000000..5c50b60d0f --- /dev/null +++ b/examples/atlas-advanced-cluster/tenant-upgrade/README.md @@ -0,0 +1,32 @@ +# MongoDB Atlas Provider -- Advanced Cluster Tenant Upgrade +This example creates a project and cluster. It is intended to show how to upgrade from shared to dedicated tier. + +Variables Required: +- `atlas_org_id`: ID of atlas organization +- `public_key`: Atlas provider public_key +- `private_key`: Atlas provider private key +- `provider_name`: Name of provider to use for cluster (TENANT, AWS, GCP) +- `backing_provider_name`: If provider_name is tenant, the backing provider (AWS, GCP) +- `provider_instance_size_name`: Size of the cluster (M0, M2, M5, M10, etc...) + +For this example, first we'll start out on the shared tier, then upgrade to a dedicated tier. + +As such, utilize the following example `terraform.tfvars` and pseudo-code to execute a working example: + +Apply with the following `terraform.tfvars` for shared tier cluster: +``` +atlas_org_id = "627a9687f7f7f7f774de306f14" +public_key = +private_key = +provider_name = "TENANT" +backing_provider_name = "AWS" +provider_instance_size_name = "M2" +``` + +Apply with the following `terraform.tfvars` to upgrade to dedicated tier: +``` +atlas_org_id = "627a9687f7f7f7f774de306f14" +public_key = +private_key = +provider_name = "GCP" +provider_instance_size_name = "M10" \ No newline at end of file diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/main.tf b/examples/atlas-advanced-cluster/tenant-upgrade/main.tf new file mode 100644 index 0000000000..21f246c7a3 --- /dev/null +++ b/examples/atlas-advanced-cluster/tenant-upgrade/main.tf @@ -0,0 +1,35 @@ +provider "mongodbatlas" { + public_key = var.public_key + private_key = var.private_key +} + +resource "mongodbatlas_advanced_cluster" "cluster" { + project_id = mongodbatlas_project.project.id + name = "ClusterToUpgrade" + cluster_type = "REPLICASET" + + replication_specs { + num_shards = 1 + + region_configs { + electable_specs { + instance_size = var.provider_instance_size_name + node_count = var.node_count + } + provider_name = var.provider_name + backing_provider_name = var.backing_provider_name + region_name = "US_EAST_1" + priority = 7 + } + } +} + +resource "mongodbatlas_project" "project" { + name = "TenantUpgradeTest" + org_id = var.atlas_org_id +} + +output "project_name" { + value = mongodbatlas_project.project.name + description = "(Expected) Name of the MongoDB Atlas Cluster" +} \ No newline at end of file diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf b/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf new file mode 100644 index 0000000000..4aacaf4a8e --- /dev/null +++ b/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf @@ -0,0 +1,26 @@ +variable "atlas_org_id" { + description = "Atlas orgnization id" + default = "" +} +variable "public_key" { + description = "Public API key to authenticate to Atlas" +} +variable "private_key" { + description = "Private API key to authenticate to Atlas" +} +variable "provider_name" { + description = "Atlas cluster provider name" + default = "AWS" +} +variable "backing_provider_name" { + description = "Atlas cluster backing provider name" + default = null +} +variable "provider_instance_size_name" { + description = "Atlas cluster provider instance name" + default = "M10" +} +variable "node_count" { + description = "Atlas cluster node count" + default = null +} \ No newline at end of file diff --git a/examples/atlas-backup-snapshot-restore-job/point-in-time/versions.tf b/examples/atlas-advanced-cluster/tenant-upgrade/versions.tf similarity index 100% rename from examples/atlas-backup-snapshot-restore-job/point-in-time/versions.tf rename to examples/atlas-advanced-cluster/tenant-upgrade/versions.tf diff --git a/examples/atlas-backup-snapshot-restore-job/point-in-time/README.md b/examples/atlas-cluster/tenant-upgrade/README.md similarity index 100% rename from examples/atlas-backup-snapshot-restore-job/point-in-time/README.md rename to examples/atlas-cluster/tenant-upgrade/README.md diff --git a/examples/atlas-backup-snapshot-restore-job/point-in-time/main.tf b/examples/atlas-cluster/tenant-upgrade/main.tf similarity index 94% rename from examples/atlas-backup-snapshot-restore-job/point-in-time/main.tf rename to examples/atlas-cluster/tenant-upgrade/main.tf index e8d9517b1d..6896f35e6d 100644 --- a/examples/atlas-backup-snapshot-restore-job/point-in-time/main.tf +++ b/examples/atlas-cluster/tenant-upgrade/main.tf @@ -14,6 +14,6 @@ resource "mongodbatlas_cluster" "cluster" { } resource "mongodbatlas_project" "project" { - name = "ClusterTenantUpgrade" + name = "TenantUpgradeTest" org_id = var.atlas_org_id } diff --git a/examples/atlas-backup-snapshot-restore-job/point-in-time/variables.tf b/examples/atlas-cluster/tenant-upgrade/variables.tf similarity index 100% rename from examples/atlas-backup-snapshot-restore-job/point-in-time/variables.tf rename to examples/atlas-cluster/tenant-upgrade/variables.tf diff --git a/examples/atlas-cluster/tenant-upgrade/versions.tf b/examples/atlas-cluster/tenant-upgrade/versions.tf new file mode 100644 index 0000000000..1a4117cfda --- /dev/null +++ b/examples/atlas-cluster/tenant-upgrade/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + mongodbatlas = { + source = "mongodb/mongodbatlas" + } + } + required_version = ">= 0.14" +} \ No newline at end of file diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index 752e135bb3..7686495e04 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -29,13 +29,14 @@ const ( errorClusterAdvancedSetting = "error setting `%s` for MongoDB ClusterAdvanced (%s): %s" errorAdvancedClusterAdvancedConfUpdate = "error updating Advanced Configuration Option form MongoDB Cluster (%s): %s" errorAdvancedClusterAdvancedConfRead = "error reading Advanced Configuration Option form MongoDB Cluster (%s): %s" + upgradeRequestCtx = "upgradeRequest" ) func resourceMongoDBAtlasAdvancedCluster() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceMongoDBAtlasAdvancedClusterCreate, ReadWithoutTimeout: resourceMongoDBAtlasAdvancedClusterRead, - UpdateWithoutTimeout: resourceMongoDBAtlasAdvancedClusterUpdate, + UpdateWithoutTimeout: resourceMongoDBAtlasAdvancedClusterUpdateOrUpgrade, DeleteWithoutTimeout: resourceMongoDBAtlasAdvancedClusterDelete, Importer: &schema.ResourceImporter{ StateContext: resourceMongoDBAtlasAdvancedClusterImportState, @@ -511,6 +512,64 @@ func resourceMongoDBAtlasAdvancedClusterRead(ctx context.Context, d *schema.Reso return nil } +func resourceMongoDBAtlasAdvancedClusterUpdateOrUpgrade(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + upgradeRequest := getUpgradeRequest(d) + + if upgradeRequest != nil { + upgradeCtx := context.WithValue(ctx, upgradeRequestCtx, upgradeRequest) + return resourceMongoDBAtlasAdvancedClusterUpgrade(upgradeCtx, d, meta) + } + + return resourceMongoDBAtlasAdvancedClusterUpdate(ctx, d, meta) +} + +func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + conn := meta.(*MongoDBClient).Atlas + ids := decodeStateID(d.Id()) + projectID := ids["project_id"] + clusterName := ids["cluster_name"] + + var upgradeResponse *matlas.Cluster + var err error + upgradeRequest := ctx.Value(upgradeRequestCtx).(*matlas.Cluster) + + if upgradeRequest == nil { + return diag.FromErr(fmt.Errorf("Upgrade called without an %s in ctx.", upgradeRequestCtx)) + } + + err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { + upgradeResponse, _, err = upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) + if err != nil { + var target *matlas.ErrorResponse + if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { + clusterRequest := &matlas.AdvancedCluster{ + Paused: pointy.Bool(false), + } + _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) + if err != nil { + return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + if errors.As(err, &target) && target.HTTPCode == 400 { + return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + return nil + }) + + if err != nil { + return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + + d.SetId(encodeStateID(map[string]string{ + "cluster_id": upgradeResponse.ID, + "project_id": projectID, + "cluster_name": clusterName, + })) + + return resourceMongoDBAtlasAdvancedClusterRead(ctx, d, meta) +} + func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { // Get client connection. conn := meta.(*MongoDBClient).Atlas @@ -568,33 +627,6 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re cluster.VersionReleaseSystem = d.Get("version_release_system").(string) } - upgradeRequest := getUpgradeRequest(d) - - if upgradeRequest != nil { - err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - _, _, err := upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) - if err != nil { - var target *matlas.ErrorResponse - if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { - clusterRequest := &matlas.AdvancedCluster{ - Paused: pointy.Bool(false), - } - _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) - if err != nil { - return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - if errors.As(err, &target) && target.HTTPCode == 400 { - return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - return nil - }) - if err != nil { - return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - // Has changes if !reflect.DeepEqual(cluster, matlas.Cluster{}) { err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { @@ -1133,6 +1165,7 @@ func getUpgradeRequest(d *schema.ResourceData) *matlas.Cluster { ProviderSettings: &matlas.ProviderSettings{ ProviderName: nRegionConfig.ProviderName, InstanceSizeName: nRegionConfig.ElectableSpecs.InstanceSize, + RegionName: nRegionConfig.RegionName, }, } } diff --git a/website/docs/r/advanced_cluster.html.markdown b/website/docs/r/advanced_cluster.html.markdown index 5c39f82532..71330f57df 100644 --- a/website/docs/r/advanced_cluster.html.markdown +++ b/website/docs/r/advanced_cluster.html.markdown @@ -15,6 +15,9 @@ More information on considerations for using advanced clusters please see [Consi ~> **IMPORTANT:**
• The primary difference between [`mongodbatlas_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster) and [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster) is that `mongodbatlas_advanced_cluster` supports multi-cloud clusters. We recommend new users start with the `mongodbatlas_advanced_cluster` resource. +
• Shared tier clusters are supported for single-cloud, see the [Example Tenant Cluster](#Example-Tenant-Cluster) below. +
• Shared tier cluster tenant upgrade is now supported as well. Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, see the [Example Tenant Cluster Upgrade](#Example-Tenant-Cluster-Upgrade) below. +
• WARNING! On shared tier cluster tenant upgrade, *only* the upgrade changes will be applied. This is done in-order to avoid a corrupt state file in the event that the upgrade succeeds, but subsequent updates fail within the same `terraform apply`. In order to apply any other cluster changes, run a secondary `terraform apply` after the upgrade succeeds. -> **NOTE:** Groups and projects are synonymous terms. You may find group_id in the official documentation. -> **NOTE:** A network container is created for a advanced cluster to reside in if one does not yet exist in the project. To use this automatically created container with another resource, such as peering, the `container_id` is exported after creation. @@ -69,6 +72,27 @@ resource "mongodbatlas_advanced_cluster" "test" { } ``` +### Example Tenant Cluster Upgrade + +```terraform +resource "mongodbatlas_advanced_cluster" "test" { + project_id = "PROJECT ID" + name = "NAME OF CLUSTER" + cluster_type = "REPLICASET" + + replication_specs { + region_configs { + electable_specs { + instance_size = "M10" + } + provider_name = "AWS" + region_name = "US_EAST_1" + priority = 1 + } + } +} +``` + ### Example Multicloud. ```terraform diff --git a/website/docs/r/cluster.html.markdown b/website/docs/r/cluster.html.markdown index 131cd24cdf..93ce7ff6c8 100644 --- a/website/docs/r/cluster.html.markdown +++ b/website/docs/r/cluster.html.markdown @@ -23,7 +23,7 @@ provider_instance_size_name = "M0" provider_name = "TENANT" backing_provider_name = "AWS" ``` -
• Shared tier clusters can now be upgraded to higher tiers via API or by this Provider. WARNING! Any change from shared tier to a different instance size will be considered a tenant upgrade. If upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, ex: +
• Shared tier clusters can now be upgraded to higher tiers via API or by this Provider. WARNING! Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, ex: ``` provider_instance_size_name = "M10" provider_name = "AWS" From 23f5f7702b9c650736ff6e858494a92be546dc0e Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 27 Oct 2022 15:45:30 -0500 Subject: [PATCH 12/27] Fixed linter issues with advanced cluster --- .../resource_mongodbatlas_advanced_cluster.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index 7686495e04..2ba90e2971 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -21,6 +21,8 @@ import ( matlas "go.mongodb.org/atlas/mongodbatlas" ) +type acCtxKey string + const ( errorClusterAdvancedCreate = "error creating MongoDB ClusterAdvanced: %s" errorClusterAdvancedRead = "error reading MongoDB ClusterAdvanced (%s): %s" @@ -29,9 +31,10 @@ const ( errorClusterAdvancedSetting = "error setting `%s` for MongoDB ClusterAdvanced (%s): %s" errorAdvancedClusterAdvancedConfUpdate = "error updating Advanced Configuration Option form MongoDB Cluster (%s): %s" errorAdvancedClusterAdvancedConfRead = "error reading Advanced Configuration Option form MongoDB Cluster (%s): %s" - upgradeRequestCtx = "upgradeRequest" ) +var upgradeRequestCtxKey acCtxKey = "upgradeRequest" + func resourceMongoDBAtlasAdvancedCluster() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceMongoDBAtlasAdvancedClusterCreate, @@ -516,7 +519,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdateOrUpgrade(ctx context.Context, d * upgradeRequest := getUpgradeRequest(d) if upgradeRequest != nil { - upgradeCtx := context.WithValue(ctx, upgradeRequestCtx, upgradeRequest) + upgradeCtx := context.WithValue(ctx, upgradeRequestCtxKey, upgradeRequest) return resourceMongoDBAtlasAdvancedClusterUpgrade(upgradeCtx, d, meta) } @@ -531,10 +534,10 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R var upgradeResponse *matlas.Cluster var err error - upgradeRequest := ctx.Value(upgradeRequestCtx).(*matlas.Cluster) + upgradeRequest := ctx.Value(upgradeRequestCtxKey).(*matlas.Cluster) if upgradeRequest == nil { - return diag.FromErr(fmt.Errorf("Upgrade called without an %s in ctx.", upgradeRequestCtx)) + return diag.FromErr(fmt.Errorf("upgrade called without %s in ctx", string(upgradeRequestCtxKey))) } err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { From ffb1d9b8ecb9e27d67e70284305ef4fb60c61378 Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 27 Oct 2022 15:55:40 -0500 Subject: [PATCH 13/27] removed unnecessary explicit setting of cluster_id --- mongodbatlas/resource_mongodbatlas_cluster.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index a57b9d6fdd..9cd358c0bf 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -904,10 +904,10 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa if !reflect.DeepEqual(cluster, matlas.Cluster{}) { var err error var updatedCluster *matlas.Cluster - willUpgrade := isUpgradeRequired(d) + isUpgrade := isUpgradeRequired(d) err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - if willUpgrade { + if isUpgrade { updatedCluster, _, err = upgradeCluster(ctx, conn, cluster, projectID, clusterName) } else { updatedCluster, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) @@ -933,10 +933,7 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err)) } - if willUpgrade { - if err := d.Set("cluster_id", updatedCluster.ID); err != nil { - return diag.FromErr(fmt.Errorf(errorClusterSetting, "cluster_id", clusterName, err)) - } + if isUpgrade { d.SetId(encodeStateID(map[string]string{ "cluster_id": updatedCluster.ID, "project_id": projectID, From c48654bb281c7b1f758026e9ebf9edfb31cd90d7 Mon Sep 17 00:00:00 2001 From: Dosty Date: Thu, 27 Oct 2022 15:57:01 -0500 Subject: [PATCH 14/27] Updated new examples to work with tf 0.13 --- examples/atlas-advanced-cluster/tenant-upgrade/versions.tf | 2 +- examples/atlas-cluster/tenant-upgrade/versions.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/versions.tf b/examples/atlas-advanced-cluster/tenant-upgrade/versions.tf index 1a4117cfda..92fca3b63d 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/versions.tf +++ b/examples/atlas-advanced-cluster/tenant-upgrade/versions.tf @@ -4,5 +4,5 @@ terraform { source = "mongodb/mongodbatlas" } } - required_version = ">= 0.14" + required_version = ">= 0.13" } \ No newline at end of file diff --git a/examples/atlas-cluster/tenant-upgrade/versions.tf b/examples/atlas-cluster/tenant-upgrade/versions.tf index 1a4117cfda..92fca3b63d 100644 --- a/examples/atlas-cluster/tenant-upgrade/versions.tf +++ b/examples/atlas-cluster/tenant-upgrade/versions.tf @@ -4,5 +4,5 @@ terraform { source = "mongodb/mongodbatlas" } } - required_version = ">= 0.14" + required_version = ">= 0.13" } \ No newline at end of file From 2643228f8ea0085a532e755fb827f044869d7a71 Mon Sep 17 00:00:00 2001 From: Dosty Everts Date: Mon, 31 Oct 2022 09:51:03 -0500 Subject: [PATCH 15/27] Update website/docs/r/advanced_cluster.html.markdown Co-authored-by: Melissa Plunkett --- website/docs/r/advanced_cluster.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/advanced_cluster.html.markdown b/website/docs/r/advanced_cluster.html.markdown index 71330f57df..0d3af11f19 100644 --- a/website/docs/r/advanced_cluster.html.markdown +++ b/website/docs/r/advanced_cluster.html.markdown @@ -16,7 +16,7 @@ More information on considerations for using advanced clusters please see [Consi
• The primary difference between [`mongodbatlas_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster) and [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster) is that `mongodbatlas_advanced_cluster` supports multi-cloud clusters. We recommend new users start with the `mongodbatlas_advanced_cluster` resource.
• Shared tier clusters are supported for single-cloud, see the [Example Tenant Cluster](#Example-Tenant-Cluster) below. -
• Shared tier cluster tenant upgrade is now supported as well. Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, see the [Example Tenant Cluster Upgrade](#Example-Tenant-Cluster-Upgrade) below. +
• Upgrading the shared tier is supported. Any change from a shared tier cluster, aka tenant, to a different instance size will be considered a tenant upgrade. When upgrading from the shared tier, change the `provider_name` from "TENANT" to your preferred provider (AWS, GCP or Azure) and remove the variable `backing_provider_name`. See the [Example Tenant Cluster Upgrade](#Example-Tenant-Cluster-Upgrade) below. Note you can upgrade a shared tier cluster only to a single provider M10 or greater.
• WARNING! On shared tier cluster tenant upgrade, *only* the upgrade changes will be applied. This is done in-order to avoid a corrupt state file in the event that the upgrade succeeds, but subsequent updates fail within the same `terraform apply`. In order to apply any other cluster changes, run a secondary `terraform apply` after the upgrade succeeds. -> **NOTE:** Groups and projects are synonymous terms. You may find group_id in the official documentation. From 42a8b2c292453d2cc12103d428fef512a6f99319 Mon Sep 17 00:00:00 2001 From: Dosty Everts Date: Mon, 31 Oct 2022 09:51:18 -0500 Subject: [PATCH 16/27] Update website/docs/r/advanced_cluster.html.markdown Co-authored-by: Melissa Plunkett --- website/docs/r/advanced_cluster.html.markdown | 1 - 1 file changed, 1 deletion(-) diff --git a/website/docs/r/advanced_cluster.html.markdown b/website/docs/r/advanced_cluster.html.markdown index 0d3af11f19..687806b92a 100644 --- a/website/docs/r/advanced_cluster.html.markdown +++ b/website/docs/r/advanced_cluster.html.markdown @@ -15,7 +15,6 @@ More information on considerations for using advanced clusters please see [Consi ~> **IMPORTANT:**
• The primary difference between [`mongodbatlas_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster) and [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster) is that `mongodbatlas_advanced_cluster` supports multi-cloud clusters. We recommend new users start with the `mongodbatlas_advanced_cluster` resource. -
• Shared tier clusters are supported for single-cloud, see the [Example Tenant Cluster](#Example-Tenant-Cluster) below.
• Upgrading the shared tier is supported. Any change from a shared tier cluster, aka tenant, to a different instance size will be considered a tenant upgrade. When upgrading from the shared tier, change the `provider_name` from "TENANT" to your preferred provider (AWS, GCP or Azure) and remove the variable `backing_provider_name`. See the [Example Tenant Cluster Upgrade](#Example-Tenant-Cluster-Upgrade) below. Note you can upgrade a shared tier cluster only to a single provider M10 or greater.
• WARNING! On shared tier cluster tenant upgrade, *only* the upgrade changes will be applied. This is done in-order to avoid a corrupt state file in the event that the upgrade succeeds, but subsequent updates fail within the same `terraform apply`. In order to apply any other cluster changes, run a secondary `terraform apply` after the upgrade succeeds. -> **NOTE:** Groups and projects are synonymous terms. You may find group_id in the official documentation. From 16e7e5fd6030199651c3a113fc521c854e132386 Mon Sep 17 00:00:00 2001 From: Dosty Everts Date: Mon, 31 Oct 2022 09:51:49 -0500 Subject: [PATCH 17/27] Update website/docs/r/advanced_cluster.html.markdown Co-authored-by: Melissa Plunkett --- website/docs/r/advanced_cluster.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/advanced_cluster.html.markdown b/website/docs/r/advanced_cluster.html.markdown index 687806b92a..d58500d2f7 100644 --- a/website/docs/r/advanced_cluster.html.markdown +++ b/website/docs/r/advanced_cluster.html.markdown @@ -16,7 +16,7 @@ More information on considerations for using advanced clusters please see [Consi
• The primary difference between [`mongodbatlas_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster) and [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster) is that `mongodbatlas_advanced_cluster` supports multi-cloud clusters. We recommend new users start with the `mongodbatlas_advanced_cluster` resource.
• Upgrading the shared tier is supported. Any change from a shared tier cluster, aka tenant, to a different instance size will be considered a tenant upgrade. When upgrading from the shared tier, change the `provider_name` from "TENANT" to your preferred provider (AWS, GCP or Azure) and remove the variable `backing_provider_name`. See the [Example Tenant Cluster Upgrade](#Example-Tenant-Cluster-Upgrade) below. Note you can upgrade a shared tier cluster only to a single provider M10 or greater. -
• WARNING! On shared tier cluster tenant upgrade, *only* the upgrade changes will be applied. This is done in-order to avoid a corrupt state file in the event that the upgrade succeeds, but subsequent updates fail within the same `terraform apply`. In order to apply any other cluster changes, run a secondary `terraform apply` after the upgrade succeeds. +
• WARNING WHEN UPGRADING TENANT/SHARED CLUSTERS!!! When upgrading from the shared tier *only* the upgrade changes will be applied. This is done in-order to avoid a corrupt state file in the event that the upgrade succeeds, but subsequent updates fail within the same `terraform apply`. In order to apply any other cluster changes, run a secondary `terraform apply` after the upgrade succeeds. -> **NOTE:** Groups and projects are synonymous terms. You may find group_id in the official documentation. -> **NOTE:** A network container is created for a advanced cluster to reside in if one does not yet exist in the project. To use this automatically created container with another resource, such as peering, the `container_id` is exported after creation. From 4b4610ad0af671a155da7390c5d9df591ce7c745 Mon Sep 17 00:00:00 2001 From: Dosty Everts Date: Mon, 31 Oct 2022 09:52:14 -0500 Subject: [PATCH 18/27] Update website/docs/r/cluster.html.markdown Co-authored-by: Melissa Plunkett --- website/docs/r/cluster.html.markdown | 6 ------ 1 file changed, 6 deletions(-) diff --git a/website/docs/r/cluster.html.markdown b/website/docs/r/cluster.html.markdown index 93ce7ff6c8..1d377178af 100644 --- a/website/docs/r/cluster.html.markdown +++ b/website/docs/r/cluster.html.markdown @@ -17,12 +17,6 @@ description: |- ~> **IMPORTANT:**
• New Users: If you are not already using `mongodbatlas_cluster` for your deployment we recommend starting with the [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster). `mongodbatlas_advanced_cluster` has all the same functionality as `mongodbatlas_cluster` but also supports multi-cloud clusters.
• Free tier cluster creation (M0) is supported. -
• Shared tier clusters (M0, M2, M5) are supported. -``` -provider_instance_size_name = "M0" -provider_name = "TENANT" -backing_provider_name = "AWS" -```
• Shared tier clusters can now be upgraded to higher tiers via API or by this Provider. WARNING! Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, ex: ``` provider_instance_size_name = "M10" From 98e3c4a10ef64f22dea28cda5448f268c0fd5228 Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 31 Oct 2022 09:53:36 -0500 Subject: [PATCH 19/27] Addressed some criticisms of new tenant-upgrade examples --- examples/atlas-advanced-cluster/tenant-upgrade/main.tf | 6 ------ examples/atlas-advanced-cluster/tenant-upgrade/variables.tf | 6 +----- examples/atlas-cluster/tenant-upgrade/variables.tf | 2 +- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/main.tf b/examples/atlas-advanced-cluster/tenant-upgrade/main.tf index 21f246c7a3..8ee986e7e2 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/main.tf +++ b/examples/atlas-advanced-cluster/tenant-upgrade/main.tf @@ -14,7 +14,6 @@ resource "mongodbatlas_advanced_cluster" "cluster" { region_configs { electable_specs { instance_size = var.provider_instance_size_name - node_count = var.node_count } provider_name = var.provider_name backing_provider_name = var.backing_provider_name @@ -27,9 +26,4 @@ resource "mongodbatlas_advanced_cluster" "cluster" { resource "mongodbatlas_project" "project" { name = "TenantUpgradeTest" org_id = var.atlas_org_id -} - -output "project_name" { - value = mongodbatlas_project.project.name - description = "(Expected) Name of the MongoDB Atlas Cluster" } \ No newline at end of file diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf b/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf index 4aacaf4a8e..2986570c19 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf +++ b/examples/atlas-advanced-cluster/tenant-upgrade/variables.tf @@ -1,5 +1,5 @@ variable "atlas_org_id" { - description = "Atlas orgnization id" + description = "Atlas organization id" default = "" } variable "public_key" { @@ -19,8 +19,4 @@ variable "backing_provider_name" { variable "provider_instance_size_name" { description = "Atlas cluster provider instance name" default = "M10" -} -variable "node_count" { - description = "Atlas cluster node count" - default = null } \ No newline at end of file diff --git a/examples/atlas-cluster/tenant-upgrade/variables.tf b/examples/atlas-cluster/tenant-upgrade/variables.tf index 0cfb025cda..2986570c19 100644 --- a/examples/atlas-cluster/tenant-upgrade/variables.tf +++ b/examples/atlas-cluster/tenant-upgrade/variables.tf @@ -1,5 +1,5 @@ variable "atlas_org_id" { - description = "Atlas orgnization id" + description = "Atlas organization id" default = "" } variable "public_key" { From 155354f8844df161895ca0d9bafa245e0fbd9559 Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 31 Oct 2022 09:57:05 -0500 Subject: [PATCH 20/27] Applied suggested docs changes for cluster tenant upgrade --- website/docs/r/cluster.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/website/docs/r/cluster.html.markdown b/website/docs/r/cluster.html.markdown index 1d377178af..0b891f5faa 100644 --- a/website/docs/r/cluster.html.markdown +++ b/website/docs/r/cluster.html.markdown @@ -17,11 +17,17 @@ description: |- ~> **IMPORTANT:**
• New Users: If you are not already using `mongodbatlas_cluster` for your deployment we recommend starting with the [`mongodbatlas_advanced_cluster`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/advanced_cluster). `mongodbatlas_advanced_cluster` has all the same functionality as `mongodbatlas_cluster` but also supports multi-cloud clusters.
• Free tier cluster creation (M0) is supported. -
• Shared tier clusters can now be upgraded to higher tiers via API or by this Provider. WARNING! Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier, change the `provider_name` from "TENANT" and to your preferred provider, remove `backing_provider_name`, ex: +
• Shared tier clusters (M0, M2, M5) can be upgraded to dedicated tiers (M10+) via this provider. WARNING WHEN UPGRADING TENANT/SHARED CLUSTERS!!! Any change from shared tier to a different instance size will be considered a tenant upgrade. When upgrading from shared tier to dedicated simply change the `provider_name` from "TENANT" to your preferred provider (AWS, GCP, AZURE) and remove the variable `backing_provider_name`, for example if you have an existing tenant/shared cluster and want to upgrade your Terraform config should be changed from: +``` +provider_instance_size_name = "M0" +provider_name = "TENANT" +backing_provider_name = "AWS" +``` +To: ``` provider_instance_size_name = "M10" provider_name = "AWS" -``` +```
• Changes to cluster configurations can affect costs. Before making changes, please see [Billing](https://docs.atlas.mongodb.com/billing/).
• If your Atlas project contains a custom role that uses actions introduced in a specific MongoDB version, you cannot create a cluster with a MongoDB version less than that version unless you delete the custom role. From 34b926637cb70ed6a6c96655217236d8697e3d2c Mon Sep 17 00:00:00 2001 From: Dosty Everts Date: Mon, 31 Oct 2022 09:59:42 -0500 Subject: [PATCH 21/27] Apply suggestions from code review Applied docs changes suggested for advanced-cluster tenant upgrade readme Co-authored-by: Melissa Plunkett --- .../atlas-advanced-cluster/tenant-upgrade/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/README.md b/examples/atlas-advanced-cluster/tenant-upgrade/README.md index 5c50b60d0f..ff64f4055b 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/README.md +++ b/examples/atlas-advanced-cluster/tenant-upgrade/README.md @@ -1,13 +1,13 @@ # MongoDB Atlas Provider -- Advanced Cluster Tenant Upgrade -This example creates a project and cluster. It is intended to show how to upgrade from shared to dedicated tier. +This example creates a project and cluster. It is intended to show how to upgrade from shared, aka tenant, to dedicated tier. Variables Required: -- `atlas_org_id`: ID of atlas organization -- `public_key`: Atlas provider public_key -- `private_key`: Atlas provider private key +- `atlas_org_id`: ID of the Atlas organization +- `public_key`: Atlas public key +- `private_key`: Atlas private key - `provider_name`: Name of provider to use for cluster (TENANT, AWS, GCP) - `backing_provider_name`: If provider_name is tenant, the backing provider (AWS, GCP) -- `provider_instance_size_name`: Size of the cluster (M0, M2, M5, M10, etc...) +- `provider_instance_size_name`: Size of the cluster (Shared: M0, M2, M5, Dedicated: M10+.) For this example, first we'll start out on the shared tier, then upgrade to a dedicated tier. @@ -23,7 +23,7 @@ backing_provider_name = "AWS" provider_instance_size_name = "M2" ``` -Apply with the following `terraform.tfvars` to upgrade to dedicated tier: +Apply with the following `terraform.tfvars` to upgrade the shared tier cluster you just created to dedicated tier; replacing the org id, public and private key with your values: ``` atlas_org_id = "627a9687f7f7f7f774de306f14" public_key = From fd6db2c567885d85e51bdc7f35b8b2c5b8af7bfb Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 31 Oct 2022 10:06:47 -0500 Subject: [PATCH 22/27] Updated cluster tenant upgrade readme to match that of the advanced cluster --- .../tenant-upgrade/README.md | 4 ++-- examples/atlas-cluster/tenant-upgrade/README.md | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/README.md b/examples/atlas-advanced-cluster/tenant-upgrade/README.md index ff64f4055b..0ddd5e430f 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/README.md +++ b/examples/atlas-advanced-cluster/tenant-upgrade/README.md @@ -11,7 +11,7 @@ Variables Required: For this example, first we'll start out on the shared tier, then upgrade to a dedicated tier. -As such, utilize the following example `terraform.tfvars` and pseudo-code to execute a working example: +Utilize the following to execute a working example, replacing the org id, public and private key with your values: Apply with the following `terraform.tfvars` for shared tier cluster: ``` @@ -23,7 +23,7 @@ backing_provider_name = "AWS" provider_instance_size_name = "M2" ``` -Apply with the following `terraform.tfvars` to upgrade the shared tier cluster you just created to dedicated tier; replacing the org id, public and private key with your values: +Apply with the following `terraform.tfvars` to upgrade the shared tier cluster you just created to dedicated tier: ``` atlas_org_id = "627a9687f7f7f7f774de306f14" public_key = diff --git a/examples/atlas-cluster/tenant-upgrade/README.md b/examples/atlas-cluster/tenant-upgrade/README.md index 55d1a7d52b..baac0a14ad 100644 --- a/examples/atlas-cluster/tenant-upgrade/README.md +++ b/examples/atlas-cluster/tenant-upgrade/README.md @@ -1,17 +1,18 @@ # MongoDB Atlas Provider -- Cluster Tenant Upgrade -This example creates a project and cluster. It is intended to show how to upgrade from shared to dedicated tier. +This example creates a project and cluster. It is intended to show how to upgrade from shared, aka tenant, to dedicated tier. Variables Required: -- `atlas_org_id`: ID of atlas organization -- `public_key`: Atlas provider public_key -- `private_key`: Atlas provider private key +- `atlas_org_id`: ID of the Atlas organization +- `public_key`: Atlas public key +- `private_key`: Atlas private key - `provider_name`: Name of provider to use for cluster (TENANT, AWS, GCP) - `backing_provider_name`: If provider_name is tenant, the backing provider (AWS, GCP) -- `provider_instance_size_name`: Size of the cluster (M0, M2, M5, M10, etc...) +- `provider_instance_size_name`: Size of the cluster (Shared: M0, M2, M5, Dedicated: M10+.) For this example, first we'll start out on the shared tier, then upgrade to a dedicated tier. -As such, utilize the following example `terraform.tfvars` and pseudo-code to execute a working example: + +Utilize the following to execute a working example, replacing the org id, public and private key with your values: Apply with the following `terraform.tfvars` for shared tier cluster: ``` @@ -23,7 +24,7 @@ backing_provider_name = "AWS" provider_instance_size_name = "M2" ``` -Apply with the following `terraform.tfvars` to upgrade to dedicated tier: +Apply with the following `terraform.tfvars` to upgrade the shared tier cluster you just created to dedicated tier: ``` atlas_org_id = "627a9687f7f7f7f774de306f14" public_key = From baf23fe4edd5dfc8964b6232268518209860d4e6 Mon Sep 17 00:00:00 2001 From: Dosty Date: Mon, 31 Oct 2022 10:54:05 -0500 Subject: [PATCH 23/27] Addressed last remaining README suggestions for new examples --- examples/atlas-advanced-cluster/tenant-upgrade/README.md | 2 +- examples/atlas-cluster/tenant-upgrade/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/atlas-advanced-cluster/tenant-upgrade/README.md b/examples/atlas-advanced-cluster/tenant-upgrade/README.md index 0ddd5e430f..5051df631b 100644 --- a/examples/atlas-advanced-cluster/tenant-upgrade/README.md +++ b/examples/atlas-advanced-cluster/tenant-upgrade/README.md @@ -13,7 +13,7 @@ For this example, first we'll start out on the shared tier, then upgrade to a de Utilize the following to execute a working example, replacing the org id, public and private key with your values: -Apply with the following `terraform.tfvars` for shared tier cluster: +Apply with the following `terraform.tfvars` to first create a shared tier cluster: ``` atlas_org_id = "627a9687f7f7f7f774de306f14" public_key = diff --git a/examples/atlas-cluster/tenant-upgrade/README.md b/examples/atlas-cluster/tenant-upgrade/README.md index baac0a14ad..34c91e11a5 100644 --- a/examples/atlas-cluster/tenant-upgrade/README.md +++ b/examples/atlas-cluster/tenant-upgrade/README.md @@ -14,7 +14,7 @@ For this example, first we'll start out on the shared tier, then upgrade to a de Utilize the following to execute a working example, replacing the org id, public and private key with your values: -Apply with the following `terraform.tfvars` for shared tier cluster: +Apply with the following `terraform.tfvars` to first create a shared tier cluster: ``` atlas_org_id = "627a9687f7f7f7f774de306f14" public_key = From cdad1a8f408617d76ee32480020eb222dbc3e1c0 Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 1 Nov 2022 11:47:10 -0500 Subject: [PATCH 24/27] Fixed naming on variables and re-pausing cluster after upgrade if necessary --- .../resource_mongodbatlas_advanced_cluster.go | 41 +++++++++++-------- mongodbatlas/resource_mongodbatlas_cluster.go | 12 +++--- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index 2ba90e2971..0016ee4993 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -516,9 +516,7 @@ func resourceMongoDBAtlasAdvancedClusterRead(ctx context.Context, d *schema.Reso } func resourceMongoDBAtlasAdvancedClusterUpdateOrUpgrade(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - upgradeRequest := getUpgradeRequest(d) - - if upgradeRequest != nil { + if upgradeRequest := getUpgradeRequest(d); upgradeRequest != nil { upgradeCtx := context.WithValue(ctx, upgradeRequestCtxKey, upgradeRequest) return resourceMongoDBAtlasAdvancedClusterUpgrade(upgradeCtx, d, meta) } @@ -570,6 +568,17 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R "cluster_name": clusterName, })) + if d.Get("paused").(bool) { + clusterRequest := &matlas.AdvancedCluster{ + Paused: pointy.Bool(true), + } + + _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) + if err != nil { + return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) + } + } + return resourceMongoDBAtlasAdvancedClusterRead(ctx, d, meta) } @@ -1146,29 +1155,29 @@ func getUpgradeRequest(d *schema.ResourceData) *matlas.Cluster { return nil } - crs, nrs := d.GetChange("replication_specs") - cReplicationSpecs := expandAdvancedReplicationSpecs(crs.(*schema.Set).List()) - nReplicationSpecs := expandAdvancedReplicationSpecs(nrs.(*schema.Set).List()) + cs, ns := d.GetChange("replication_specs") + currentSpecs := expandAdvancedReplicationSpecs(cs.(*schema.Set).List()) + updatedSpecs := expandAdvancedReplicationSpecs(ns.(*schema.Set).List()) - if len(cReplicationSpecs) != 1 || len(nReplicationSpecs) != 1 || len(cReplicationSpecs[0].RegionConfigs) != 1 || len(nReplicationSpecs[0].RegionConfigs) != 1 { + if len(currentSpecs) != 1 || len(updatedSpecs) != 1 || len(currentSpecs[0].RegionConfigs) != 1 || len(updatedSpecs[0].RegionConfigs) != 1 { return nil } - cRegionConfig := cReplicationSpecs[0].RegionConfigs[0] - nRegionConfig := nReplicationSpecs[0].RegionConfigs[0] - cInstanceSize := cRegionConfig.ElectableSpecs.InstanceSize + currentRegion := currentSpecs[0].RegionConfigs[0] + updatedRegion := updatedSpecs[0].RegionConfigs[0] + currentSize := currentRegion.ElectableSpecs.InstanceSize - if cRegionConfig.ElectableSpecs.InstanceSize == nRegionConfig.ElectableSpecs.InstanceSize || !(cInstanceSize == "M0" || - cInstanceSize == "M2" || - cInstanceSize == "M5") { + if currentRegion.ElectableSpecs.InstanceSize == updatedRegion.ElectableSpecs.InstanceSize || !(currentSize == "M0" || + currentSize == "M2" || + currentSize == "M5") { return nil } return &matlas.Cluster{ ProviderSettings: &matlas.ProviderSettings{ - ProviderName: nRegionConfig.ProviderName, - InstanceSizeName: nRegionConfig.ElectableSpecs.InstanceSize, - RegionName: nRegionConfig.RegionName, + ProviderName: updatedRegion.ProviderName, + InstanceSizeName: updatedRegion.ElectableSpecs.InstanceSize, + RegionName: updatedRegion.RegionName, }, } } diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 9cd358c0bf..5b4c9ccbca 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -1233,13 +1233,13 @@ func flattenProviderSettings(d *schema.ResourceData, settings *matlas.ProviderSe } func isUpgradeRequired(d *schema.ResourceData) bool { - pInstance, nInstance := d.GetChange("provider_instance_size_name") + currentSize, updatedSize := d.GetChange("provider_instance_size_name") - if pInstance == nInstance { + if currentSize == updatedSize { return false } - return pInstance == "M0" || pInstance == "M2" || pInstance == "M5" + return currentSize == "M0" || currentSize == "M2" || currentSize == "M5" } func expandReplicationSpecs(d *schema.ResourceData) ([]matlas.ReplicationSpec, error) { @@ -1452,10 +1452,10 @@ func resourceClusterRefreshFunc(ctx context.Context, name, projectID string, cli func resourceClusterCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { var err error - pName, nName := d.GetChange("provider_name") + currentProvider, updatedProvider := d.GetChange("provider_name") - willProviderChange := pName != nName - willLeaveTenant := willProviderChange && pName == "TENANT" + willProviderChange := currentProvider != updatedProvider + willLeaveTenant := willProviderChange && currentProvider == "TENANT" if willLeaveTenant { err = d.SetNewComputed("backing_provider_name") From 07e953b13e106eafff62743f68260ba7e637d61d Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 1 Nov 2022 12:42:14 -0500 Subject: [PATCH 25/27] Reduced nested ifs in cluster update --- .../resource_mongodbatlas_advanced_cluster.go | 4 +-- mongodbatlas/resource_mongodbatlas_cluster.go | 35 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index 0016ee4993..f4d231a3f1 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -1155,9 +1155,9 @@ func getUpgradeRequest(d *schema.ResourceData) *matlas.Cluster { return nil } - cs, ns := d.GetChange("replication_specs") + cs, us := d.GetChange("replication_specs") currentSpecs := expandAdvancedReplicationSpecs(cs.(*schema.Set).List()) - updatedSpecs := expandAdvancedReplicationSpecs(ns.(*schema.Set).List()) + updatedSpecs := expandAdvancedReplicationSpecs(us.(*schema.Set).List()) if len(currentSpecs) != 1 || len(updatedSpecs) != 1 || len(currentSpecs[0].RegionConfigs) != 1 || len(updatedSpecs[0].RegionConfigs) != 1 { return nil diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 5b4c9ccbca..f7780598a6 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -912,23 +912,22 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } else { updatedCluster, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) } - if err != nil { - var target *matlas.ErrorResponse - if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { - clusterRequest := &matlas.Cluster{ - Paused: pointy.Bool(false), - } - _, _, err := updateCluster(ctx, conn, clusterRequest, projectID, clusterName) - if err != nil { - return resource.NonRetryableError(fmt.Errorf(errorClusterUpdate, clusterName, err)) - } - } - if errors.As(err, &target) && target.HTTPCode == 400 { - return resource.NonRetryableError(fmt.Errorf(errorClusterUpdate, clusterName, err)) + + if didErrOnPausedCluster(err) { + clusterRequest := &matlas.Cluster{ + Paused: pointy.Bool(false), } + + _, _, err = updateCluster(ctx, conn, clusterRequest, projectID, clusterName) + } + + if err != nil { + return resource.NonRetryableError(fmt.Errorf(errorClusterUpdate, clusterName, err)) } + return nil }) + if err != nil { return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err)) } @@ -973,6 +972,16 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa return resourceMongoDBAtlasClusterRead(ctx, d, meta) } +func didErrOnPausedCluster(err error) bool { + if err == nil { + return false + } + + var target *matlas.ErrorResponse + + return errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" +} + func resourceMongoDBAtlasClusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { // Get client connection. conn := meta.(*MongoDBClient).Atlas From 6b9f9cf73d635ca6af3f98e4a43c6a997dec63ee Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 1 Nov 2022 21:54:08 -0500 Subject: [PATCH 26/27] No longer attempting to unpause tenant tier clusters prior to upgrade. Any updates fail --- .../resource_mongodbatlas_advanced_cluster.go | 31 +------------ mongodbatlas/resource_mongodbatlas_cluster.go | 43 +++++++++---------- 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index f4d231a3f1..97d6a9a253 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -538,25 +538,7 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R return diag.FromErr(fmt.Errorf("upgrade called without %s in ctx", string(upgradeRequestCtxKey))) } - err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - upgradeResponse, _, err = upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) - if err != nil { - var target *matlas.ErrorResponse - if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" { - clusterRequest := &matlas.AdvancedCluster{ - Paused: pointy.Bool(false), - } - _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) - if err != nil { - return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - if errors.As(err, &target) && target.HTTPCode == 400 { - return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - return nil - }) + upgradeResponse, _, err = upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) if err != nil { return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) @@ -568,17 +550,6 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R "cluster_name": clusterName, })) - if d.Get("paused").(bool) { - clusterRequest := &matlas.AdvancedCluster{ - Paused: pointy.Bool(true), - } - - _, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName) - if err != nil { - return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) - } - } - return resourceMongoDBAtlasAdvancedClusterRead(ctx, d, meta) } diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index f7780598a6..78ef5adb8b 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -900,18 +900,24 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } } - // Has changes - if !reflect.DeepEqual(cluster, matlas.Cluster{}) { - var err error - var updatedCluster *matlas.Cluster - isUpgrade := isUpgradeRequired(d) - - err = resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { - if isUpgrade { - updatedCluster, _, err = upgradeCluster(ctx, conn, cluster, projectID, clusterName) - } else { - updatedCluster, _, err = updateCluster(ctx, conn, cluster, projectID, clusterName) - } + var didUnpauseCluster bool = false + + if isUpgradeRequired(d) { + updatedCluster, _, err := upgradeCluster(ctx, conn, cluster, projectID, clusterName) + + if err != nil { + return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err)) + } + + d.SetId(encodeStateID(map[string]string{ + "cluster_id": updatedCluster.ID, + "project_id": projectID, + "cluster_name": updatedCluster.Name, + "provider_name": updatedCluster.ProviderSettings.ProviderName, + })) + } else if !reflect.DeepEqual(cluster, matlas.Cluster{}) { + err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError { + _, _, err := updateCluster(ctx, conn, cluster, projectID, clusterName) if didErrOnPausedCluster(err) { clusterRequest := &matlas.Cluster{ @@ -919,6 +925,8 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } _, _, err = updateCluster(ctx, conn, clusterRequest, projectID, clusterName) + + didUnpauseCluster = true } if err != nil { @@ -931,15 +939,6 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa if err != nil { return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err)) } - - if isUpgrade { - d.SetId(encodeStateID(map[string]string{ - "cluster_id": updatedCluster.ID, - "project_id": projectID, - "cluster_name": updatedCluster.Name, - "provider_name": updatedCluster.ProviderSettings.ProviderName, - })) - } } /* @@ -958,7 +957,7 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } } - if d.Get("paused").(bool) { + if didUnpauseCluster { clusterRequest := &matlas.Cluster{ Paused: pointy.Bool(true), } From 785476ccc30a0c1a50f2ec39d53add1bbae60be5 Mon Sep 17 00:00:00 2001 From: Dosty Date: Tue, 1 Nov 2022 22:01:56 -0500 Subject: [PATCH 27/27] Fixed linter error on bool init --- mongodbatlas/resource_mongodbatlas_advanced_cluster.go | 4 +--- mongodbatlas/resource_mongodbatlas_cluster.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go index 97d6a9a253..0abab8f047 100644 --- a/mongodbatlas/resource_mongodbatlas_advanced_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_advanced_cluster.go @@ -530,15 +530,13 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R projectID := ids["project_id"] clusterName := ids["cluster_name"] - var upgradeResponse *matlas.Cluster - var err error upgradeRequest := ctx.Value(upgradeRequestCtxKey).(*matlas.Cluster) if upgradeRequest == nil { return diag.FromErr(fmt.Errorf("upgrade called without %s in ctx", string(upgradeRequestCtxKey))) } - upgradeResponse, _, err = upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) + upgradeResponse, _, err := upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName) if err != nil { return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err)) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 78ef5adb8b..47d208c5c2 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -900,7 +900,7 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa } } - var didUnpauseCluster bool = false + var didUnpauseCluster = false if isUpgradeRequired(d) { updatedCluster, _, err := upgradeCluster(ctx, conn, cluster, projectID, clusterName)