From 4a7a07d71125e1a1e51404aaf1a7622e09323e89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 1 Dec 2023 17:39:19 -0300 Subject: [PATCH 1/2] update `heimweh/go-pagerduty` to `09be11e` --- go.mod | 2 +- go.sum | 4 ++-- .../heimweh/go-pagerduty/pagerduty/pagerduty.go | 17 +++++++++++++++++ vendor/modules.txt | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index e90cd21c4..ce3aefc75 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/hashicorp/terraform-exec v0.16.0 github.com/hashicorp/terraform-json v0.13.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.11.0 - github.com/heimweh/go-pagerduty v0.0.0-20231201163338-a11b2f79dfcf + github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea ) require ( diff --git a/go.sum b/go.sum index 48b359598..8acd45800 100644 --- a/go.sum +++ b/go.sum @@ -250,8 +250,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= -github.com/heimweh/go-pagerduty v0.0.0-20231201163338-a11b2f79dfcf h1:LyQb5eb/+R5VSA7aP5bBjDW4nHndclJQ2bG5N4lGmik= -github.com/heimweh/go-pagerduty v0.0.0-20231201163338-a11b2f79dfcf/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s= +github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea h1:JJnJ9l1XBIFepPShm8XNvbILSMVQW8sjeAKctexHots= +github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s= github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go b/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go index 957f38cf9..0c3aef5dd 100644 --- a/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go +++ b/vendor/github.com/heimweh/go-pagerduty/pagerduty/pagerduty.go @@ -9,7 +9,9 @@ import ( "log" "net/http" "net/url" + "strconv" "strings" + "time" "github.com/google/go-querystring/query" "github.com/heimweh/go-pagerduty/persistentconfig" @@ -568,6 +570,21 @@ func (c *Client) decodeErrorResponse(res *Response) error { v := &errorResponse{Error: &Error{ErrorResponse: res}} err := c.DecodeJSON(res, v) + // Delaying retry based on ratelimit-reset recommended by PagerDuty + // https://developer.pagerduty.com/docs/72d3b724589e3-rest-api-rate-limits#reaching-the-limit + ratelimitReset := res.Response.Header.Get("ratelimit-reset") + if res.Response.StatusCode == http.StatusTooManyRequests && ratelimitReset != "" { + waitFor, err := strconv.ParseInt(ratelimitReset, 10, 0) + if err == nil { + reqMethod := res.Response.Request.Method + reqEndpoint := res.Response.Request.URL + log.Printf("[INFO] Rate limit hit, throttling by %d seconds until next retry to %s: %s", waitFor, strings.ToUpper(reqMethod), reqEndpoint) + time.Sleep(time.Duration(waitFor) * time.Second) + v.Error.needToRetry = true + return v.Error + } + } + isUsingScopedAPITokenFromCredentials := *c.Config.APIAuthTokenType == AuthTokenTypeUseAppCredentials isOauthScopeMissing := isUsingScopedAPITokenFromCredentials && res.Response.StatusCode == http.StatusForbidden needNewOauthScopedAccessToken := isUsingScopedAPITokenFromCredentials && res.Response.StatusCode == http.StatusUnauthorized diff --git a/vendor/modules.txt b/vendor/modules.txt index 076a1d2f0..bda6064cb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -182,7 +182,7 @@ github.com/hashicorp/terraform-svchost # github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d ## explicit github.com/hashicorp/yamux -# github.com/heimweh/go-pagerduty v0.0.0-20231201163338-a11b2f79dfcf +# github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea ## explicit; go 1.17 github.com/heimweh/go-pagerduty/pagerduty github.com/heimweh/go-pagerduty/persistentconfig From 0f5c1213c5e3a59d487ee52f9e303f3ef1feee6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 1 Dec 2023 17:40:07 -0300 Subject: [PATCH 2/2] remove retries delays for service and service integration --- pagerduty/resource_pagerduty_service.go | 1 - pagerduty/resource_pagerduty_service_integration.go | 2 -- 2 files changed, 3 deletions(-) diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index 484fa57c1..ec657c223 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -480,7 +480,6 @@ func fetchService(d *schema.ResourceData, meta interface{}, errCallback func(err errResp := errCallback(err, d) if errResp != nil { - time.Sleep(2 * time.Second) return resource.RetryableError(errResp) } diff --git a/pagerduty/resource_pagerduty_service_integration.go b/pagerduty/resource_pagerduty_service_integration.go index 1746c78b8..40d84dc73 100644 --- a/pagerduty/resource_pagerduty_service_integration.go +++ b/pagerduty/resource_pagerduty_service_integration.go @@ -635,7 +635,6 @@ func fetchPagerDutyServiceIntegration(d *schema.ResourceData, meta interface{}, errResp := errCallback(err, d) if errResp != nil { - time.Sleep(2 * time.Second) return resource.RetryableError(errResp) } @@ -732,7 +731,6 @@ func resourcePagerDutyServiceIntegrationCreate(d *schema.ResourceData, meta inte retryErr := resource.Retry(1*time.Minute, func() *resource.RetryError { if serviceIntegration, _, err := client.Services.CreateIntegration(service, serviceIntegration); err != nil { if isErrCode(err, 400) { - time.Sleep(2 * time.Second) return resource.RetryableError(err) }