From 03f7077653c7ce1dda62fc88415a7b2dee8c1062 Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Wed, 28 Oct 2020 14:25:18 -0400 Subject: [PATCH] spaces_bucket: Add retry logic to ensure bucket is available before proceeding. (#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 --- .../resource_digitalocean_spaces_bucket.go | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/digitalocean/resource_digitalocean_spaces_bucket.go b/digitalocean/resource_digitalocean_spaces_bucket.go index e9d9f417b..bbbd9a4c0 100644 --- a/digitalocean/resource_digitalocean_spaces_bucket.go +++ b/digitalocean/resource_digitalocean_spaces_bucket.go @@ -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 { @@ -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))