diff --git a/azurerm/internal/services/postgres/postgresql_server_resource.go b/azurerm/internal/services/postgres/postgresql_server_resource.go index ab13b78f9a85e..a1f5397934e97 100644 --- a/azurerm/internal/services/postgres/postgresql_server_resource.go +++ b/azurerm/internal/services/postgres/postgresql_server_resource.go @@ -585,15 +585,12 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("parsing Postgres Server ID : %v", err) } - // Locks for scaling of replica functionality + // Locks for upscaling of replicas mode := postgresql.CreateMode(d.Get("create_mode").(string)) - source := d.Get("creation_source_server_id").(string) - primaryID := id.String() if mode == postgresql.CreateModeReplica { - primaryID = source + primaryID = d.Get("creation_source_server_id").(string) } - locks.ByID(primaryID) defer locks.UnlockByID(primaryID) @@ -602,6 +599,33 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er return fmt.Errorf("expanding `sku_name` for PostgreSQL Server %s (Resource Group %q): %v", id.Name, id.ResourceGroup, err) } + if d.HasChange("sku_name") && mode != postgresql.CreateModeReplica { + oldRaw, newRaw := d.GetChange("sku_name") + old := oldRaw.(string) + new := newRaw.(string) + + if indexOfSku(old) < indexOfSku(new) { + listReplicas, err := replicasClient.ListByServer(ctx, id.ResourceGroup, id.Name) + if err != nil { + return fmt.Errorf("request error for list of replicas for PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + + propertiesReplica := postgresql.ServerUpdateParameters{ + Sku: sku, + } + for _, replica := range *listReplicas.Value { + future, err := client.Update(ctx, id.ResourceGroup, *replica.Name, propertiesReplica) + if err != nil { + return fmt.Errorf("upscaling PostgreSQL Server Replica %q (Resource Group %q): %+v", *replica.Name, id.ResourceGroup, err) + } + + if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { + return fmt.Errorf("waiting for update of PostgreSQL Server Replica %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + } + } + } + } + publicAccess := postgresql.PublicNetworkAccessEnumEnabled if v := d.Get("public_network_access_enabled"); !v.(bool) { publicAccess = postgresql.PublicNetworkAccessEnumDisabled @@ -628,33 +652,6 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er Tags: tags.Expand(d.Get("tags").(map[string]interface{})), } - if d.HasChange("sku_name") && mode != postgresql.CreateModeReplica { - oldRaw, newRaw := d.GetChange("sku_name") - old := oldRaw.(string) - new := newRaw.(string) - - if indexOfSku(old) < indexOfSku(new) { - propertiesReplica := postgresql.ServerUpdateParameters{ - Sku: sku, - } - - listReplicas, err := replicasClient.ListByServer(ctx, id.ResourceGroup, id.Name) - if err != nil { - return fmt.Errorf("request error for list of replicas for PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) - } - for _, replica := range *listReplicas.Value { - future, err := client.Update(ctx, id.ResourceGroup, *replica.Name, propertiesReplica) - if err != nil { - return fmt.Errorf("updating PostgreSQL Server Replica %q (Resource Group %q): %+v", *replica.Name, id.ResourceGroup, err) - } - - if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting for update of PostgreSQL Server Replica %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) - } - } - } - } - future, err := client.Update(ctx, id.ResourceGroup, id.Name, properties) if err != nil { return fmt.Errorf("updating PostgreSQL Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) diff --git a/azurerm/internal/services/postgres/postgresql_server_resource_test.go b/azurerm/internal/services/postgres/postgresql_server_resource_test.go index d0b6298488495..62fa0d30851bf 100644 --- a/azurerm/internal/services/postgres/postgresql_server_resource_test.go +++ b/azurerm/internal/services/postgres/postgresql_server_resource_test.go @@ -326,7 +326,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) { r := PostgreSQLServerResource{} data.ResourceTest(t, r, []resource.TestStep{ { - Config: r.scaleableReplica(data, "11", "GP_Gen5_2"), + Config: r.scaleableReplica(data, "GP_Gen5_2", "11"), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_2"), @@ -336,7 +336,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) { }, data.ImportStep("administrator_login_password"), { - Config: r.scaleableReplica(data, "11", "GP_Gen5_4"), + Config: r.scaleableReplica(data, "GP_Gen5_4", "11"), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_4"), @@ -346,7 +346,7 @@ func TestAccPostgreSQLServer_scaleReplica(t *testing.T) { }, data.ImportStep("administrator_login_password"), { - Config: r.scaleableReplica(data, "11", "GP_Gen5_2"), + Config: r.scaleableReplica(data, "GP_Gen5_2", "11"), Check: resource.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_2"), @@ -890,7 +890,7 @@ resource "azurerm_postgresql_server" "test" { `, data.RandomInteger, data.Locations.Primary, version, tlsVersion) } -func (r PostgreSQLServerResource) scaleableReplica(data acceptance.TestData, version string, sku string) string { +func (r PostgreSQLServerResource) scaleableReplica(data acceptance.TestData, sku string, version string) string { return fmt.Sprintf(` %[1]s @@ -899,13 +899,13 @@ resource "azurerm_postgresql_server" "replica" { location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name - version = "%[3]s" - sku_name = "%[4]s" + sku_name = "%[3]s" + version = "%[4]s" create_mode = "Replica" creation_source_server_id = azurerm_postgresql_server.test.id ssl_enforcement_enabled = true } -`, r.template(data, sku, version), data.RandomInteger, version, sku) +`, r.template(data, sku, version), data.RandomInteger, sku, version) }