Skip to content

Commit

Permalink
use errwrap.Walk (#3165) (#372)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Feb 25, 2020
1 parent 594208e commit 0642214
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
13 changes: 4 additions & 9 deletions google/common_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package google
import (
"fmt"
"log"
"net/url"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/googleapi"
)

type Waiter interface {
Expand Down Expand Up @@ -103,14 +101,11 @@ func CommonRefreshFunc(w Waiter) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
op, err := w.QueryOp()
if err != nil {
for _, e := range getAllTypes(err, &googleapi.Error{}, &url.Error{}) {
// Retry 404 when getting operation (not resource state)
if isRetryableError(e, isNotFoundRetryableError("GET operation")) {
log.Printf("[DEBUG] Dismissed error on GET of operation '%v' retryable: %s", w.OpName(), err)
return nil, "done: false", nil
}
// Retry 404 when getting operation (not resource state)
if isRetryableError(err, isNotFoundRetryableError("GET operation")) {
log.Printf("[DEBUG] Dismissed retryable error on GET operation %q: %s", w.OpName(), err)
return nil, "done: false", nil
}

return nil, "", fmt.Errorf("error while retrieving operation: %s", err)
}

Expand Down
27 changes: 8 additions & 19 deletions google/retry_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package google

import (
"log"
"net/url"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"google.golang.org/api/googleapi"
)

func retry(retryFunc func() error) error {
Expand All @@ -31,31 +29,22 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration, errorRetr
})
}

func getAllTypes(err error, args ...interface{}) []error {
var result []error
for _, v := range args {
subResult := errwrap.GetAllType(err, v)
if subResult != nil {
result = append(result, subResult...)
}
}
return result
}

func isRetryableError(topErr error, customPredicates ...RetryErrorPredicateFunc) bool {
retryPredicates := append(
// Global error retry predicates are registered in this default list.
defaultErrorRetryPredicates,
customPredicates...)

// Check all wrapped errors for a retryable error status.
for _, err := range getAllTypes(topErr, &googleapi.Error{}, &url.Error{}) {
isRetryable := false
errwrap.Walk(topErr, func(werr error) {
for _, pred := range retryPredicates {
if retry, reason := pred(err); retry {
log.Printf("[DEBUG] Dismissed an error as retryable. %s - %s", reason, err)
return true
if predRetry, predReason := pred(werr); predRetry {
log.Printf("[DEBUG] Dismissed an error as retryable. %s - %s", predReason, werr)
isRetryable = true
return
}
}
}
return false
})
return isRetryable
}

0 comments on commit 0642214

Please sign in to comment.