Skip to content

Commit

Permalink
Use pagination for data.pagerduty_escalation_policy
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Jun 26, 2024
1 parent 543dc2d commit a40c834
Showing 1 changed file with 40 additions and 28 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
}

0 comments on commit a40c834

Please sign in to comment.