Skip to content

Commit

Permalink
add a retry to all write operations (#7)
Browse files Browse the repository at this point in the history
* add a retry to all write operations

* refactor to retryRequestOnEventLock()
  • Loading branch information
tcarreira authored Jun 2, 2023
1 parent d7a819b commit 312487b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
30 changes: 30 additions & 0 deletions hcaas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package hcaas
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tsuruCmd "github.com/tsuru/tsuru/cmd"
)
Expand Down Expand Up @@ -73,3 +77,29 @@ func (h *hcaasProvider) serviceURL(serviceName, instance, path string) string {
q.Set("callback", fmt.Sprintf("/resources/%s/%s", instance, strings.TrimLeft(path, "/")))
return fmt.Sprintf("%s/services/%s/proxy/%s?%s", h.Host, serviceName, instance, q.Encode())
}

// retryRequestOnEventLock will retry the same request if the response is a 5xx containing a "event locked" in the response
func retryRequestOnEventLock(ctx context.Context, d *schema.ResourceData, req *http.Request) error {
return retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate)-time.Minute, func() *retry.RetryError {
resp, err := http.DefaultClient.Do(req)

if err != nil {
if strings.Contains(err.Error(), "event locked") {
return retry.RetryableError(err)
}
return retry.NonRetryableError(err)
}

defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

if resp.StatusCode >= http.StatusInternalServerError && strings.Contains(string(body), "event locked") {
return retry.RetryableError(err)
}

if resp.StatusCode >= http.StatusBadRequest {
return retry.NonRetryableError(fmt.Errorf("bad status code: %d, body: %q", resp.StatusCode, string(body)))
}
return nil
})
}
23 changes: 5 additions & 18 deletions hcaas/resource_hcaas_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,14 @@ func resourceHcaasGroupCreate(ctx context.Context, d *schema.ResourceData, meta

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}

d.SetId(r.Group)

return nil

}

func resourceHcaasGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -145,17 +138,11 @@ func resourceHcaasGroupDelete(ctx context.Context, d *schema.ResourceData, meta

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}
return nil
}

Expand Down
22 changes: 4 additions & 18 deletions hcaas/resource_hcaas_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,12 @@ func resourceHcaasURLCreate(ctx context.Context, d *schema.ResourceData, meta in

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}

d.SetId(r.URL)

return nil
}

Expand Down Expand Up @@ -162,17 +154,11 @@ func resourceHcaasURLDelete(ctx context.Context, d *schema.ResourceData, meta in

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}
return nil
}

Expand Down
21 changes: 4 additions & 17 deletions hcaas/resource_hcaas_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,12 @@ func resourceHcaasWatcherCreate(ctx context.Context, d *schema.ResourceData, met

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}

d.SetId(r.Watcher)

return nil
}

Expand Down Expand Up @@ -144,17 +136,12 @@ func resourceHcaasWatcherDelete(ctx context.Context, d *schema.ResourceData, met

req.Header.Set("Authorization", provider.Token)

resp, err := http.DefaultClient.Do(req)
err = retryRequestOnEventLock(ctx, d, req)

if err != nil {
return diag.FromErr(err)
}

defer resp.Body.Close()

if resp.StatusCode >= http.StatusBadRequest {
body, _ := ioutil.ReadAll(resp.Body)
return diag.Errorf("Bad status code: %d, body: %q", resp.StatusCode, string(body))
}
return nil
}

Expand Down

0 comments on commit 312487b

Please sign in to comment.