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 3 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 @@ -400,6 +400,13 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
// storage_mb size...
storageTiers := storageTierMappings[newMb]

// set default tier for storage when there is now configuration available
if v := diff.GetRawConfig().AsValueMap()["storage_tier"]; v.IsNull() {
newTier = string(storageTiers.DefaultTier)
// set the new value that the update function will pickup and deploy new new storage tier.
diff.SetNew("storage_tier", newTier)
}
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 we can remove all of this code and handle everything in the !isValid check below:

Suggested change
// set default tier for storage when there is now configuration available
if v := diff.GetRawConfig().AsValueMap()["storage_tier"]; v.IsNull() {
newTier = string(storageTiers.DefaultTier)
// set the new value that the update function will pickup and deploy new new storage tier.
diff.SetNew("storage_tier", newTier)
}


Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think if we just replace the current if !isValid { code below with this instead:

Suggested change
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))
}

Copy link
Contributor Author

@HappyTobi HappyTobi May 15, 2024

Choose a reason for hiding this comment

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

This will change the logic a bit.
When you remove the "storage_tier" from you tf file, the default tier will not be set, it will use the one that you set before!

if newTier == "" {
newTier = string(storageTiers.DefaultTier)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,48 @@ 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),
),
},
})
}

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 +593,41 @@ 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) 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