Skip to content

Commit

Permalink
add retry on db replica create (#907)
Browse files Browse the repository at this point in the history
* add retry on db replica create

* gofmt on resource_digitalocean_database_replica
  • Loading branch information
DMW2151 authored Nov 24, 2022
1 parent 9eced33 commit 2a9921d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
30 changes: 26 additions & 4 deletions digitalocean/resource_digitalocean_database_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -131,12 +132,29 @@ func resourceDigitalOceanDatabaseReplicaCreate(ctx context.Context, d *schema.Re
}

log.Printf("[DEBUG] DatabaseReplica create configuration: %#v", opts)
replica, _, err := client.Databases.CreateReplica(context.Background(), clusterId, opts)

var replicaCluster *godo.DatabaseReplica

// Retry requests that fail w. Failed Precondition (412). New DBs can be marked ready while
// first backup is still being created.
err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
rc, resp, err := client.Databases.CreateReplica(context.Background(), clusterId, opts)
if err != nil {
if resp.StatusCode == 412 {
return resource.RetryableError(err)
} else {
return resource.NonRetryableError(fmt.Errorf("Error creating DatabaseReplica: %s", err))
}
}
replicaCluster = rc
return nil
})

if err != nil {
return diag.Errorf("Error creating DatabaseReplica: %s", err)
return diag.FromErr(err)
}

replica, err = waitForDatabaseReplica(client, clusterId, "online", replica.Name)
replica, err := waitForDatabaseReplica(client, clusterId, "online", replicaCluster.Name)
if err != nil {
return diag.Errorf("Error creating DatabaseReplica: %s", err)
}
Expand Down Expand Up @@ -218,7 +236,11 @@ func waitForDatabaseReplica(client *godo.Client, cluster_id, status, name string
n := 0

for range ticker.C {
replica, _, err := client.Databases.GetReplica(context.Background(), cluster_id, name)
replica, resp, err := client.Databases.GetReplica(context.Background(), cluster_id, name)
if resp.StatusCode == 404 {
continue
}

if err != nil {
ticker.Stop()
return nil, fmt.Errorf("Error trying to read DatabaseReplica state: %s", err)
Expand Down
1 change: 0 additions & 1 deletion digitalocean/resource_digitalocean_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func resourceDigitalOceanVPCDelete(ctx context.Context, d *schema.ResourceData,
resp, err := client.VPCs.Delete(context.Background(), vpcID)
if err != nil {
// Retry if VPC still contains member resources to prevent race condition
// with database cluster deletion.
if resp.StatusCode == 403 {
return resource.RetryableError(err)
} else {
Expand Down

0 comments on commit 2a9921d

Please sign in to comment.