From 5bcebaee3e75806bd928ba3c531e03c0985c36e5 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 13:26:44 +0800 Subject: [PATCH 1/8] support createmode = update --- .../postgresql_flexible_server_resource.go | 48 ++++++++++++++++++- ...ostgresql_flexible_server_resource_test.go | 41 ++++++++++++++++ .../postgresql_flexible_server.html.markdown | 6 ++- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 42caf7700565..89350c0246ee 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -1,8 +1,10 @@ package postgres import ( + "context" "fmt" "log" + "strconv" "strings" "time" @@ -126,7 +128,6 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { Type: pluginsdk.TypeString, Optional: true, Computed: true, - ForceNew: true, ValidateFunc: validation.StringInSlice(servers.PossibleValuesForServerVersion(), false), }, @@ -135,11 +136,11 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { "create_mode": { Type: pluginsdk.TypeString, Optional: true, - ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ string(servers.CreateModeDefault), string(servers.CreateModePointInTimeRestore), string(servers.CreateModeReplica), + string(servers.CreateModeUpdate), }, false), }, @@ -286,6 +287,39 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { "tags": commonschema.Tags(), }, + + CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error { + oldVersionVal, newVersionVal := d.GetChange("version") + createModeVal := d.Get("create_mode").(string) + + if createModeVal != string(servers.CreateModeUpdate) { + d.ForceNew("create_mode") + } + + if d.HasChange("version") { + if oldVersionVal != "" && newVersionVal != "" { + oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) + if err != nil { + return err + } + + newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) + if err != nil { + return err + } + + if !(oldVersion < newVersion && createModeVal == string(servers.CreateModeUpdate)) { + d.ForceNew("version") + } + } else { + d.ForceNew("version") + } + } else { + d.ForceNew("version") + } + + return nil + }), } } @@ -673,6 +707,16 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte parameters.Identity = identity } + if d.HasChange("create_mode") { + createMode := servers.CreateModeForUpdate(d.Get("create_mode").(string)) + parameters.Properties.CreateMode = &createMode + } + + if d.HasChange("version") { + version := servers.ServerVersion(d.Get("version").(string)) + parameters.Properties.Version = &version + } + if err = client.UpdateThenPoll(ctx, *id, parameters); err != nil { return fmt.Errorf("updating %s: %+v", id, err) } diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 57ceccaf4291..aab22e9198f1 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -315,6 +315,28 @@ func TestAccPostgresqlFlexibleServer_replica(t *testing.T) { }) } +func TestAccPostgresqlFlexibleServer_upgradeVersion(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test") + r := PostgresqlFlexibleServerResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), + { + Config: r.upgradeVersion(data), + 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 { @@ -838,3 +860,22 @@ resource "azurerm_postgresql_flexible_server" "replica" { } `, r.basic(data), data.RandomInteger) } + +func (r PostgresqlFlexibleServerResource) upgradeVersion(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 = 32768 + create_mode = "Update" + version = "13" + sku_name = "GP_Standard_D2s_v3" + zone = "2" +} +`, r.template(data), data.RandomInteger) +} diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index ef3d41f62b1c..4c013f74e2cd 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -100,7 +100,9 @@ The following arguments are supported: * `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. -* `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `PointInTimeRestore` and `Replica`. Changing this forces a new PostgreSQL Flexible Server to be created. +* `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `PointInTimeRestore`, `Replica` and `Update`. Changing this forces a new PostgreSQL Flexible Server to be created. + +-> **Note:** When `create_mode` is set to `Update`, it wouldn't force a new resource to be created. * `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. @@ -130,6 +132,8 @@ The following arguments are supported: * `version` - (Optional) The version of PostgreSQL Flexible Server to use. Possible values are `11`,`12`, `13` and `14`. Required when `create_mode` is `Default`. Changing this forces a new PostgreSQL Flexible Server to be created. +-> **Note:** When `create_mode` is `Update`, upgrading version wouldn't force a new resource to be created. + * `zone` - (Optional) Specifies the Availability Zone in which the PostgreSQL Flexible Server should be located. -> **Note:** Azure will automatically assign an Availability Zone if one is not specified. If the PostgreSQL Flexible Server fails-over to the Standby Availability Zone, the `zone` will be updated to reflect the current Primary Availability Zone. You can use [Terraform's `ignore_changes` functionality](https://www.terraform.io/docs/language/meta-arguments/lifecycle.html#ignore_changes) to ignore changes to the `zone` and `high_availability.0.standby_availability_zone` fields should you wish for Terraform to not migrate the PostgreSQL Flexible Server back to it's primary Availability Zone after a fail-over. From dd2c65a847b289625a9772f8e615276d2aa36749 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 15:59:20 +0800 Subject: [PATCH 2/8] update code --- website/docs/r/postgresql_flexible_server.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index 4c013f74e2cd..adef627305c4 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -104,6 +104,8 @@ The following arguments are supported: -> **Note:** When `create_mode` is set to `Update`, it wouldn't force a new resource to be created. +-> **Note:** While creating the resource, `create_mode` cannot be set to `Update`. + * `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. * `private_dns_zone_id` - (Optional) The ID of the private DNS zone to create the PostgreSQL Flexible Server. Changing this forces a new PostgreSQL Flexible Server to be created. From fd0e2468be15f8bb2db9dbdf32fc38afea39ed3f Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 16:27:15 +0800 Subject: [PATCH 3/8] update code --- .../postgresql_flexible_server_resource.go | 40 +++++++++-------- ...ostgresql_flexible_server_resource_test.go | 44 +++++++++++++++++-- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 89350c0246ee..53a0728eafbf 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -289,32 +289,36 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { }, CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error { - oldVersionVal, newVersionVal := d.GetChange("version") createModeVal := d.Get("create_mode").(string) + oldVersionVal, newVersionVal := d.GetChange("version") - if createModeVal != string(servers.CreateModeUpdate) { - d.ForceNew("create_mode") - } - - if d.HasChange("version") { - if oldVersionVal != "" && newVersionVal != "" { - oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) - if err != nil { - return err - } - - newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) - if err != nil { - return err - } - - if !(oldVersion < newVersion && createModeVal == string(servers.CreateModeUpdate)) { + if createModeVal == string(servers.CreateModeUpdate) { + if d.HasChange("version") { + if oldVersionVal != "" && newVersionVal != "" { + oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) + if err != nil { + return err + } + + newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) + if err != nil { + return err + } + + if !(oldVersion < newVersion) { + d.ForceNew("create_mode") + d.ForceNew("version") + } + } else { + d.ForceNew("create_mode") d.ForceNew("version") } } else { + d.ForceNew("create_mode") d.ForceNew("version") } } else { + d.ForceNew("create_mode") d.ForceNew("version") } diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index aab22e9198f1..0ddc3a6a7b08 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -366,20 +366,56 @@ resource "azurerm_resource_group" "test" { func (r PostgresqlFlexibleServerResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` -%s +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-postgresql-test0223" + location = "westeurope" +} resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-fs-%d" + name = "acctest-fs-test0222" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location administrator_login = "adminTerraform" administrator_password = "QAZwsx123" storage_mb = 32768 - version = "12" sku_name = "GP_Standard_D2s_v3" zone = "2" + + create_mode = "Default" + version = "13" } -`, r.template(data), data.RandomInteger) +`) +} + +func (r PostgresqlFlexibleServerResource) basic2(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-postgresql-test0223" + location = "westeurope" +} + +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-fs-test0222" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + administrator_login = "adminTerraform" + administrator_password = "QAZwsx123" + storage_mb = 32768 + sku_name = "GP_Standard_D2s_v3" + zone = "2" + + create_mode = "Update" + version = "13" +} +`) } func (r PostgresqlFlexibleServerResource) requiresImport(data acceptance.TestData) string { From 913d2b9567cc2914a9a425570cb20e9e30eb9f39 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 16:35:58 +0800 Subject: [PATCH 4/8] update code --- .../services/postgres/postgresql_flexible_server_resource.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 53a0728eafbf..b56a5f646b03 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -290,10 +290,11 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error { createModeVal := d.Get("create_mode").(string) - oldVersionVal, newVersionVal := d.GetChange("version") if createModeVal == string(servers.CreateModeUpdate) { if d.HasChange("version") { + oldVersionVal, newVersionVal := d.GetChange("version") + if oldVersionVal != "" && newVersionVal != "" { oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) if err != nil { From 350572ac0d03a271651fe2e11168b8c997d00766 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 16:42:37 +0800 Subject: [PATCH 5/8] update code --- .../postgresql_flexible_server_resource.go | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index b56a5f646b03..fc76e0a76c39 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -292,25 +292,20 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { createModeVal := d.Get("create_mode").(string) if createModeVal == string(servers.CreateModeUpdate) { - if d.HasChange("version") { - oldVersionVal, newVersionVal := d.GetChange("version") - - if oldVersionVal != "" && newVersionVal != "" { - oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) - if err != nil { - return err - } - - newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) - if err != nil { - return err - } - - if !(oldVersion < newVersion) { - d.ForceNew("create_mode") - d.ForceNew("version") - } - } else { + oldVersionVal, newVersionVal := d.GetChange("version") + + if oldVersionVal != "" && newVersionVal != "" { + oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) + if err != nil { + return err + } + + newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) + if err != nil { + return err + } + + if !(oldVersion < newVersion) { d.ForceNew("create_mode") d.ForceNew("version") } From ea1e159a96fc060bb90e6834459ccd7bbc93e437 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 16:45:03 +0800 Subject: [PATCH 6/8] update code --- .../postgresql_flexible_server_resource.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index fc76e0a76c39..01973ce64ae2 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -305,19 +305,15 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { return err } - if !(oldVersion < newVersion) { - d.ForceNew("create_mode") - d.ForceNew("version") + if oldVersion < newVersion { + return nil } - } else { - d.ForceNew("create_mode") - d.ForceNew("version") } - } else { - d.ForceNew("create_mode") - d.ForceNew("version") } + d.ForceNew("create_mode") + d.ForceNew("version") + return nil }), } From 9c29ad63f621bdc50dd1e452dfb81e5ad2b0618a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 15 Feb 2023 16:48:01 +0800 Subject: [PATCH 7/8] update code --- ...ostgresql_flexible_server_resource_test.go | 44 ++----------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 0ddc3a6a7b08..aab22e9198f1 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -366,56 +366,20 @@ resource "azurerm_resource_group" "test" { func (r PostgresqlFlexibleServerResource) basic(data acceptance.TestData) string { return fmt.Sprintf(` -provider "azurerm" { - features {} -} - -resource "azurerm_resource_group" "test" { - name = "acctestRG-postgresql-test0223" - location = "westeurope" -} - -resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-fs-test0222" - resource_group_name = azurerm_resource_group.test.name - location = azurerm_resource_group.test.location - administrator_login = "adminTerraform" - administrator_password = "QAZwsx123" - storage_mb = 32768 - sku_name = "GP_Standard_D2s_v3" - zone = "2" - - create_mode = "Default" - version = "13" -} -`) -} - -func (r PostgresqlFlexibleServerResource) basic2(data acceptance.TestData) string { - return fmt.Sprintf(` -provider "azurerm" { - features {} -} - -resource "azurerm_resource_group" "test" { - name = "acctestRG-postgresql-test0223" - location = "westeurope" -} +%s resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-fs-test0222" + 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 = 32768 + version = "12" sku_name = "GP_Standard_D2s_v3" zone = "2" - - create_mode = "Update" - version = "13" } -`) +`, r.template(data), data.RandomInteger) } func (r PostgresqlFlexibleServerResource) requiresImport(data acceptance.TestData) string { From 9ae088a3bcce89c61ba4a1fe16016f0915d14737 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 16 Feb 2023 09:05:42 +0800 Subject: [PATCH 8/8] update code --- website/docs/r/postgresql_flexible_server.html.markdown | 2 -- 1 file changed, 2 deletions(-) diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index adef627305c4..dfa30f00af39 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -102,8 +102,6 @@ The following arguments are supported: * `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `PointInTimeRestore`, `Replica` and `Update`. Changing this forces a new PostgreSQL Flexible Server to be created. --> **Note:** When `create_mode` is set to `Update`, it wouldn't force a new resource to be created. - -> **Note:** While creating the resource, `create_mode` cannot be set to `Update`. * `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.