Skip to content

Commit

Permalink
More retries for sql_database_instance
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Aug 29, 2019
1 parent f985b92 commit d447100
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ func isFirstGen(d *schema.ResourceData) bool {
return !regexp.MustCompile("db*").Match([]byte(tier))
}

func isSqlOperationInProgressError(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 {
return true, "" //"fmt.Errorf("Error, failed to create instance %s with error code 409: %s. This may be due to a name collision - SQL instance names cannot be reused within a week.", instance.Name, err)"
}
return false, ""
}

func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down Expand Up @@ -490,11 +497,12 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, instance.MasterInstanceName))
}

op, err := config.clientSqlAdmin.Instances.Insert(project, instance).Do()
var op *sqladmin.Operation
err = retryTimeDuration(func() (operr error) {
op, operr = config.clientSqlAdmin.Instances.Insert(project, instance).Do()
return operr
}, d.Timeout(schema.TimeoutCreate), isSqlOperationInProgressError)
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 {
return fmt.Errorf("Error, failed to create instance %s with error code 409: %s. This may be due to a name collision - SQL instance names cannot be reused within a week.", instance.Name, err)
}
return fmt.Errorf("Error, failed to create instance %s: %s", instance.Name, err)
}

Expand All @@ -515,10 +523,10 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
// Users in a replica instance are inherited from the master instance and should be left alone.
if sqlDatabaseIsMaster(d) {
var users *sqladmin.UsersListResponse
err = retryTime(func() error {
err = retryTimeDuration(func() error {
users, err = config.clientSqlAdmin.Users.List(project, instance.Name).Do()
return err
}, 5)
}, d.Timeout(schema.TimeoutRead), isSqlOperationInProgressError)
if err != nil {
return fmt.Errorf("Error, attempting to list users associated with instance %s: %s", instance.Name, err)
}
Expand Down Expand Up @@ -701,13 +709,10 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
}

var instance *sqladmin.DatabaseInstance
err = retry(
func() error {
instance, err = config.clientSqlAdmin.Instances.Get(project, d.Id()).Do()
return err
},
)

err = retryTimeDuration(func() (rerr error) {
instance, rerr = config.clientSqlAdmin.Instances.Get(project, d.Id()).Do()
return rerr
}, d.Timeout(schema.TimeoutRead), isSqlOperationInProgressError)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("SQL Database Instance %q", d.Get("name").(string)))
}
Expand Down Expand Up @@ -781,7 +786,11 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, v.(string)))
}

op, err := config.clientSqlAdmin.Instances.Update(project, d.Get("name").(string), instance).Do()
var op *sqladmin.Operation
err = retryTimeDuration(func() (rerr error) {
op, rerr = config.clientSqlAdmin.Instances.Update(project, d.Get("name").(string), instance).Do()
return rerr
}, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError)
if err != nil {
return fmt.Errorf("Error, failed to update instance settings for %s: %s", instance.Name, err)
}
Expand Down Expand Up @@ -810,11 +819,10 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
}

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

if err != nil {
return fmt.Errorf("Error, failed to delete instance %s: %s", d.Get("name").(string), err)
}
Expand Down

0 comments on commit d447100

Please sign in to comment.