From aa36376fea630de0b6e7ca2232a456b85e5b9eb2 Mon Sep 17 00:00:00 2001 From: Josiah Purtlebaugh Date: Thu, 9 Jun 2022 12:31:18 +0900 Subject: [PATCH] Fix pagination for pagerduty_service data source #523 --- pagerduty/data_source_pagerduty_service.go | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/pagerduty/data_source_pagerduty_service.go b/pagerduty/data_source_pagerduty_service.go index 21a3531cf..316cb520c 100644 --- a/pagerduty/data_source_pagerduty_service.go +++ b/pagerduty/data_source_pagerduty_service.go @@ -39,28 +39,39 @@ func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) er o := &pagerduty.ListServicesOptions{ Query: searchName, + Limit: 100, } return resource.Retry(5*time.Minute, func() *resource.RetryError { - resp, _, err := client.Services.List(o) - if err != nil { - if isErrCode(err, 429) { - // 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 resource.RetryableError(err) - } + var found *pagerduty.Service - return resource.NonRetryableError(err) - } + for { + resp, _, err := client.Services.List(o) + if err != nil { + if isErrCode(err, 429) { + // 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 resource.RetryableError(err) + } - var found *pagerduty.Service + return resource.NonRetryableError(err) + } - for _, service := range resp.Services { - if service.Name == searchName { - found = service + for _, service := range resp.Services { + if service.Name == searchName { + found = service + break + } + } + + if found != nil { + break + } + if !resp.More { break } + o.Offset += 100 } if found == nil {