Skip to content

Commit

Permalink
Add retry for tsuru_pool_constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed May 27, 2021
1 parent 477ad47 commit c91be87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tsuru/resource_tsuru_pool_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func resourceTsuruPoolConstraintSet(ctx context.Context, d *schema.ResourceData,
Blacklist: d.Get("blacklist").(bool),
}

_, err := provider.TsuruClient.PoolApi.ConstraintSet(ctx, constraint)
err := tsuruRetry(ctx, d, func() error {
_, internalErr := provider.TsuruClient.PoolApi.ConstraintSet(ctx, constraint)
return internalErr
})

if err != nil {
return diag.Errorf("Could not set tsuru pool pool constraint: %q, err: %s", id, err.Error())
Expand Down Expand Up @@ -118,10 +121,15 @@ func resourceTsuruPoolConstraintDelete(ctx context.Context, d *schema.ResourceDa
provider := meta.(*tsuruProvider)

id := d.Get("pool_expr").(string) + "/" + d.Get("field").(string)
_, err := provider.TsuruClient.PoolApi.ConstraintSet(ctx, tsuru.PoolConstraintSet{
PoolExpr: d.Get("pool_expr").(string),
Field: d.Get("field").(string),
Values: []string{},

err := tsuruRetry(ctx, d, func() error {
_, internalErr := provider.TsuruClient.PoolApi.ConstraintSet(ctx, tsuru.PoolConstraintSet{
PoolExpr: d.Get("pool_expr").(string),
Field: d.Get("field").(string),
Values: []string{},
})

return internalErr
})

if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions tsuru/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package tsuru

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
tsuru_client "github.com/tsuru/go-tsuruclient/pkg/tsuru"
)

const ID_SEPARATOR = "::"
Expand All @@ -17,6 +21,22 @@ func isRetryableError(err []byte) bool {
return strings.Contains(e, "event locked")
}

func tsuruRetry(ctx context.Context, d *schema.ResourceData, f func() error) error {
return resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
err := f()
if err != nil {
var apiError tsuru_client.GenericOpenAPIError
if errors.As(err, &apiError) {
if isRetryableError(apiError.Body()) {
return resource.RetryableError(err)
}
}
return resource.NonRetryableError(err)
}
return nil
})
}

func createID(input []string) string {
return strings.TrimSpace(strings.Join(input, ID_SEPARATOR))
}
Expand Down

0 comments on commit c91be87

Please sign in to comment.