Skip to content

Commit

Permalink
retry on resourceNotReady (#197)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and danawillow committed Sep 12, 2019
1 parent 1409249 commit 5f4baa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions google/common_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Waiter interface {
// if the operation has no current error.
Error() error

// IsRetryable returns whether a given error should be retried.
IsRetryable(error) bool

// SetOp sets the operation we're waiting on in a Waiter struct so that it
// can be used in other methods.
SetOp(interface{}) error
Expand Down Expand Up @@ -59,6 +62,10 @@ func (w *CommonOperationWaiter) Error() error {
return nil
}

func (w *CommonOperationWaiter) IsRetryable(error) bool {
return false
}

func (w *CommonOperationWaiter) SetOp(op interface{}) error {
if err := Convert(op, &w.Op); err != nil {
return err
Expand Down Expand Up @@ -110,6 +117,10 @@ func CommonRefreshFunc(w Waiter) resource.StateRefreshFunc {
}

if err = w.Error(); err != nil {
if w.IsRetryable(err) {
log.Printf("[DEBUG] Retrying operation GET based on retryable err: %s", err)
return op, w.State(), nil
}
return nil, "", err
}

Expand Down
11 changes: 11 additions & 0 deletions google/compute_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ func (w *ComputeOperationWaiter) Error() error {
return nil
}

func (w *ComputeOperationWaiter) IsRetryable(err error) bool {
if oe, ok := err.(ComputeOperationError); ok {
for _, e := range oe.Errors {
if e.Code == "RESOURCE_NOT_READY" {
return true
}
}
}
return false
}

func (w *ComputeOperationWaiter) SetOp(op interface{}) error {
var ok bool
w.Op, ok = op.(*compute.Operation)
Expand Down

0 comments on commit 5f4baa9

Please sign in to comment.