From 8d82af54650617c9af69e3efe9882a8005ef7775 Mon Sep 17 00:00:00 2001 From: Jeremiah Lusby Date: Fri, 9 Oct 2020 11:44:12 -0400 Subject: [PATCH 1/2] Update pagerduty_escalation_policy and pagerduty_service resource reads to retry up to six times on receiving a 404 Not Found. --- pagerduty/provider.go | 4 ++++ pagerduty/resource_pagerduty_escalation_policy.go | 13 ++++++++++--- pagerduty/resource_pagerduty_service.go | 12 +++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pagerduty/provider.go b/pagerduty/provider.go index ab990153a..3da6629b4 100644 --- a/pagerduty/provider.go +++ b/pagerduty/provider.go @@ -92,6 +92,10 @@ func handleNotFoundError(err error, d *schema.ResourceData) error { return fmt.Errorf("Error reading: %s: %s", d.Id(), err) } +func handleError(err error, d *schema.ResourceData) error { + return fmt.Errorf("Error reading: %s: %s", d.Id(), err) +} + func providerConfigure(data *schema.ResourceData, terraformVersion string) (interface{}, error) { config := Config{ SkipCredsValidation: data.Get("skip_credentials_validation").(bool), diff --git a/pagerduty/resource_pagerduty_escalation_policy.go b/pagerduty/resource_pagerduty_escalation_policy.go index af56e9c23..a6419e39c 100644 --- a/pagerduty/resource_pagerduty_escalation_policy.go +++ b/pagerduty/resource_pagerduty_escalation_policy.go @@ -118,14 +118,21 @@ func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interf func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) - log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id()) - o := &pagerduty.GetEscalationPolicyOptions{} + attempts := 0 return resource.Retry(2*time.Minute, func() *resource.RetryError { + attempts = attempts + 1 + + log.Printf("[INFO] Reading PagerDuty Escalation Policy %s. Attempt %i", d.Id(), attempts) escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o) if err != nil { - errResp := handleNotFoundError(err, d) + log.Printf("[WARN] Escalation Policy read error") + errResp := handleError(err, d) + if attempts > 6 { + errResp = handleNotFoundError(err, d) + } + if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index b8dd71e7b..517d94f8f 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -296,13 +296,19 @@ func resourcePagerDutyServiceCreate(d *schema.ResourceData, meta interface{}) er func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*pagerduty.Client) - log.Printf("[INFO] Reading PagerDuty service %s", d.Id()) - + attempts := 0 return resource.Retry(2*time.Minute, func() *resource.RetryError { + attempts = attempts + 1 + + log.Printf("[INFO] Reading PagerDuty Service %s. Attempt %i", d.Id(), attempts) service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{}) if err != nil { log.Printf("[WARN] Service read error") - errResp := handleNotFoundError(err, d) + errResp := handleError(err, d) + if attempts > 6 { + errResp = handleNotFoundError(err, d) + } + if errResp != nil { time.Sleep(2 * time.Second) return resource.RetryableError(errResp) From ab6c82b79071f567c5ac9322b80d4da067ca359b Mon Sep 17 00:00:00 2001 From: Jeremiah Lusby Date: Fri, 9 Oct 2020 13:28:31 -0400 Subject: [PATCH 2/2] Fix message format --- pagerduty/resource_pagerduty_escalation_policy.go | 2 +- pagerduty/resource_pagerduty_service.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pagerduty/resource_pagerduty_escalation_policy.go b/pagerduty/resource_pagerduty_escalation_policy.go index a6419e39c..e99d93b4e 100644 --- a/pagerduty/resource_pagerduty_escalation_policy.go +++ b/pagerduty/resource_pagerduty_escalation_policy.go @@ -124,7 +124,7 @@ func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interfac return resource.Retry(2*time.Minute, func() *resource.RetryError { attempts = attempts + 1 - log.Printf("[INFO] Reading PagerDuty Escalation Policy %s. Attempt %i", d.Id(), attempts) + log.Printf("[INFO] Reading PagerDuty Escalation Policy %s. Attempt %s", d.Id(), attempts) escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o) if err != nil { log.Printf("[WARN] Escalation Policy read error") diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index 517d94f8f..1ae1ea09d 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -300,7 +300,7 @@ func resourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) erro return resource.Retry(2*time.Minute, func() *resource.RetryError { attempts = attempts + 1 - log.Printf("[INFO] Reading PagerDuty Service %s. Attempt %i", d.Id(), attempts) + log.Printf("[INFO] Reading PagerDuty Service %s. Attempt %s", d.Id(), attempts) service, _, err := client.Services.Get(d.Id(), &pagerduty.GetServiceOptions{}) if err != nil { log.Printf("[WARN] Service read error")