Skip to content

Commit

Permalink
spaces_bucket: Add retry logic to ensure bucket is available before p…
Browse files Browse the repository at this point in the history
…roceeding. (#512)

As we immediately apply additional configuration after creating the bucket,
we should first ensure that the bucket is available before leaving the create
method in order to protect against instablity like seen in #510.

Closes: #510
  • Loading branch information
andrewsomething authored Oct 28, 2020
1 parent 8ffa8fc commit 03f7077
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions digitalocean/resource_digitalocean_spaces_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ func resourceDigitalOceanBucketCreate(ctx context.Context, d *schema.ResourceDat

svc := s3.New(client)

name := d.Get("name").(string)
input := &s3.CreateBucketInput{
Bucket: aws.String(d.Get("name").(string)),
Bucket: aws.String(name),
ACL: aws.String(d.Get("acl").(string)),
}

err = resource.Retry(5*time.Minute, func() *resource.RetryError {
log.Printf("[DEBUG] Trying to create new Spaces bucket: %q", d.Get("name").(string))
log.Printf("[DEBUG] Trying to create new Spaces bucket: %q", name)
_, err := svc.CreateBucket(input)
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "OperationAborted" {
log.Printf("[WARN] Got an error while trying to create Spaces bucket %s: %s", d.Get("name").(string), err)
log.Printf("[WARN] Got an error while trying to create Spaces bucket %s: %s", name, err)
return resource.RetryableError(
fmt.Errorf("[WARN] Error creating Spaces bucket %s, retrying: %s",
d.Get("name").(string), err))
fmt.Errorf("[WARN] Error creating Spaces bucket %s, retrying: %s", name, err))
}
}
if err != nil {
Expand All @@ -231,6 +231,21 @@ func resourceDigitalOceanBucketCreate(ctx context.Context, d *schema.ResourceDat
if err != nil {
return diag.Errorf("Error creating Spaces bucket: %s", err)
}

err = resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := svc.HeadBucket(&s3.HeadBucketInput{Bucket: aws.String(name)})
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NotFound" {
log.Printf("[DEBUG] Waiting for Spaces bucket to be available: %q, retrying: %v", name, awsErr.Message())
return resource.RetryableError(err)
}
}
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})
log.Println("Bucket created")

d.SetId(d.Get("name").(string))
Expand Down

0 comments on commit 03f7077

Please sign in to comment.