Skip to content

Commit

Permalink
Including #25947 fixes #25938
Browse files Browse the repository at this point in the history
* azurerm_postgresql_flexible_server: fix storage_tier calculation

* Add feedback

* Add storage tier test

* Add feedback and more tests
  • Loading branch information
HappyTobi authored May 16, 2024
1 parent 721935f commit 96ad2ff
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/services/postgres/postgresql_flexible_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
111 changes: 111 additions & 0 deletions internal/services/postgres/postgresql_flexible_server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 96ad2ff

Please sign in to comment.