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 - update ForceNew for create_mode and version #20495

Merged
merged 8 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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