Skip to content

Commit

Permalink
Merge pull request #874 from imjaroiswebdev/ds-user-paginate-results
Browse files Browse the repository at this point in the history
Address: Service DS not locating queried name when is not in first page of results
  • Loading branch information
imjaroiswebdev authored May 20, 2024
2 parents bf9f562 + f65c38b commit 58ebcee
Showing 1 changed file with 50 additions and 40 deletions.
90 changes: 50 additions & 40 deletions pagerduty/data_source_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,56 +75,66 @@ func dataSourcePagerDutyServiceRead(d *schema.ResourceData, meta interface{}) er

searchName := d.Get("name").(string)

lookupOffset := 0
more := true
o := &pagerduty.ListServicesOptions{
Query: searchName,
Limit: 100,
}

return retry.Retry(5*time.Minute, func() *retry.RetryError {
resp, _, err := client.Services.List(o)
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
return retry.NonRetryableError(err)
foundServices := make([]*pagerduty.Service, 0)

retryErr := retry.Retry(5*time.Minute, func() *retry.RetryError {
for more {
o.Offset = lookupOffset
resp, _, err := client.Services.List(o)
if err != nil {
if isErrCode(err, http.StatusBadRequest) {
return retry.NonRetryableError(err)
}

return retry.RetryableError(err)
}
more = resp.More
lookupOffset += resp.Limit

// 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)
foundServices = append(foundServices, resp.Services...)
}

var found *pagerduty.Service

for _, service := range resp.Services {
if service.Name == searchName {
found = service
break
}
}
return nil
})
if retryErr != nil {
return retryErr
}

if found == nil {
return retry.NonRetryableError(
fmt.Errorf("Unable to locate any service with the name: %s", searchName),
)
var found *pagerduty.Service
for _, service := range foundServices {
if service.Name == searchName {
found = service
break
}
}

var teams []map[string]interface{}
for _, team := range found.Teams {
teams = append(teams, map[string]interface{}{
"id": team.ID,
"name": team.Summary,
})
}
if found == nil {
return fmt.Errorf("Unable to locate any service with the name: %s", searchName)
}

d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("type", found.Type)
d.Set("auto_resolve_timeout", found.AutoResolveTimeout)
d.Set("acknowledgement_timeout", found.AcknowledgementTimeout)
d.Set("alert_creation", found.AlertCreation)
d.Set("description", found.Description)
d.Set("teams", teams)
d.Set("escalation_policy", found.EscalationPolicy.ID)
var teams []map[string]interface{}
for _, team := range found.Teams {
teams = append(teams, map[string]interface{}{
"id": team.ID,
"name": team.Summary,
})
}

return nil
})
d.SetId(found.ID)
d.Set("name", found.Name)
d.Set("type", found.Type)
d.Set("auto_resolve_timeout", found.AutoResolveTimeout)
d.Set("acknowledgement_timeout", found.AcknowledgementTimeout)
d.Set("alert_creation", found.AlertCreation)
d.Set("description", found.Description)
d.Set("teams", teams)
d.Set("escalation_policy", found.EscalationPolicy.ID)

return nil
}

0 comments on commit 58ebcee

Please sign in to comment.