Skip to content

Commit

Permalink
Parse 409 error strings to determine retriable status (#544)
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 Mar 22, 2019
1 parent af69df6 commit b6f0aac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 1 addition & 5 deletions google-beta/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})

op, err := config.clientSqlAdmin.Instances.Insert(project, instance).Do()
if err != nil {
if googleapiError, ok := err.(*googleapi.Error); ok && googleapiError.Code == 409 {
return fmt.Errorf("Error, the name %s is unavailable because it was used recently", instance.Name)
} else {
return fmt.Errorf("Error, failed to create instance %s: %s", instance.Name, err)
}
return fmt.Errorf("Error, failed to create instance %s: %s", instance.Name, err)
}

d.SetId(instance.Name)
Expand Down
11 changes: 9 additions & 2 deletions google-beta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,22 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration) error {
}

func isRetryableError(err error) bool {
// 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) {
if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
return true
}
// These operations are always hitting googleapis.com - they should rarely
// time out, and if they do, that timeout is retryable.
if urlerr, ok := err.(*url.Error); ok && urlerr.Timeout() {
return true
}
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 && strings.Contains(gerr.Body, "operationInProgress") {
// 409's are retried because cloud sql throws a 409 when concurrent calls are made.
// The only way right now to determine it is a SQL 409 due to concurrent calls is to
// look at the contents of the error message.
// See https://github.com/terraform-providers/terraform-provider-google/issues/3279
return true
}

return false
}

Expand Down

0 comments on commit b6f0aac

Please sign in to comment.