Skip to content

Commit 4d58b44

Browse files
serviceusage: retry on 403's for operation calls (#5234) (#10171)
Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent b9f9e81 commit 4d58b44

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

.changelog/5234.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
serviceusage: enabled the service api to retry on failed operation calls in anticipation of transient errors that occur when first enabling the service.
3+
```

google/service_usage_operation.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ package google
1616
import (
1717
"encoding/json"
1818
"fmt"
19+
"log"
20+
"strings"
1921
"time"
22+
23+
"google.golang.org/api/googleapi"
2024
)
2125

2226
type ServiceUsageOperationWaiter struct {
23-
Config *Config
24-
UserAgent string
25-
Project string
27+
Config *Config
28+
UserAgent string
29+
Project string
30+
retryCount int
2631
CommonOperationWaiter
2732
}
2833

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

44+
func (w *ServiceUsageOperationWaiter) IsRetryable(err error) bool {
45+
// Retries errors on 403 3 times if the error message
46+
// returned contains `has not been used in project`
47+
maxRetries := 3
48+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 403 {
49+
if w.retryCount < maxRetries && strings.Contains(gerr.Body, "has not been used in project") {
50+
w.retryCount += 1
51+
log.Printf("[DEBUG] retrying on 403 %v more times", w.retryCount-maxRetries-1)
52+
return true
53+
}
54+
}
55+
return false
56+
}
57+
3958
func createServiceUsageWaiter(config *Config, op map[string]interface{}, project, activity, userAgent string) (*ServiceUsageOperationWaiter, error) {
4059
w := &ServiceUsageOperationWaiter{
4160
Config: config,

0 commit comments

Comments
 (0)