provider/google: retry storage creation/deletion on rate limiting

Our GCP storage tests are really flaky right now due to rate limiting.
In theory, this could also impact Terraform users that are
deleting/creating large numbers of Google Cloud Storage buckets at once.

To fix, I'm detecting the specific error code that GCP returns when it's
a rate limit error, and using that with resource.Retry to try the
request again.
This commit is contained in:
Paddy 2017-03-02 16:42:28 -08:00
parent 452f47ce8c
commit 3cb5107bd2
1 changed files with 28 additions and 6 deletions

View File

@ -4,7 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/googleapi" "google.golang.org/api/googleapi"
@ -122,12 +124,23 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
} }
} }
call := config.clientStorage.Buckets.Insert(project, sb) var res *storage.Bucket
if v, ok := d.GetOk("predefined_acl"); ok {
call = call.PredefinedAcl(v.(string))
}
res, err := call.Do() err = resource.Retry(1*time.Minute, func() *resource.RetryError {
call := config.clientStorage.Buckets.Insert(project, sb)
if v, ok := d.GetOk("predefined_acl"); ok {
call = call.PredefinedAcl(v.(string))
}
res, err = call.Do()
if err == nil {
return nil
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 429 {
return resource.RetryableError(gerr)
}
return resource.NonRetryableError(err)
})
if err != nil { if err != nil {
fmt.Printf("Error creating bucket %s: %v", bucket, err) fmt.Printf("Error creating bucket %s: %v", bucket, err)
@ -260,7 +273,16 @@ func resourceStorageBucketDelete(d *schema.ResourceData, meta interface{}) error
} }
// remove empty bucket // remove empty bucket
err := config.clientStorage.Buckets.Delete(bucket).Do() err := resource.Retry(1*time.Minute, func() *resource.RetryError {
err := config.clientStorage.Buckets.Delete(bucket).Do()
if err == nil {
return nil
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 429 {
return resource.RetryableError(gerr)
}
return resource.NonRetryableError(err)
})
if err != nil { if err != nil {
fmt.Printf("Error deleting bucket %s: %v\n\n", bucket, err) fmt.Printf("Error deleting bucket %s: %v\n\n", bucket, err)
return err return err