Skip to content

Commit 6d90a00

Browse files
committed
DRY adjustments
1 parent 709698f commit 6d90a00

File tree

4 files changed

+40
-81
lines changed

4 files changed

+40
-81
lines changed

internal/client/cloud_client.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func (c *CloudClient) UpdateResource(resource Resource, endpoint string, body an
150150
return nil, err
151151
}
152152

153-
// Handle the 412 Precondition Failed error by retrying with the latest ETag
154-
if respWithETag.Response.StatusCode == http.StatusPreconditionFailed {
153+
// Handle retryable errors (412 Precondition Failed, 409 Conflict) by refreshing ETag and retrying
154+
retryWithFreshETag := func(errorContext string) error {
155155
// Close the body of the first response
156156
if respWithETag.Response.Body != nil {
157157
_ = respWithETag.Response.Body.Close()
@@ -160,30 +160,21 @@ func (c *CloudClient) UpdateResource(resource Resource, endpoint string, body an
160160
// Get the latest ETag
161161
latestETag, err := getLatestETag()
162162
if err != nil {
163-
return nil, fmt.Errorf("failed to get latest ETag for retry: %w", err)
163+
return fmt.Errorf("failed to get latest ETag for retry (%s): %w", errorContext, err)
164164
}
165165

166166
// Retry the update with the latest ETag
167167
respWithETag, err = updateWithETag(latestETag)
168-
if err != nil {
169-
return nil, err
170-
}
168+
return err
171169
}
172170

173-
if respWithETag.Response.StatusCode == http.StatusConflict {
174-
// Close the body of the first response
175-
if respWithETag.Response.Body != nil {
176-
_ = respWithETag.Response.Body.Close()
177-
}
178-
179-
latestETag, err := getLatestETag()
180-
if err != nil {
181-
return nil, fmt.Errorf("failed to get latest ETag after FGAM configuration change: %w", err)
171+
switch respWithETag.Response.StatusCode {
172+
case http.StatusPreconditionFailed:
173+
if err := retryWithFreshETag("precondition failed"); err != nil {
174+
return nil, err
182175
}
183-
184-
// Retry the update with the fresh ETag
185-
respWithETag, err = updateWithETag(latestETag)
186-
if err != nil {
176+
case http.StatusConflict:
177+
if err := retryWithFreshETag("FGAM configuration change"); err != nil {
187178
return nil, err
188179
}
189180
}

internal/client/policy.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ func (c *CloudClient) UpdatePolicy(policy *models.Policy, etag string) (*PolicyW
149149
return nil, err
150150
}
151151

152-
// Handle the 412 Precondition Failed error by retrying with the latest ETag
153-
if respWithETag.Response.StatusCode == http.StatusPreconditionFailed {
152+
// Handle retryable errors (412 Precondition Failed, 409 Conflict) by refreshing ETag and retrying
153+
retryWithFreshETag := func(errorContext string) error {
154154
// Close the body of the first response
155155
if respWithETag.Response.Body != nil {
156156
_ = respWithETag.Response.Body.Close()
@@ -159,31 +159,21 @@ func (c *CloudClient) UpdatePolicy(policy *models.Policy, etag string) (*PolicyW
159159
// Get the latest ETag
160160
latestETag, err := getLatestETag()
161161
if err != nil {
162-
return nil, fmt.Errorf("failed to get latest ETag for retry: %w", err)
162+
return fmt.Errorf("failed to get latest ETag for retry (%s): %w", errorContext, err)
163163
}
164164

165165
// Retry the update with the latest ETag
166166
respWithETag, err = updateWithETag(latestETag)
167-
if err != nil {
168-
return nil, err
169-
}
167+
return err
170168
}
171169

172-
// Handle 409 Conflict error, FGAM config changes, by retrying with fresh ETag
173-
if respWithETag.Response.StatusCode == http.StatusConflict {
174-
if respWithETag.Response.Body != nil {
175-
_ = respWithETag.Response.Body.Close()
176-
}
177-
178-
// Get the latest ETag after FGAM configuration change
179-
latestETag, err := getLatestETag()
180-
if err != nil {
181-
return nil, fmt.Errorf("failed to get latest ETag after FGAM configuration change: %w", err)
170+
switch respWithETag.Response.StatusCode {
171+
case http.StatusPreconditionFailed:
172+
if err := retryWithFreshETag("precondition failed"); err != nil {
173+
return nil, err
182174
}
183-
184-
// Retry the update with the fresh ETag
185-
respWithETag, err = updateWithETag(latestETag)
186-
if err != nil {
175+
case http.StatusConflict:
176+
if err := retryWithFreshETag("FGAM configuration change"); err != nil {
187177
return nil, err
188178
}
189179
}

internal/client/role.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func (c *CloudClient) UpdateRole(role *models.Role, etag string) (*RoleWithETag,
156156
return nil, err
157157
}
158158

159-
// Handle the 412 Precondition Failed error by retrying with the latest ETag
160-
if respWithETag.Response.StatusCode == http.StatusPreconditionFailed {
159+
// Handle retryable errors (412 Precondition Failed, 409 Conflict) by refreshing ETag and retrying
160+
retryWithFreshETag := func(errorContext string) error {
161161
// Close the body of the first response
162162
if respWithETag.Response.Body != nil {
163163
_ = respWithETag.Response.Body.Close()
@@ -166,32 +166,21 @@ func (c *CloudClient) UpdateRole(role *models.Role, etag string) (*RoleWithETag,
166166
// Get the latest ETag
167167
latestETag, err := getLatestETag()
168168
if err != nil {
169-
return nil, fmt.Errorf("failed to get latest ETag for retry: %w", err)
169+
return fmt.Errorf("failed to get latest ETag for retry (%s): %w", errorContext, err)
170170
}
171171

172172
// Retry the update with the latest ETag
173173
respWithETag, err = updateWithETag(latestETag)
174-
if err != nil {
175-
return nil, err
176-
}
174+
return err
177175
}
178176

179-
// Handle 409 Conflict error (FGAM configuration changes) by retrying with fresh ETag
180-
if respWithETag.Response.StatusCode == http.StatusConflict {
181-
// Close the body of the first response
182-
if respWithETag.Response.Body != nil {
183-
_ = respWithETag.Response.Body.Close()
184-
}
185-
186-
// Get the latest ETag after FGAM configuration change
187-
latestETag, err := getLatestETag()
188-
if err != nil {
189-
return nil, fmt.Errorf("failed to get latest ETag after FGAM configuration change: %w", err)
177+
switch respWithETag.Response.StatusCode {
178+
case http.StatusPreconditionFailed:
179+
if err := retryWithFreshETag("precondition failed"); err != nil {
180+
return nil, err
190181
}
191-
192-
// Retry the update with the fresh ETag
193-
respWithETag, err = updateWithETag(latestETag)
194-
if err != nil {
182+
case http.StatusConflict:
183+
if err := retryWithFreshETag("FGAM configuration change"); err != nil {
195184
return nil, err
196185
}
197186
}

internal/client/service_account.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func (c *CloudClient) UpdateServiceAccount(serviceAccount *models.ServiceAccount
172172
return nil, err
173173
}
174174

175-
// Handle the 412 Precondition Failed error by retrying with the latest ETag
176-
if respWithETag.Response.StatusCode == http.StatusPreconditionFailed {
175+
// Handle retryable errors (412 Precondition Failed, 409 Conflict) by refreshing ETag and retrying
176+
retryWithFreshETag := func(errorContext string) error {
177177
// Close the body of the first response
178178
if respWithETag.Response.Body != nil {
179179
_ = respWithETag.Response.Body.Close()
@@ -182,32 +182,21 @@ func (c *CloudClient) UpdateServiceAccount(serviceAccount *models.ServiceAccount
182182
// Get the latest ETag
183183
latestETag, err := getLatestETag()
184184
if err != nil {
185-
return nil, fmt.Errorf("failed to get latest ETag for retry: %w", err)
185+
return fmt.Errorf("failed to get latest ETag for retry (%s): %w", errorContext, err)
186186
}
187187

188188
// Retry the update with the latest ETag
189189
respWithETag, err = updateWithETag(latestETag)
190-
if err != nil {
191-
return nil, err
192-
}
190+
return err
193191
}
194192

195-
// Handle 409 Conflict error, FGAM config changes, by retrying with fresh ETag
196-
if respWithETag.Response.StatusCode == http.StatusConflict {
197-
// Close the body of the first response
198-
if respWithETag.Response.Body != nil {
199-
_ = respWithETag.Response.Body.Close()
200-
}
201-
202-
// Get the latest ETag after FGAM config change
203-
latestETag, err := getLatestETag()
204-
if err != nil {
205-
return nil, fmt.Errorf("failed to get latest ETag after FGAM configuration change: %w", err)
193+
switch respWithETag.Response.StatusCode {
194+
case http.StatusPreconditionFailed:
195+
if err := retryWithFreshETag("precondition failed"); err != nil {
196+
return nil, err
206197
}
207-
208-
// Retry the update with the fresh ETag
209-
respWithETag, err = updateWithETag(latestETag)
210-
if err != nil {
198+
case http.StatusConflict:
199+
if err := retryWithFreshETag("FGAM configuration change"); err != nil {
211200
return nil, err
212201
}
213202
}

0 commit comments

Comments
 (0)