Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_postgresql_flexible_server: fix storage_tier calculation #25947

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great start to the test coverage, yet I think there are many more scenarios around this especially if we are now setting defaults when storage mb changes. I would like to see tests for storage tier set above storage tier default value, then remove the storage tier from the configuration (e.g., storage mb set to 65536 with a storage tier of P10 in the config file) then remove the storage tier P10 in the config. It should then revert tier from P10 -> P6, etc. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure will add one more test

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
Loading