From f14f602d38e65393dcad91ddf311f20027f30f1c Mon Sep 17 00:00:00 2001 From: Agustin Bettati Date: Mon, 9 Oct 2023 09:58:36 +0200 Subject: [PATCH] fix: avoid including provider_disk_type_name property in cluster update request if attribute was removed (#1508) * fix: avoid including provider_disk_type_name property in cluster update request if attribute was removed * acceptance test --- mongodbatlas/resource_mongodbatlas_cluster.go | 15 ++--- .../resource_mongodbatlas_cluster_test.go | 60 ++++++++++++++++--- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/mongodbatlas/resource_mongodbatlas_cluster.go b/mongodbatlas/resource_mongodbatlas_cluster.go index 82e483d56a..d7d246d075 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster.go +++ b/mongodbatlas/resource_mongodbatlas_cluster.go @@ -1125,7 +1125,14 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er ProviderName: providerName, RegionName: region, VolumeType: cast.ToString(d.Get("provider_volume_type")), - DiskTypeName: cast.ToString(d.Get("provider_disk_type_name")), + } + + if d.HasChange("provider_disk_type_name") { + _, newdiskTypeName := d.GetChange("provider_disk_type_name") + diskTypeName := cast.ToString(newdiskTypeName) + if diskTypeName != "" { // ensure disk type is not included in request if attribute is removed, prevents errors in NVME intances + providerSettings.DiskTypeName = diskTypeName + } } if providerName == "TENANT" { @@ -1146,12 +1153,6 @@ func expandProviderSetting(d *schema.ResourceData) (*matlas.ProviderSettings, er providerSettings.EncryptEBSVolume = pointy.Bool(true) } - if d.Get("provider_name") == "AZURE" { - if v, ok := d.GetOk("provider_disk_type_name"); ok && !strings.Contains(providerSettings.InstanceSizeName, "NVME") { - providerSettings.DiskTypeName = cast.ToString(v) - } - } - return providerSettings, nil } diff --git a/mongodbatlas/resource_mongodbatlas_cluster_test.go b/mongodbatlas/resource_mongodbatlas_cluster_test.go index 00dc06d77c..04ea108ada 100644 --- a/mongodbatlas/resource_mongodbatlas_cluster_test.go +++ b/mongodbatlas/resource_mongodbatlas_cluster_test.go @@ -365,7 +365,7 @@ func TestAccClusterRSCluster_basicAzure(t *testing.T) { CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy, Steps: []resource.TestStep{ { - Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "true"), + Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "true", "M30", true), Check: resource.ComposeTestCheckFunc( testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster), testAccCheckMongoDBAtlasClusterAttributes(&cluster, name), @@ -377,7 +377,7 @@ func TestAccClusterRSCluster_basicAzure(t *testing.T) { ), }, { - Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "false"), + Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "false", "M30", true), Check: resource.ComposeTestCheckFunc( testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster), testAccCheckMongoDBAtlasClusterAttributes(&cluster, name), @@ -392,6 +392,49 @@ func TestAccClusterRSCluster_basicAzure(t *testing.T) { }) } +func TestAccClusterRSCluster_AzureUpdateToNVME(t *testing.T) { + var ( + cluster matlas.Cluster + resourceName = "mongodbatlas_cluster.basic_azure" + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + projectName = acctest.RandomWithPrefix("test-acc") + name = fmt.Sprintf("test-acc-%s", acctest.RandString(10)) + ) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheckBasic(t) }, + ProtoV6ProviderFactories: testAccProviderV6Factories, + CheckDestroy: testAccCheckMongoDBAtlasClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "true", "M60", true), + Check: resource.ComposeTestCheckFunc( + testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster), + testAccCheckMongoDBAtlasClusterAttributes(&cluster, name), + resource.TestCheckResourceAttrSet(resourceName, "project_id"), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "provider_instance_size_name", "M60"), + resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"), + resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"), + resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"), + ), + }, + { + Config: testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, "true", "M60_NVME", false), + Check: resource.ComposeTestCheckFunc( + testAccCheckMongoDBAtlasClusterExists(resourceName, &cluster), + testAccCheckMongoDBAtlasClusterAttributes(&cluster, name), + resource.TestCheckResourceAttrSet(resourceName, "project_id"), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "provider_instance_size_name", "M60_NVME"), + resource.TestCheckResourceAttrSet(resourceName, "mongo_uri"), + resource.TestCheckResourceAttrSet(resourceName, "replication_specs.#"), + resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.regions_config.#"), + ), + }, + }, + }) +} + func TestAccClusterRSCluster_basicGCP(t *testing.T) { var ( cluster matlas.Cluster @@ -1665,9 +1708,12 @@ resource "mongodbatlas_cluster" "advance_conf" { `, orgID, projectName, name, autoscalingEnabled, p.MinimumEnabledTLSProtocol) } -func testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, backupEnabled string) string { +func testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, backupEnabled, instanceSizeName string, includeDiskType bool) string { + var diskType string + if includeDiskType { + diskType = `provider_disk_type_name = "P6"` + } return fmt.Sprintf(` - resource "mongodbatlas_project" "cluster_project" { name = %[2]q org_id = %[1]q @@ -1692,11 +1738,11 @@ func testAccMongoDBAtlasClusterConfigAzure(orgID, projectName, name, backupEnabl // Provider Settings "block" provider_name = "AZURE" - provider_disk_type_name = "P6" - provider_instance_size_name = "M30" + %[5]s + provider_instance_size_name = %[6]q provider_region_name = "US_EAST_2" } - `, orgID, projectName, name, backupEnabled) + `, orgID, projectName, name, backupEnabled, diskType, instanceSizeName) } func testAccMongoDBAtlasClusterConfigGCP(orgID, projectName, name, backupEnabled string) string {