Skip to content

Commit

Permalink
azurerm_postgresql_flexible_server - update ForceNew for `create_…
Browse files Browse the repository at this point in the history
…mode` and `version` (#20495)
  • Loading branch information
neil-yechenwei committed Feb 16, 2023
1 parent 9f6f57b commit 45924e5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
44 changes: 42 additions & 2 deletions internal/services/postgres/postgresql_flexible_server_resource.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package postgres

import (
"context"
"fmt"
"log"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -126,7 +128,6 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(servers.PossibleValuesForServerVersion(), false),
},

Expand All @@ -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),
},

Expand Down Expand Up @@ -286,6 +287,35 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource {

"tags": commonschema.Tags(),
},

CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
createModeVal := d.Get("create_mode").(string)

if createModeVal == string(servers.CreateModeUpdate) {
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 {
return nil
}
}
}

d.ForceNew("create_mode")
d.ForceNew("version")

return nil
}),
}
}

Expand Down Expand Up @@ -673,6 +703,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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion website/docs/r/postgresql_flexible_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:** 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.

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 45924e5

Please sign in to comment.