Skip to content

Commit

Permalink
Merge pull request #677 from imjaroiswebdev/issue-273-service-event-c…
Browse files Browse the repository at this point in the history
…onsistency-issue

Handle retries and state drift clean up for Escalation Policy
  • Loading branch information
imjaroiswebdev authored Apr 17, 2023
2 parents 7970c86 + ed73f84 commit d9977b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions pagerduty/resource_pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit d9977b1

Please sign in to comment.