Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle retries and state drift clean up for Escalation Policy #677

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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