diff --git a/pagerduty/data_source_pagerduty_escalation_policy.go b/pagerduty/data_source_pagerduty_escalation_policy.go index 4fc01c7d7..503b55a65 100644 --- a/pagerduty/data_source_pagerduty_escalation_policy.go +++ b/pagerduty/data_source_pagerduty_escalation_policy.go @@ -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 }