Skip to content

Commit

Permalink
fix(sfn): handle pagination and ignore errors for expired executions …
Browse files Browse the repository at this point in the history
…history (#1934)

Signed-off-by: Patrick Decat <[email protected]>
  • Loading branch information
pdecat authored Feb 8, 2024
1 parent 5efefa0 commit be8bfe3
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions aws/table_aws_sfn_state_machine_execution_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package aws

import (
"context"
"errors"
"strconv"
"strings"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sfn"
"github.com/aws/aws-sdk-go-v2/service/sfn/types"
"github.com/aws/smithy-go"

sfnv1 "github.com/aws/aws-sdk-go/service/sfn"

Expand Down Expand Up @@ -346,20 +348,48 @@ func getRowDataForExecutionHistory(ctx context.Context, d *plugin.QueryData, arn
return nil, nil
}

params := &sfn.GetExecutionHistoryInput{
ExecutionArn: aws.String(arn),
maxLimit := int32(1000)
// If the requested number of items is less than the paging max limit
// set the limit to that instead
limit := d.QueryContext.Limit
if limit != nil {
if *limit < int64(maxLimit) {
maxLimit = int32(*limit)
}
}

var items []historyInfo

listHistory, err := svc.GetExecutionHistory(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_error", err)
return nil, err
input := &sfn.GetExecutionHistoryInput{
MaxResults: maxLimit,
ExecutionArn: aws.String(arn),
}
paginator := sfn.NewGetExecutionHistoryPaginator(svc, input, func(o *sfn.GetExecutionHistoryPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})
// List call
for paginator.HasMorePages() {
plugin.Logger(ctx).Trace("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_call GetExecutionHistory", arn)
output, err := paginator.NextPage(ctx)
if err != nil {
// Note: IgnoreConfig doesn't work when a ParentHydrate is used https://github.com/turbot/steampipe-plugin-sdk/issues/544
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
switch apiErr.(type) {
case *types.ExecutionDoesNotExist:
// Ignore expired executions for which history is no longer available
plugin.Logger(ctx).Trace("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_error ignore_expired", err)
return nil, nil
}
}
plugin.Logger(ctx).Error("aws_sfn_state_machine_execution_history.getRowDataForExecutionHistory", "api_error", err)
return nil, err
}

for _, event := range listHistory.Events {
items = append(items, historyInfo{event, arn})
for _, event := range output.Events {
items = append(items, historyInfo{event, arn})
}
}

return items, nil
Expand Down

0 comments on commit be8bfe3

Please sign in to comment.