Skip to content

Commit

Permalink
Add retry for storage bucket 412 (#3434) (#6235)
Browse files Browse the repository at this point in the history
* add retry for storage bucket 412

* format

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and emilymye committed Apr 30, 2020
1 parent 9d166ee commit 6756eb5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/3434.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: Added retries for `google_storage_bucket_iam_*` on 412 (precondition not met) errors for eventually consistent bucket creation.
```
7 changes: 7 additions & 0 deletions google/error_retry_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,10 @@ func isNotFoundRetryableError(opType string) RetryErrorPredicateFunc {
return false, ""
}
}

func isStoragePreconditionError(err error) (bool, string) {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 412 {
return true, fmt.Sprintf("Retry on storage precondition not met")
}
return false, ""
}
4 changes: 2 additions & 2 deletions google/iam_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (u *StorageBucketIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.
return nil, err
}

policy, err := sendRequest(u.Config, "GET", "", url, obj)
policy, err := sendRequest(u.Config, "GET", "", url, obj, isStoragePreconditionError)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand All @@ -129,7 +129,7 @@ func (u *StorageBucketIamUpdater) SetResourceIamPolicy(policy *cloudresourcemana
return err
}

_, err = sendRequestWithTimeout(u.Config, "PUT", "", url, obj, u.d.Timeout(schema.TimeoutCreate))
_, err = sendRequestWithTimeout(u.Config, "PUT", "", url, obj, u.d.Timeout(schema.TimeoutCreate), isStoragePreconditionError)
if err != nil {
return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err)
}
Expand Down

0 comments on commit 6756eb5

Please sign in to comment.