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 - move the error check for storage_mb from CustomizeDiff to Create/Update func #25986

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -384,11 +384,6 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
newMb = newStorageMbRaw.(int)
newTier = newTierRaw.(string)

// storage_mb can only be scaled up...
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it not possible to also detect that something has changed forcing a new resoruce to be created and just not throw the error?

Copy link
Contributor Author

@neil-yechenwei neil-yechenwei May 17, 2024

Choose a reason for hiding this comment

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

@katbyte , Unfortunately, I didn't find any way to ask terraform if the resource is going to be destroyed or not, I assume that bit is only known by the core runtime. And I didn't find any way to judge if the resource is going to be destroyed per the condition combination. So I have to move it to create/update func.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

if newMb < oldStorageMbRaw.(int) {
return fmt.Errorf("'storage_mb' can only be scaled up, expected the new 'storage_mb' value (%d) to be larger than the previous 'storage_mb' value (%d)", newMb, oldStorageMbRaw.(int))
}

// if newMb or newTier values are empty,
// assign the default values that will
// be assigned in the create func...
Expand Down Expand Up @@ -517,7 +512,10 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte
return fmt.Errorf("expanding `sku_name` for %s: %v", id, err)
}

storage := expandArmServerStorage(d)
storage, err := expandArmServerStorage(d)
if err != nil {
return err
}
var storageMb int

if storage.StorageSizeGB == nil || *storage.StorageSizeGB == 0 {
Expand Down Expand Up @@ -854,9 +852,14 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte

if d.HasChange("auto_grow_enabled") || d.HasChange("storage_mb") || d.HasChange("storage_tier") {
// TODO remove the additional update after https://github.com/Azure/azure-rest-api-specs/issues/22867 is fixed
storage, err := expandArmServerStorage(d)
if err != nil {
return err
}

storageUpdateParameters := servers.ServerForUpdate{
Properties: &servers.ServerPropertiesForUpdate{
Storage: expandArmServerStorage(d),
Storage: storage,
},
}

Expand Down Expand Up @@ -999,7 +1002,7 @@ func expandArmServerMaintenanceWindow(input []interface{}) *servers.MaintenanceW
return &maintenanceWindow
}

func expandArmServerStorage(d *pluginsdk.ResourceData) *servers.Storage {
func expandArmServerStorage(d *pluginsdk.ResourceData) (*servers.Storage, error) {
storage := servers.Storage{}

autoGrow := servers.StorageAutoGrowDisabled
Expand All @@ -1008,6 +1011,12 @@ func expandArmServerStorage(d *pluginsdk.ResourceData) *servers.Storage {
}
storage.AutoGrow = &autoGrow

// storage_mb can only be scaled up...
oldStorageMbRaw, newStorageMbRaw := d.GetChange("storage_mb")
if newStorageMbRaw.(int) < oldStorageMbRaw.(int) {
return nil, fmt.Errorf("'storage_mb' can only be scaled up, expected the new 'storage_mb' value (%d) to be larger than the previous 'storage_mb' value (%d)", newStorageMbRaw.(int), oldStorageMbRaw.(int))
}

if v, ok := d.GetOk("storage_mb"); ok {
storage.StorageSizeGB = pointer.FromInt64(int64(v.(int) / 1024))
}
Expand All @@ -1016,7 +1025,7 @@ func expandArmServerStorage(d *pluginsdk.ResourceData) *servers.Storage {
storage.Tier = pointer.To(servers.AzureManagedDiskPerformanceTiers(v.(string)))
}

return &storage
return &storage, nil
}

func expandArmServerBackup(d *pluginsdk.ResourceData) *servers.Backup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,28 @@ func TestAccPostgresqlFlexibleServer_updateOnlyWithStorageTier(t *testing.T) {
})
}

func TestAccPostgresqlFlexibleServer_recreateWithLowerStorageMb(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test")
r := PostgresqlFlexibleServerResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.recreateWithLowerStorageMb(data, data.RandomInteger, "65536"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("administrator_password", "create_mode"),
{
Config: r.recreateWithLowerStorageMb(data, data.RandomInteger+1, "32768"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("administrator_password", "create_mode"),
})
}

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 @@ -1378,3 +1400,22 @@ resource "azurerm_postgresql_flexible_server" "test" {
}
`, r.template(data), data.RandomInteger, storageMb, storageTier)
}

func (r PostgresqlFlexibleServerResource) recreateWithLowerStorageMb(data acceptance.TestData, nameSuffix int, 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
storage_tier = "P6"
version = "12"
sku_name = "GP_Standard_D2s_v3"
zone = "2"
}
`, r.template(data), nameSuffix, storageMb)
}
Loading