Skip to content

Commit

Permalink
Retry SQL delete calls on 409 (#2811)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and chrisst committed Jan 8, 2019
1 parent 43d40f4 commit b31626a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"

"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

func resourceSqlDatabase() *schema.Resource {
Expand Down Expand Up @@ -206,10 +206,10 @@ func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))

var op *sqladmin.Operation
err = retryTime(func() error {
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Databases.Delete(project, instance_name, database_name).Do()
return err
}, 5 /* minutes */)
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete"+
Expand Down
8 changes: 6 additions & 2 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"

"google.golang.org/api/googleapi"
"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

const privateNetworkLinkRegex = "projects/(" + ProjectRegex + ")/global/networks/((?:[a-z](?:[-a-z0-9]*[a-z0-9])?))$"
Expand Down Expand Up @@ -782,7 +782,11 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, v.(string)))
}

op, err := config.clientSqlAdmin.Instances.Delete(project, d.Get("name").(string)).Do()
var op *sqladmin.Operation
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Instances.Delete(project, d.Get("name").(string)).Do()
return err
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete instance %s: %s", d.Get("name").(string), err)
Expand Down
9 changes: 7 additions & 2 deletions google/resource_sql_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/sqladmin/v1beta4"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

func resourceSqlUser() *schema.Resource {
Expand Down Expand Up @@ -206,7 +206,12 @@ func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error {

mutexKV.Lock(instanceMutexKey(project, instance))
defer mutexKV.Unlock(instanceMutexKey(project, instance))
op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()

var op *sqladmin.Operation
err = retryTimeDuration(func() error {
op, err = config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()
return err
}, d.Timeout(schema.TimeoutDelete))

if err != nil {
return fmt.Errorf("Error, failed to delete"+
Expand Down
3 changes: 2 additions & 1 deletion google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration) error {
}

func isRetryableError(err error) bool {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
// 409's are retried because cloud sql throws a 409 when concurrent calls are made
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 409 || gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
return true
}
return false
Expand Down

0 comments on commit b31626a

Please sign in to comment.