Skip to content

Commit

Permalink
Merge pull request #894 from cjgajard/eras-nurupo
Browse files Browse the repository at this point in the history
Improvements for escalation policies
  • Loading branch information
cjgajard authored Jun 26, 2024
2 parents 7bdd60f + a40c834 commit 92a2e9b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
68 changes: 40 additions & 28 deletions pagerduty/data_source_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,54 @@ func dataSourcePagerDutyEscalationPolicyRead(d *schema.ResourceData, meta interf
log.Printf("[INFO] Reading PagerDuty escalation policy")

searchName := d.Get("name").(string)
var offset int = 0
var found *pagerduty.EscalationPolicy
more := true

o := &pagerduty.ListEscalationPoliciesOptions{
Query: searchName,
}

return retry.Retry(5*time.Minute, func() *retry.RetryError {
resp, _, err := client.EscalationPolicies.List(o)
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
return retry.NonRetryableError(err)
for more {
err := retry.Retry(5*time.Minute, func() *retry.RetryError {
o := &pagerduty.ListEscalationPoliciesOptions{
Query: searchName,
Limit: 100,
Offset: offset,
}

// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return retry.RetryableError(err)
}
resp, _, err := client.EscalationPolicies.List(o)
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
return retry.NonRetryableError(err)
}

// Delaying retry by 30s as recommended by PagerDuty
// https://developer.pagerduty.com/docs/rest-api-v2/rate-limiting/#what-are-possible-workarounds-to-the-events-api-rate-limit
time.Sleep(30 * time.Second)
return retry.RetryableError(err)
}

var found *pagerduty.EscalationPolicy
offset += 100
more = resp.More

for _, policy := range resp.EscalationPolicies {
if policy.Name == searchName {
found = policy
break
for _, policy := range resp.EscalationPolicies {
if policy.Name == searchName {
found = policy
more = false
return nil
}
}
}

if found == nil {
return retry.NonRetryableError(
fmt.Errorf("Unable to locate any escalation policy with the name: %s", searchName),
)
return nil
})
if err != nil {
return err
}
}

if found == nil {
return fmt.Errorf("Unable to locate any escalation policy with the name: %s", searchName)
}

d.SetId(found.ID)
d.Set("name", found.Name)
d.SetId(found.ID)
d.Set("name", found.Name)

return nil
})
return nil
}
11 changes: 10 additions & 1 deletion pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ func resourcePagerDutyEscalationPolicyUpdate(d *schema.ResourceData, meta interf
// Removing the inclusion of escalation_rule_assignment_strategies for
// accounts wihtout the required entitlements.
for idx, er := range escalationPolicy.EscalationRules {
if er.EscalationRuleAssignmentStrategy == nil {
continue
}

if er.EscalationRuleAssignmentStrategy.Type == "round_robin" {
return fmt.Errorf("Round Robin Scheduling is available for accounts on the following pricing plans: Business, Digital Operations (legacy) and Enterprise for Incident Management. Therefore, set the escalation_rule_assignment_strategy to 'assign_to_everyone' for the escalation rule at index %d", idx)
}
Expand Down Expand Up @@ -287,7 +291,12 @@ func resourcePagerDutyEscalationPolicyDelete(d *schema.ResourceData, meta interf
return retry.RetryableError(err)
}

return retry.NonRetryableError(err)
err = handleNotFoundError(err, d)
if err != nil {
return retry.NonRetryableError(err)
}

return nil
}
return nil
})
Expand Down
2 changes: 0 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
"pagerduty_escalation_policy.foo", "rule.0.escalation_delay_in_minutes", "10"),
),
},

{
Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated(username, email, escalationPolicyUpdated),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -306,7 +305,6 @@ func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error {
if _, _, err := client.EscalationPolicies.Get(r.Primary.ID, &pagerduty.GetEscalationPolicyOptions{}); err == nil {
return fmt.Errorf("Escalation Policy still exists")
}

}
return nil
}
Expand Down

0 comments on commit 92a2e9b

Please sign in to comment.