Skip to content

Commit

Permalink
Add new retry wrapper fn, retry sql database instance operations that…
Browse files Browse the repository at this point in the history
… commonly 503 (#417)

* Retry sql database instance operations that commonly 503

* use new retry wrapper fn in resource_storage_bucket.go
  • Loading branch information
danawillow authored Oct 3, 2017
1 parent a776adc commit 2db85e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
18 changes: 12 additions & 6 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,20 +625,26 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
// If a default root user was created with a wildcard ('%') hostname, delete it. Note that if the resource is a
// replica, then any users are inherited from the master instance and should be left alone.
if !sqlResourceIsReplica(d) {
users, err := config.clientSqlAdmin.Users.List(project, instance.Name).Do()
var users *sqladmin.UsersListResponse
err = retry(func() error {
users, err = config.clientSqlAdmin.Users.List(project, instance.Name).Do()
return err
})
if err != nil {
return fmt.Errorf("Error, attempting to list users associated with instance %s: %s", instance.Name, err)
}
for _, u := range users.Items {
if u.Name == "root" && u.Host == "%" {
op, err = config.clientSqlAdmin.Users.Delete(project, instance.Name, u.Host, u.Name).Do()
err = retry(func() error {
op, err = config.clientSqlAdmin.Users.Delete(project, instance.Name, u.Host, u.Name).Do()
if err == nil {
err = sqladminOperationWait(config, op, project, "Delete default root User")
}
return err
})
if err != nil {
return fmt.Errorf("Error, failed to delete default 'root'@'*' user, but the database was created successfully: %s", err)
}
err = sqladminOperationWait(config, op, project, "Delete default root User")
if err != nil {
return err
}
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,9 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error

var res *storage.Bucket

err = resource.Retry(1*time.Minute, func() *resource.RetryError {
err = retry(func() error {
res, err = config.clientStorage.Buckets.Insert(project, sb).Do()
if err == nil {
return nil
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 429 {
return resource.RetryableError(gerr)
}
return resource.NonRetryableError(err)
return err
})

if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"log"
"regexp"
"strings"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
computeBeta "google.golang.org/api/compute/v0.beta"
Expand Down Expand Up @@ -331,3 +333,16 @@ func convertArrToMap(ifaceArr []interface{}) map[string]struct{} {
}
return sm
}

func retry(retryFunc func() error) error {
return resource.Retry(1*time.Minute, func() *resource.RetryError {
err := retryFunc()
if err == nil {
return nil
}
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
return resource.RetryableError(gerr)
}
return resource.NonRetryableError(err)
})
}

0 comments on commit 2db85e8

Please sign in to comment.