Skip to content

Commit 7382a42

Browse files
authored
azurerm_postgresql_flexible_server: downgrading version forces a new resource to be created (#28559)
1 parent 4a012b1 commit 7382a42

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

internal/services/postgres/postgresql_flexible_server_resource.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -351,31 +351,17 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
351351
},
352352

353353
CustomizeDiff: pluginsdk.CustomDiffWithAll(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
354-
createModeVal := d.Get("create_mode").(string)
355-
356-
if createModeVal == string(servers.CreateModeUpdate) {
354+
if d.HasChange("version") {
357355
oldVersionVal, newVersionVal := d.GetChange("version")
356+
// `version` value has been validated already, ignore the parse errors is safe
357+
oldVersion, _ := strconv.ParseInt(oldVersionVal.(string), 10, 32)
358+
newVersion, _ := strconv.ParseInt(newVersionVal.(string), 10, 32)
358359

359-
if oldVersionVal != "" && newVersionVal != "" {
360-
oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32)
361-
if err != nil {
362-
return err
363-
}
364-
365-
newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32)
366-
if err != nil {
367-
return err
368-
}
369-
370-
if oldVersion < newVersion {
371-
return nil
372-
}
360+
if oldVersion > newVersion {
361+
d.ForceNew("version")
373362
}
363+
return nil
374364
}
375-
376-
d.ForceNew("create_mode")
377-
d.ForceNew("version")
378-
379365
return nil
380366
}, func(ctx context.Context, diff *pluginsdk.ResourceDiff, v interface{}) error {
381367
oldLoginName, _ := diff.GetChange("administrator_login")

internal/services/postgres/postgresql_flexible_server_resource_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,40 @@ func TestAccPostgresqlFlexibleServer_updateSku(t *testing.T) {
178178
})
179179
}
180180

181+
func TestAccPostgresqlFlexibleServer_updateVersion(t *testing.T) {
182+
data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test")
183+
r := PostgresqlFlexibleServerResource{}
184+
data.ResourceTest(t, r, []acceptance.TestStep{
185+
{
186+
Config: r.withVersion(data, 12, ""),
187+
Check: acceptance.ComposeTestCheckFunc(
188+
check.That(data.ResourceName).ExistsInAzure(r),
189+
),
190+
},
191+
data.ImportStep("administrator_password", "create_mode"),
192+
{
193+
Config: r.withVersion(data, 13, ""),
194+
Check: acceptance.ComposeTestCheckFunc(
195+
check.That(data.ResourceName).ExistsInAzure(r),
196+
),
197+
},
198+
{
199+
Config: r.withVersion(data, 13, "Update"),
200+
Check: acceptance.ComposeTestCheckFunc(
201+
check.That(data.ResourceName).ExistsInAzure(r),
202+
),
203+
},
204+
data.ImportStep("administrator_password", "create_mode"),
205+
{
206+
Config: r.withVersion(data, 14, "Default"),
207+
Check: acceptance.ComposeTestCheckFunc(
208+
check.That(data.ResourceName).ExistsInAzure(r),
209+
),
210+
},
211+
data.ImportStep("administrator_password", "create_mode"),
212+
})
213+
}
214+
181215
func TestAccPostgresqlFlexibleServer_geoRestore(t *testing.T) {
182216
data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test")
183217
r := PostgresqlFlexibleServerResource{}
@@ -772,6 +806,29 @@ resource "azurerm_postgresql_flexible_server" "test" {
772806
`, r.template(data), data.RandomInteger)
773807
}
774808

809+
func (r PostgresqlFlexibleServerResource) withVersion(data acceptance.TestData, versionNum int, creatMode string) string {
810+
createModeProp := ""
811+
if creatMode != "" {
812+
createModeProp = fmt.Sprintf("create_mode = \"%s\"", creatMode)
813+
}
814+
815+
return fmt.Sprintf(`
816+
%s
817+
818+
resource "azurerm_postgresql_flexible_server" "test" {
819+
name = "acctest-fs-%d"
820+
resource_group_name = azurerm_resource_group.test.name
821+
location = azurerm_resource_group.test.location
822+
administrator_login = "adminTerraform"
823+
administrator_password = "QAZwsx123"
824+
version = "%d"
825+
%s
826+
sku_name = "GP_Standard_D2s_v3"
827+
zone = "2"
828+
}
829+
`, r.template(data), data.RandomInteger, versionNum, createModeProp)
830+
}
831+
775832
func (r PostgresqlFlexibleServerResource) geoRestoreSource(data acceptance.TestData) string {
776833
return fmt.Sprintf(`
777834
provider "azurerm" {

website/docs/r/postgresql_flexible_server.html.markdown

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ The following arguments are supported:
113113

114114
* `geo_redundant_backup_enabled` - (Optional) Is Geo-Redundant backup enabled on the PostgreSQL Flexible Server. Defaults to `false`. Changing this forces a new PostgreSQL Flexible Server to be created.
115115

116-
* `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `GeoRestore`, `PointInTimeRestore`, `Replica` and `Update`. Changing this forces a new PostgreSQL Flexible Server to be created.
117-
118-
-> **Note:** `create_mode` cannot be changed once it's set since it's a parameter at creation.
119-
120-
-> **Note:** While creating the resource, `create_mode` cannot be set to `Update`.
116+
* `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `GeoRestore`, `PointInTimeRestore`, `Replica` and `Update`.
121117

122118
* `delegated_subnet_id` - (Optional) The ID of the virtual network subnet to create the PostgreSQL Flexible Server. The provided subnet should not have any other resource deployed in it and this subnet will be delegated to the PostgreSQL Flexible Server, if not already delegated. Changing this forces a new PostgreSQL Flexible Server to be created.
123119

@@ -161,7 +157,9 @@ The following arguments are supported:
161157

162158
* `version` - (Optional) The version of PostgreSQL Flexible Server to use. Possible values are `11`,`12`, `13`, `14`, `15` and `16`. Required when `create_mode` is `Default`.
163159

164-
-> **Note:** When `create_mode` is `Update`, upgrading version wouldn't force a new resource to be created.
160+
-> **Note:** Downgrading `version` isn't supported and will force a new PostgreSQL Flexible Server to be created.
161+
162+
-> **Note:** In-place version updates are irreversible and may cause downtime for the PostgreSQL Flexible Server, determined by the size of the instance.
165163

166164
* `zone` - (Optional) Specifies the Availability Zone in which the PostgreSQL Flexible Server should be located.
167165

0 commit comments

Comments
 (0)