cloudbeat
cloudbeat copied to clipboard
AWS SDK: Use paginators
Motivation The AWS go SDK provides paginators for list operation of its API: https://aws.github.io/aws-sdk-go-v2/docs/making-requests/#using-paginators
Instead, in our codebase, we are using the manual way of paginating which involves checking the NextToken in the output and copying to the input if not empty. This method is more error-prone (higher chance the developer forgets something) and more verbose.
I propose to switch to using the SDK paginators where applicable, for example:
diff --git a/resources/providers/awslib/iam/access_analyzer.go b/resources/providers/awslib/iam/access_analyzer.go
index b498cabf..fdfc28a7 100644
--- a/resources/providers/awslib/iam/access_analyzer.go
+++ b/resources/providers/awslib/iam/access_analyzer.go
@@ -85,17 +85,13 @@ func (p Provider) GetAccessAnalyzers(ctx context.Context) (awslib.AwsResource, e
func getAccessAnalyzersForRegion(ctx context.Context, region string, c AccessAnalyzerClient) (analyzersForRegion, error) {
analyzers := make([]types.AnalyzerSummary, 0)
- input := &accessanalyzer.ListAnalyzersInput{}
- for {
- out, err := c.ListAnalyzers(ctx, input)
+ paginator := accessanalyzer.NewListAnalyzersPaginator(c, &accessanalyzer.ListAnalyzersInput{})
+ for paginator.HasMorePages() {
+ out, err := paginator.NextPage(ctx)
if err != nil {
return analyzersForRegion{}, err
}
analyzers = append(analyzers, out.Analyzers...)
- if out.NextToken == nil {
- break
- }
- input.NextToken = out.NextToken
}
return analyzersForRegion{
Definition of done What needs to be completed at the end of this task
- [ ] Use paginators throughout the codebase instead of
NextToken - [ ] Tests do not change (no functional difference)