From 189b7d23712e9458dc45dae7b94db8f43f15616f Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 7 Feb 2023 14:39:07 +0800 Subject: [PATCH 1/7] azurerm_postgresql_flexible_server - support for new properties replicationRole and replicaCapacity --- .../postgresql_flexible_server_resource.go | 40 +++++++++++++++++++ ...ostgresql_flexible_server_resource_test.go | 35 ++++++++++++++++ .../postgresql_flexible_server.html.markdown | 4 ++ 3 files changed, 79 insertions(+) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 929cac449a3b..013cf7f2f2b3 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -248,6 +248,30 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { Computed: true, }, + "replica_capacity": { + Type: pluginsdk.TypeInt, + Optional: true, + ForceNew: true, + Default: 5, + }, + + "replication_role": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + Default: string(servers.ReplicationRolePrimary), + ValidateFunc: validation.StringInSlice([]string{ + string(servers.ReplicationRoleAsyncReplica), + string(servers.ReplicationRoleGeoAsyncReplica), + string(servers.ReplicationRoleGeoSyncReplica), + string(servers.ReplicationRoleNone), + string(servers.ReplicationRolePrimary), + string(servers.ReplicationRoleSecondary), + string(servers.ReplicationRoleSyncReplica), + string(servers.ReplicationRoleWalReplica), + }, false), + }, + "identity": commonschema.SystemAssignedUserAssignedIdentityOptional(), "customer_managed_key": { @@ -398,6 +422,15 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte } parameters.Identity = identity + if v, ok := d.GetOk("replica_capacity"); ok { + parameters.Properties.ReplicaCapacity = utils.Int64(int64(v.(int))) + } + + if v, ok := d.GetOk("replication_role"); ok { + replicationRole := servers.ReplicationRole(v.(string)) + parameters.Properties.ReplicationRole = &replicationRole + } + if err = client.CreateThenPoll(ctx, id, parameters); err != nil { return fmt.Errorf("creating %s: %+v", id, err) } @@ -463,6 +496,8 @@ func resourcePostgresqlFlexibleServerRead(d *pluginsdk.ResourceData, meta interf d.Set("zone", props.AvailabilityZone) d.Set("version", props.Version) d.Set("fqdn", props.FullyQualifiedDomainName) + d.Set("replica_capacity", props.ReplicaCapacity) + d.Set("replication_role", props.ReplicationRole) if network := props.Network; network != nil { publicNetworkAccess := false @@ -632,6 +667,11 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte parameters.Identity = identity } + if d.HasChange("replication_role") { + replicationRole := servers.ReplicationRole(d.Get("replication_role").(string)) + parameters.Properties.ReplicationRole = &replicationRole + } + 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 2436838e0a97..710c2751c865 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -285,6 +285,21 @@ func TestAccPostgresqlFlexibleServer_createWithCustomerManagedKey(t *testing.T) }) } +func TestAccPostgresqlFlexibleServer_replica(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server", "test") + r := PostgresqlFlexibleServerResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.replica(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 { @@ -777,3 +792,23 @@ resource "azurerm_postgresql_flexible_server" "test" { } `, r.cmkTemplate(data), data.RandomInteger) } + +func (r PostgresqlFlexibleServerResource) replica(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 + version = "12" + sku_name = "GP_Standard_D2s_v3" + zone = "2" + replication_role = "Secondary" + replica_capacity = 6 +} +`, 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 5382dad7a0ee..757f422cb7f5 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -116,6 +116,10 @@ The following arguments are supported: * `point_in_time_restore_time_in_utc` - (Optional) The point in time to restore from `source_server_id` when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. +* `replica_capacity` - (Optional) The allowed replica capacity for a PostgreSQL Flexible Server. Defaults to `5`. Changing this forces a new PostgreSQL Flexible Server to be created. + +* `replication_role` - (Optional) The replication role for the PostgreSQL Flexible Server. Possible values are `AsyncReplica`, `GeoAsyncReplica`, `GeoSyncReplica`, `None`, `Primary`, `Secondary`, `SyncReplica` and `WalReplica`. Defaults to `Primary`. Changing this forces a new PostgreSQL Flexible Server to be created. + * `sku_name` - (Optional) The SKU Name for the PostgreSQL Flexible Server. The name of the SKU, follows the `tier` + `name` pattern (e.g. `B_Standard_B1ms`, `GP_Standard_D2s_v3`, `MO_Standard_E4s_v3`). * `source_server_id` - (Optional) The resource ID of the source PostgreSQL Flexible Server to be restored. Required when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. From 08874f0b90a0585b77013939a270bd38877a9561 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 7 Feb 2023 16:08:26 +0800 Subject: [PATCH 2/7] update code --- .../postgresql_flexible_server_resource.go | 1 + ...ostgresql_flexible_server_resource_test.go | 19 +++++++++++++------ .../postgresql_flexible_server.html.markdown | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 013cf7f2f2b3..13430d252ce8 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -139,6 +139,7 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { ValidateFunc: validation.StringInSlice([]string{ string(servers.CreateModeDefault), string(servers.CreateModePointInTimeRestore), + string(servers.CreateModeReplica), }, false), }, diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 710c2751c865..3e6021e49c8c 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -797,18 +797,25 @@ func (r PostgresqlFlexibleServerResource) replica(data acceptance.TestData) stri return fmt.Sprintf(` %s -resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-fs-%d" +resource "azurerm_postgresql_flexible_server" "source" { + name = "acctest-fssource-%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" + zone = "2" sku_name = "GP_Standard_D2s_v3" +} + +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-fsreplica-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "12" zone = "2" - replication_role = "Secondary" - replica_capacity = 6 + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.source.id } -`, r.template(data), data.RandomInteger) +`, r.template(data), data.RandomInteger, data.RandomInteger) } diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index 757f422cb7f5..98ed2be3f2c4 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -100,7 +100,7 @@ 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` and `PointInTimeRestore`. 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. * `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. From f1417ee0dbfaf616eb89b47e417a1427fe95028b Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 7 Feb 2023 16:24:16 +0800 Subject: [PATCH 3/7] update code --- .../postgresql_flexible_server_resource.go | 40 ------------------- ...ostgresql_flexible_server_resource_test.go | 1 + .../postgresql_flexible_server.html.markdown | 4 -- 3 files changed, 1 insertion(+), 44 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 13430d252ce8..4a84224db540 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -249,30 +249,6 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { Computed: true, }, - "replica_capacity": { - Type: pluginsdk.TypeInt, - Optional: true, - ForceNew: true, - Default: 5, - }, - - "replication_role": { - Type: pluginsdk.TypeString, - Optional: true, - ForceNew: true, - Default: string(servers.ReplicationRolePrimary), - ValidateFunc: validation.StringInSlice([]string{ - string(servers.ReplicationRoleAsyncReplica), - string(servers.ReplicationRoleGeoAsyncReplica), - string(servers.ReplicationRoleGeoSyncReplica), - string(servers.ReplicationRoleNone), - string(servers.ReplicationRolePrimary), - string(servers.ReplicationRoleSecondary), - string(servers.ReplicationRoleSyncReplica), - string(servers.ReplicationRoleWalReplica), - }, false), - }, - "identity": commonschema.SystemAssignedUserAssignedIdentityOptional(), "customer_managed_key": { @@ -423,15 +399,6 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte } parameters.Identity = identity - if v, ok := d.GetOk("replica_capacity"); ok { - parameters.Properties.ReplicaCapacity = utils.Int64(int64(v.(int))) - } - - if v, ok := d.GetOk("replication_role"); ok { - replicationRole := servers.ReplicationRole(v.(string)) - parameters.Properties.ReplicationRole = &replicationRole - } - if err = client.CreateThenPoll(ctx, id, parameters); err != nil { return fmt.Errorf("creating %s: %+v", id, err) } @@ -497,8 +464,6 @@ func resourcePostgresqlFlexibleServerRead(d *pluginsdk.ResourceData, meta interf d.Set("zone", props.AvailabilityZone) d.Set("version", props.Version) d.Set("fqdn", props.FullyQualifiedDomainName) - d.Set("replica_capacity", props.ReplicaCapacity) - d.Set("replication_role", props.ReplicationRole) if network := props.Network; network != nil { publicNetworkAccess := false @@ -668,11 +633,6 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte parameters.Identity = identity } - if d.HasChange("replication_role") { - replicationRole := servers.ReplicationRole(d.Get("replication_role").(string)) - parameters.Properties.ReplicationRole = &replicationRole - } - 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 3e6021e49c8c..028b1fbaa6d2 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -803,6 +803,7 @@ resource "azurerm_postgresql_flexible_server" "source" { location = azurerm_resource_group.test.location administrator_login = "adminTerraform" administrator_password = "QAZwsx123" + storage_mb = 32768 version = "12" zone = "2" sku_name = "GP_Standard_D2s_v3" diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index 98ed2be3f2c4..0e32547aa3eb 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -116,10 +116,6 @@ The following arguments are supported: * `point_in_time_restore_time_in_utc` - (Optional) The point in time to restore from `source_server_id` when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. -* `replica_capacity` - (Optional) The allowed replica capacity for a PostgreSQL Flexible Server. Defaults to `5`. Changing this forces a new PostgreSQL Flexible Server to be created. - -* `replication_role` - (Optional) The replication role for the PostgreSQL Flexible Server. Possible values are `AsyncReplica`, `GeoAsyncReplica`, `GeoSyncReplica`, `None`, `Primary`, `Secondary`, `SyncReplica` and `WalReplica`. Defaults to `Primary`. Changing this forces a new PostgreSQL Flexible Server to be created. - * `sku_name` - (Optional) The SKU Name for the PostgreSQL Flexible Server. The name of the SKU, follows the `tier` + `name` pattern (e.g. `B_Standard_B1ms`, `GP_Standard_D2s_v3`, `MO_Standard_E4s_v3`). * `source_server_id` - (Optional) The resource ID of the source PostgreSQL Flexible Server to be restored. Required when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. From 50d0f65ddd256e6e3eddbeaacdc64cb7edacb70a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 7 Feb 2023 19:24:53 +0800 Subject: [PATCH 4/7] update code --- ...ostgresql_flexible_server_resource_test.go | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 028b1fbaa6d2..e2fa2363f8b2 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -291,12 +291,20 @@ func TestAccPostgresqlFlexibleServer_replica(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.replica(data), + Config: r.basic(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), }, data.ImportStep("administrator_password", "create_mode"), + { + PreConfig: func() { time.Sleep(15 * time.Minute) }, + Config: r.replica(data), + Check: acceptance.ComposeTestCheckFunc( + check.That("azurerm_postgresql_flexible_server.replica").ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), }) } @@ -797,26 +805,13 @@ func (r PostgresqlFlexibleServerResource) replica(data acceptance.TestData) stri return fmt.Sprintf(` %s -resource "azurerm_postgresql_flexible_server" "source" { - name = "acctest-fssource-%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" - zone = "2" - sku_name = "GP_Standard_D2s_v3" -} - -resource "azurerm_postgresql_flexible_server" "test" { - name = "acctest-fsreplica-%d" - resource_group_name = azurerm_resource_group.test.name - location = azurerm_resource_group.test.location - version = "12" - zone = "2" - create_mode = "Replica" - source_server_id = azurerm_postgresql_flexible_server.source.id +resource "azurerm_postgresql_flexible_server" "replica" { + name = "acctest-fs-replica-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + zone = "3" + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.test.id } -`, r.template(data), data.RandomInteger, data.RandomInteger) +`, r.basic(data), data.RandomInteger) } From 2bd40eb9cf043752e177dfd1160cd794247cd87e Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 8 Feb 2023 08:28:45 +0800 Subject: [PATCH 5/7] update code --- .../postgres/postgresql_flexible_server_resource.go | 6 ++++++ website/docs/r/postgresql_flexible_server.html.markdown | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 4a84224db540..8c4077d80669 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -313,6 +313,12 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte } } + if servers.CreateMode(createMode) == servers.CreateModeReplica { + if _, ok := d.GetOk("source_server_id"); !ok { + return fmt.Errorf("`source_server_id` is required when `create_mode` is `Replica`") + } + } + if createMode == "" || servers.CreateMode(createMode) == servers.CreateModeDefault { if _, ok := d.GetOk("administrator_login"); !ok { return fmt.Errorf("`administrator_login` is required when `create_mode` is `Default`") diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index 0e32547aa3eb..cc1633eb4193 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -118,7 +118,7 @@ The following arguments are supported: * `sku_name` - (Optional) The SKU Name for the PostgreSQL Flexible Server. The name of the SKU, follows the `tier` + `name` pattern (e.g. `B_Standard_B1ms`, `GP_Standard_D2s_v3`, `MO_Standard_E4s_v3`). -* `source_server_id` - (Optional) The resource ID of the source PostgreSQL Flexible Server to be restored. Required when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. +* `source_server_id` - (Optional) The resource ID of the source PostgreSQL Flexible Server to be restored. Required when `create_mode` is `PointInTimeRestore` or `Replica`. Changing this forces a new PostgreSQL Flexible Server to be created. * `storage_mb` - (Optional) The max storage allowed for the PostgreSQL Flexible Server. Possible values are `32768`, `65536`, `131072`, `262144`, `524288`, `1048576`, `2097152`, `4194304`, `8388608`, and `16777216`. From b369032831ae683d8d2eb306944da4d3c2d3386d Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 8 Feb 2023 12:07:14 +0800 Subject: [PATCH 6/7] update code --- .../postgresql_flexible_server_resource.go | 34 +++++++++++++++++++ ...ostgresql_flexible_server_resource_test.go | 23 +++++++++++++ .../postgresql_flexible_server.html.markdown | 4 +++ 3 files changed, 61 insertions(+) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 8c4077d80669..42caf7700565 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -249,6 +249,14 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { Computed: true, }, + "replication_role": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(servers.ReplicationRoleNone), + }, false), + }, + "identity": commonschema.SystemAssignedUserAssignedIdentityOptional(), "customer_managed_key": { @@ -304,6 +312,10 @@ func resourcePostgresqlFlexibleServerCreate(d *pluginsdk.ResourceData, meta inte createMode := d.Get("create_mode").(string) + if _, ok := d.GetOk("replication_role"); ok { + return fmt.Errorf("`replication_role` cannot be set while creating") + } + if servers.CreateMode(createMode) == servers.CreateModePointInTimeRestore { if _, ok := d.GetOk("source_server_id"); !ok { return fmt.Errorf("`source_server_id` is required when `create_mode` is `PointInTimeRestore`") @@ -471,6 +483,9 @@ func resourcePostgresqlFlexibleServerRead(d *pluginsdk.ResourceData, meta interf d.Set("version", props.Version) d.Set("fqdn", props.FullyQualifiedDomainName) + // Currently, `replicationRole` is set to `Primary` when `createMode` is `Replica` and `replicationRole` is updated to `None`. Service team confirmed it should be set to `None` for this scenario. See more details from https://github.com/Azure/azure-rest-api-specs/issues/22499 + d.Set("replication_role", d.Get("replication_role").(string)) + if network := props.Network; network != nil { publicNetworkAccess := false if network.PublicNetworkAccess != nil { @@ -591,6 +606,25 @@ func resourcePostgresqlFlexibleServerUpdate(d *pluginsdk.ResourceData, meta inte } } + if d.HasChange("replication_role") { + createMode := d.Get("create_mode").(string) + replicationRole := d.Get("replication_role").(string) + if createMode == string(servers.CreateModeReplica) && replicationRole == string(servers.ReplicationRoleNone) { + replicationRole := servers.ReplicationRoleNone + parameters := servers.ServerForUpdate{ + Properties: &servers.ServerPropertiesForUpdate{ + ReplicationRole: &replicationRole, + }, + } + + if err := client.UpdateThenPoll(ctx, *id, parameters); err != nil { + return fmt.Errorf("updating `replication_role` for %s: %+v", *id, err) + } + } else { + return fmt.Errorf("`replication_role` only can be updated to `None` for replica server") + } + } + if d.HasChange("administrator_password") { parameters.Properties.AdministratorLoginPassword = utils.String(d.Get("administrator_password").(string)) } diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index e2fa2363f8b2..2d69aeae426f 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -305,6 +305,13 @@ func TestAccPostgresqlFlexibleServer_replica(t *testing.T) { ), }, data.ImportStep("administrator_password", "create_mode"), + { + Config: r.updateReplicationRole(data), + Check: acceptance.ComposeTestCheckFunc( + check.That("azurerm_postgresql_flexible_server.replica").ExistsInAzure(r), + ), + }, + data.ImportStep("administrator_password", "create_mode"), }) } @@ -815,3 +822,19 @@ resource "azurerm_postgresql_flexible_server" "replica" { } `, r.basic(data), data.RandomInteger) } + +func (r PostgresqlFlexibleServerResource) updateReplicationRole(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_postgresql_flexible_server" "replica" { + name = "acctest-fs-replica-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + zone = "3" + create_mode = "Replica" + source_server_id = azurerm_postgresql_flexible_server.test.id + replication_role = "None" +} +`, r.basic(data), data.RandomInteger) +} diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index cc1633eb4193..ef3d41f62b1c 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -116,6 +116,10 @@ The following arguments are supported: * `point_in_time_restore_time_in_utc` - (Optional) The point in time to restore from `source_server_id` when `create_mode` is `PointInTimeRestore`. Changing this forces a new PostgreSQL Flexible Server to be created. +* `replication_role` - (Optional) The replication role for the PostgreSQL Flexible Server. Possible value is `None`. + +~> **NOTE:** The `replication_role` cannot be set while creating and only can be updated to `None` for replica server. + * `sku_name` - (Optional) The SKU Name for the PostgreSQL Flexible Server. The name of the SKU, follows the `tier` + `name` pattern (e.g. `B_Standard_B1ms`, `GP_Standard_D2s_v3`, `MO_Standard_E4s_v3`). * `source_server_id` - (Optional) The resource ID of the source PostgreSQL Flexible Server to be restored. Required when `create_mode` is `PointInTimeRestore` or `Replica`. Changing this forces a new PostgreSQL Flexible Server to be created. From 3ba0db77dffe66ac7738dda3f85124b608dfbd9a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 8 Feb 2023 14:37:04 +0800 Subject: [PATCH 7/7] update code --- .../postgres/postgresql_flexible_server_resource_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource_test.go b/internal/services/postgres/postgresql_flexible_server_resource_test.go index 2d69aeae426f..57ceccaf4291 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_resource_test.go @@ -816,7 +816,7 @@ resource "azurerm_postgresql_flexible_server" "replica" { name = "acctest-fs-replica-%d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location - zone = "3" + zone = "2" create_mode = "Replica" source_server_id = azurerm_postgresql_flexible_server.test.id } @@ -831,7 +831,7 @@ resource "azurerm_postgresql_flexible_server" "replica" { name = "acctest-fs-replica-%d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location - zone = "3" + zone = "2" create_mode = "Replica" source_server_id = azurerm_postgresql_flexible_server.test.id replication_role = "None"