Skip to content

Commit

Permalink
serviceusage: retry on 403's for operation calls (#5234) (#10171)
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 committed Sep 27, 2021
1 parent b9f9e81 commit 4d58b44
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/5234.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
serviceusage: enabled the service api to retry on failed operation calls in anticipation of transient errors that occur when first enabling the service.
```
25 changes: 22 additions & 3 deletions google/service_usage_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ package google
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"

"google.golang.org/api/googleapi"
)

type ServiceUsageOperationWaiter struct {
Config *Config
UserAgent string
Project string
Config *Config
UserAgent string
Project string
retryCount int
CommonOperationWaiter
}

Expand All @@ -36,6 +41,20 @@ func (w *ServiceUsageOperationWaiter) QueryOp() (interface{}, error) {
return sendRequest(w.Config, "GET", w.Project, url, w.UserAgent, nil)
}

func (w *ServiceUsageOperationWaiter) IsRetryable(err error) bool {
// Retries errors on 403 3 times if the error message
// returned contains `has not been used in project`
maxRetries := 3
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 403 {
if w.retryCount < maxRetries && strings.Contains(gerr.Body, "has not been used in project") {
w.retryCount += 1
log.Printf("[DEBUG] retrying on 403 %v more times", w.retryCount-maxRetries-1)
return true
}
}
return false
}

func createServiceUsageWaiter(config *Config, op map[string]interface{}, project, activity, userAgent string) (*ServiceUsageOperationWaiter, error) {
w := &ServiceUsageOperationWaiter{
Config: config,
Expand Down

0 comments on commit 4d58b44

Please sign in to comment.