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

postgresql_server: Wait for restart PostgreSQL replicas to prevent failures #11458

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
24 changes: 21 additions & 3 deletions azurerm/internal/services/postgres/postgresql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,20 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er
primaryID := id.String()
if mode == postgresql.CreateModeReplica {
primaryID = d.Get("creation_source_server_id").(string)

// Wait for possible restarts triggered by scaling primary (and its replicas)
log.Printf("[DEBUG] Waiting for PostgreSQL Server %q (Resource Group %q) to become available", id.Name, id.ResourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{string(postgresql.ServerStateInaccessible), "Restarting"},
Target: []string{string(postgresql.ServerStateReady)},
Refresh: postgreSqlStateRefreshFunc(ctx, client, id.ResourceGroup, id.Name),
MinTimeout: 15 * time.Second,
Timeout: d.Timeout(schema.TimeoutCreate),
}

if _, err = stateConf.WaitForState(); err != nil {
return fmt.Errorf("waiting for PostgreSQL Server %q (Resource Group %q)to become available: %+v", id.Name, id.ResourceGroup, err)
}
}
locks.ByID(primaryID)
defer locks.UnlockByID(primaryID)
Expand All @@ -629,13 +643,17 @@ func resourcePostgreSQLServerUpdate(d *schema.ResourceData, meta interface{}) er
Sku: sku,
}
for _, replica := range *listReplicas.Value {
future, err := client.Update(ctx, id.ResourceGroup, *replica.Name, propertiesReplica)
replicaId, err := parse.ServerID(*replica.ID)
if err != nil {
return fmt.Errorf("parsing Postgres Server Replica ID : %v", err)
}
future, err := client.Update(ctx, replicaId.ResourceGroup, replicaId.Name, propertiesReplica)
if err != nil {
return fmt.Errorf("upscaling PostgreSQL Server Replica %q (Resource Group %q): %+v", *replica.Name, id.ResourceGroup, err)
return fmt.Errorf("upscaling PostgreSQL Server Replica %q (Resource Group %q): %+v", replicaId.Name, replicaId.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)
return fmt.Errorf("waiting for update of PostgreSQL Server Replica %q (Resource Group %q): %+v", replicaId.Name, replicaId.ResourceGroup, err)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,32 +752,42 @@ func (r PostgreSQLServerResource) createReplica(data acceptance.TestData, sku st
return fmt.Sprintf(`
%[1]s

resource "azurerm_resource_group" "replica" {
name = "acctestRG-psql-%[2]d-replica"
location = "%[3]s"
}

resource "azurerm_postgresql_server" "replica" {
name = "acctest-psql-server-%[2]d-replica"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
location = "%[3]s"
resource_group_name = azurerm_resource_group.replica.name

sku_name = "%[3]s"
sku_name = "%[4]s"
version = "11"

create_mode = "Replica"
creation_source_server_id = azurerm_postgresql_server.test.id

ssl_enforcement_enabled = true
}
`, r.template(data, sku, "11"), data.RandomInteger, sku)
`, r.template(data, sku, "11"), data.RandomInteger, data.Locations.Secondary, sku)
}

func (r PostgreSQLServerResource) createReplicas(data acceptance.TestData, sku string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_resource_group" "replica1" {
name = "acctestRG-psql-%[2]d-replica1"
location = "%[3]s"
}

resource "azurerm_postgresql_server" "replica1" {
name = "acctest-psql-server-%[2]d-replica1"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
location = "%[3]s"
resource_group_name = azurerm_resource_group.replica1.name

sku_name = "%[3]s"
sku_name = "%[4]s"
version = "11"

create_mode = "Replica"
Expand All @@ -791,15 +801,15 @@ resource "azurerm_postgresql_server" "replica2" {
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

sku_name = "%[3]s"
sku_name = "%[4]s"
version = "11"

create_mode = "Replica"
creation_source_server_id = azurerm_postgresql_server.test.id

ssl_enforcement_enabled = true
}
`, r.template(data, sku, "11"), data.RandomInteger, sku)
`, r.template(data, sku, "11"), data.RandomInteger, data.Locations.Secondary, sku)
}

func (r PostgreSQLServerResource) createPointInTimeRestore(data acceptance.TestData, version, restoreTime string) string {
Expand Down