From 96ad2ffeff222cfaea2707c479293479f81b5f9d Mon Sep 17 00:00:00 2001 From: Tobias Schug <4014687+HappyTobi@users.noreply.github.com> Date: Thu, 16 May 2024 03:18:23 +0200 Subject: [PATCH] Including #25947 fixes #25938 * azurerm_postgresql_flexible_server: fix storage_tier calculation * Add feedback * Add storage tier test * Add feedback and more tests --- .../postgresql_flexible_server_resource.go | 14 +++ ...ostgresql_flexible_server_resource_test.go | 111 ++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 139892eaf68f..2eec141b97b3 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -414,6 +414,20 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { } if !isValid { + if strings.EqualFold(oldTierRaw.(string), newTier) { + // The tier value did not change, so we need to determin if they are + // using the default value for the tier, or they actually defined the + // tier in the config or not... If they did not define + // the tier in the config we need to assign a new valid default + // tier for the newMb value. However, if the tier is in the config + // this is a valid error and should be returned... + if v := diff.GetRawConfig().AsValueMap()["storage_tier"]; v.IsNull() { + diff.SetNew("storage_tier", string(storageTiers.DefaultTier)) + log.Printf("[DEBUG]: 'storage_tier' was not valid and was not in the config assigning new default 'storage_tier' %q -> %q\n", newTier, storageTiers.DefaultTier) + return nil + } + } + return fmt.Errorf("invalid 'storage_tier' %q for defined 'storage_mb' size '%d', expected one of [%s]", newTier, newMb, azure.QuotedStringSlice(*storageTiers.ValidTiers)) } diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 37414e552b1b..17ff0b543179 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -524,6 +524,65 @@ func TestAccPostgresqlFlexibleServer_invalidStorageTierScalingStorageMbStorageTi }) } +func TestAccPostgresqlFlexibleServer_updateOnlyWithStorageMb(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test") + r := PostgresqlFlexibleServerResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.invalidStorageTierScaling(data, "P4", "32768"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), + { + Config: r.updateOnlyWithStorageMb(data, "65536"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + }) +} + +func TestAccPostgresqlFlexibleServer_updateOnlyWithStorageTier(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test") + r := PostgresqlFlexibleServerResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.invalidStorageTierScaling(data, "P4", "32768"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), + { + Config: r.updateOnlyWithStorageTier(data, "P10"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), + { + Config: r.updateStorageTierWithoutProperty(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + // the storage tier is not changed to the default value, because p10 is still valid. + check.That(data.ResourceName).Key("storage_tier").HasValue("P10"), + ), + }, + data.ImportStep("administrator_password", "create_mode"), + { + Config: r.updateOnlyWithStorageMb(data, "262144"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("storage_tier").HasValue("P15"), + ), + }, + }) +} + func (PostgresqlFlexibleServerResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := servers.ParseFlexibleServerID(state.ID) if err != nil { @@ -551,6 +610,58 @@ resource "azurerm_resource_group" "test" { `, data.RandomInteger, data.Locations.Primary) } +func (r PostgresqlFlexibleServerResource) updateOnlyWithStorageTier(data acceptance.TestData, storageTier string) string { + return fmt.Sprintf(` +%s +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-fs-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + administrator_login = "adminTerraform" + administrator_password = "QAZwsx123" + storage_mb = 65536 + storage_tier = "%s" + version = "12" + sku_name = "GP_Standard_D2s_v3" + zone = "2" +} +`, r.template(data), data.RandomInteger, storageTier) +} + +func (r PostgresqlFlexibleServerResource) updateStorageTierWithoutProperty(data acceptance.TestData) string { + return fmt.Sprintf(` +%s +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-fs-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + administrator_login = "adminTerraform" + administrator_password = "QAZwsx123" + storage_mb = 65536 + version = "12" + sku_name = "GP_Standard_D2s_v3" + zone = "2" +} +`, r.template(data), data.RandomInteger) +} + +func (r PostgresqlFlexibleServerResource) updateOnlyWithStorageMb(data acceptance.TestData, storageMb string) string { + return fmt.Sprintf(` +%s +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-fs-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + administrator_login = "adminTerraform" + administrator_password = "QAZwsx123" + storage_mb = %s + version = "12" + sku_name = "GP_Standard_D2s_v3" + zone = "2" +} +`, r.template(data), data.RandomInteger, storageMb) +} + func (r PostgresqlFlexibleServerResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` %s