Skip to content

Commit

Permalink
fix: avoid including provider_disk_type_name property in cluster upda…
Browse files Browse the repository at this point in the history
…te request if attribute was removed (#1508)

* fix: avoid including provider_disk_type_name property in cluster update request if attribute was removed

* acceptance test
  • Loading branch information
AgustinBettati authored Oct 9, 2023
1 parent fb031dd commit f14f602
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
15 changes: 8 additions & 7 deletions mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}

Expand Down
60 changes: 53 additions & 7 deletions mongodbatlas/resource_mongodbatlas_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit f14f602

Please sign in to comment.