-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package apiutil | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/PagerDuty/terraform-provider-pagerduty/util" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" | ||
) | ||
|
||
// AllFunc is a signature to use with function `All`, it receives the current | ||
// number of items already listed, it returns a boolean signaling whether the | ||
// system should keep requesting more items, and an error if any occured. | ||
type AllFunc = func(offset int) (bool, error) | ||
|
||
// Limit is the maximum amount of items a single request to PagerDuty's API | ||
// should response | ||
const Limit = 100 | ||
|
||
// All provides a boilerplate to request all pages from a list of a resource | ||
// from PagerDuty's API | ||
func All(ctx context.Context, requestFn AllFunc) error { | ||
offset := 0 | ||
keepSearching := true | ||
|
||
for keepSearching { | ||
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError { | ||
more, err := requestFn(offset) | ||
|
||
if err != nil { | ||
if util.IsBadRequestError(err) { | ||
return retry.NonRetryableError(err) | ||
} | ||
return retry.RetryableError(err) | ||
} | ||
|
||
offset += Limit | ||
keepSearching = more | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |