magodo / terraform-provider-restful

Terraform provider to manage RESTful resources
https://registry.terraform.io/providers/magodo/restful
Mozilla Public License 2.0
15 stars 5 forks source link

Add retry policy for the main client #74

Closed LaurentLesle closed 11 months ago

LaurentLesle commented 11 months ago

By default the restful provider is retrying operations for errors.

Impacted resources:

Here a pseudo code that would address this issue. If the post operation returns 429 or 409, the provider will resend the POST request until count or success.

resource "restful_operation" "register_rp" {
  path = format("/subscriptions/%s/providers/Microsoft.ProviderHub/register", var.subscription_id)
  query = {
    api-version = ["2014-04-01-preview"]
  }
  method = "POST"

  # NEW pseudo config
  retry = {
   count = 10
   status_locator = "code"
   status = {
     pending = ["409", "429"]
   }
 }
  poll = {
    url_locator    = format("exact./subscriptions/%s/providers/Microsoft.ProviderHub?api-version=2014-04-01-preview", var.subscription_id)
    status_locator = "body.registrationState"
    status = {
      success = "Registered"
      pending = ["Registering"]
    }
  }
}

client.go (pseudo code)

    retryStatusCodes := []int{409, 429}

    client := resty.New()
    if opt.Security != nil {
        var err error
        client, err = opt.Security.newClient(ctx, httpClient)
        if err != nil {
            return nil, err
        }
    }

    client.
        SetRetryCount(10).
        SetRetryWaitTime(30 * time.Second).
        AddRetryCondition(func(r *resty.Response, err error) bool {
            if err != nil {
                return true
            }
            // Retry if the status code is in the retryStatusCodes slice
            for _, statusCode := range retryStatusCodes {
                if r.StatusCode() == statusCode {
                    return true
                }
            }
            return false
        })

    if _, err := url.Parse(baseURL); err != nil {
        return nil, err
    }
magodo commented 11 months ago

@LaurentLesle Would you like to test out #75?

LaurentLesle commented 11 months ago

all working. Thanks