|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/cloudformation" |
| 10 | + types "github.com/aws/aws-sdk-go-v2/service/cloudformation/types" |
| 11 | +) |
| 12 | + |
| 13 | +// TODO: implement AWS client wrappers, and incorporate this into the cfn:CreateStack call |
| 14 | +func WrapCFNStackFailure(ctx context.Context, cfnClient *cloudformation.Client, createStackErr error, stackName string) error { |
| 15 | + resourceByFailureMode := make(map[string][]string) |
| 16 | + eventsPaginator := cloudformation.NewDescribeStackEventsPaginator(cfnClient, &cloudformation.DescribeStackEventsInput{ |
| 17 | + StackName: &stackName, |
| 18 | + }) |
| 19 | + for eventsPaginator.HasMorePages() { |
| 20 | + page, err := eventsPaginator.NextPage(ctx) |
| 21 | + if err != nil { |
| 22 | + return createStackErr |
| 23 | + } |
| 24 | + for _, event := range page.StackEvents { |
| 25 | + if event.ResourceStatus == types.ResourceStatusCreateFailed { |
| 26 | + // TODO: filter out resource creation cancelled failures when there's an actual failure |
| 27 | + if _, ok := resourceByFailureMode[aws.ToString(event.ResourceStatusReason)]; !ok { |
| 28 | + resourceByFailureMode[aws.ToString(event.ResourceStatusReason)] = []string{} |
| 29 | + } |
| 30 | + resourceByFailureMode[aws.ToString(event.ResourceStatusReason)] = append(resourceByFailureMode[aws.ToString(event.ResourceStatusReason)], aws.ToString(event.LogicalResourceId)) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + var enhancedDetails []string |
| 35 | + for reason, resources := range resourceByFailureMode { |
| 36 | + enhancedDetails = append(enhancedDetails, fmt.Sprintf("%s: %s", strings.Join(resources, ","), reason)) |
| 37 | + } |
| 38 | + return fmt.Errorf("%w: %s", createStackErr, strings.Join(enhancedDetails, "--")) |
| 39 | +} |
0 commit comments