From ed73f84de0d382d2bea14cdaa1aa5e5b383e0eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Mon, 17 Apr 2023 16:34:59 -0400 Subject: [PATCH] update retries handling and state clean up for fetching escalation policy --- .../resource_pagerduty_escalation_policy.go | 19 ++++++++---- ...source_pagerduty_escalation_policy_test.go | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/pagerduty/resource_pagerduty_escalation_policy.go b/pagerduty/resource_pagerduty_escalation_policy.go index 6b1b57812..3893c4beb 100644 --- a/pagerduty/resource_pagerduty_escalation_policy.go +++ b/pagerduty/resource_pagerduty_escalation_policy.go @@ -130,7 +130,7 @@ func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interf } d.SetId(escalationPolicy.ID) - readErr = resourcePagerDutyEscalationPolicyRead(d, meta) + readErr = fetchEscalationPolicy(d, meta, genError) if readErr != nil { return resource.NonRetryableError(readErr) } @@ -139,20 +139,29 @@ func resourcePagerDutyEscalationPolicyCreate(d *schema.ResourceData, meta interf } func resourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id()) + return fetchEscalationPolicy(d, meta, handleNotFoundError) +} + +func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback func(error, *schema.ResourceData) error) error { client, err := meta.(*Config).Client() if err != nil { return err } - log.Printf("[INFO] Reading PagerDuty escalation policy: %s", d.Id()) - o := &pagerduty.GetEscalationPolicyOptions{} return resource.Retry(5*time.Minute, func() *resource.RetryError { escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o) if err != nil { - time.Sleep(2 * time.Second) - return resource.RetryableError(err) + errResp := errCallback(err, d) + log.Printf("[WARN] Escalation Policy read error") + if errResp != nil { + time.Sleep(2 * time.Second) + return resource.RetryableError(err) + } + + return nil } d.Set("name", escalationPolicy.Name) diff --git a/pagerduty/resource_pagerduty_escalation_policy_test.go b/pagerduty/resource_pagerduty_escalation_policy_test.go index 431959568..e15de4de5 100644 --- a/pagerduty/resource_pagerduty_escalation_policy_test.go +++ b/pagerduty/resource_pagerduty_escalation_policy_test.go @@ -96,6 +96,15 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) { "pagerduty_escalation_policy.foo", "rule.1.escalation_delay_in_minutes", "20"), ), }, + // Validating that externally removed escalation policies are detected and + // planed for re-creation + { + Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated(username, email, escalationPolicyUpdated), + Check: resource.ComposeTestCheckFunc( + testAccExternallyDestroyEscalationPolicy("pagerduty_escalation_policy.foo"), + ), + ExpectNonEmptyPlan: true, + }, }, }) } @@ -194,6 +203,26 @@ func testAccCheckPagerDutyEscalationPolicyExists(n string) resource.TestCheckFun } } +func testAccExternallyDestroyEscalationPolicy(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + if rs.Primary.ID == "" { + return fmt.Errorf("No Tag ID is set") + } + + client, _ := testAccProvider.Meta().(*Config).Client() + _, err := client.EscalationPolicies.Delete(rs.Primary.ID) + if err != nil { + return err + } + + return nil + } +} + func testAccCheckPagerDutyEscalationPolicyConfig(name, email, escalationPolicy string) string { return fmt.Sprintf(` resource "pagerduty_user" "foo" {