From c5ee563ca25c6812729c6b664940bde9a47a0ffb Mon Sep 17 00:00:00 2001 From: "k.goto" Date: Tue, 7 Mar 2023 20:40:53 +0900 Subject: [PATCH] feat: #81 Do not display nested child stacks (#121) --- internal/operation/cloudformation_stack.go | 5 ++ .../operation/cloudformation_stack_test.go | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/internal/operation/cloudformation_stack.go b/internal/operation/cloudformation_stack.go index e533cc18..8cd39e78 100644 --- a/internal/operation/cloudformation_stack.go +++ b/internal/operation/cloudformation_stack.go @@ -146,6 +146,11 @@ func (o *CloudFormationStackOperator) ListStacksFilteredByKeyword(ctx context.Co } for _, stackSummary := range stackSummaries { + // pass the nested child stacks + if stackSummary.RootId != nil { + continue + } + // for case-insensitive lowerStackName := strings.ToLower(*stackSummary.StackName) lowerKeyword := strings.ToLower(*keyword) diff --git a/internal/operation/cloudformation_stack_test.go b/internal/operation/cloudformation_stack_test.go index e353f5b2..077bc7c0 100644 --- a/internal/operation/cloudformation_stack_test.go +++ b/internal/operation/cloudformation_stack_test.go @@ -1613,6 +1613,65 @@ func TestCloudFormationStackOperator_ListStacksFilteredByKeyword(t *testing.T) { }, wantErr: false, }, + { + name: "list stacks with RootId filtered by keyword successfully", + args: args{ + ctx: ctx, + keyword: "TestStack", + }, + prepareMockCloudFormationFn: func(m *client.MockICloudFormation) { + m.EXPECT().ListStacks(gomock.Any()).Return( + []types.StackSummary{ + { + StackName: aws.String("TestStack1"), + StackStatus: types.StackStatusCreateComplete, + RootId: aws.String("test-stack-root"), + }, + { + StackName: aws.String("TestStack2"), + StackStatus: types.StackStatusCreateComplete, + }, + }, + nil, + ) + }, + want: want{ + filteredStacks: []string{ + "TestStack2", + }, + err: nil, + }, + wantErr: false, + }, + { + name: "list stacks with RootId filtered by keyword and no stacks do not have RootId found successfully", + args: args{ + ctx: ctx, + keyword: "TestStack", + }, + prepareMockCloudFormationFn: func(m *client.MockICloudFormation) { + m.EXPECT().ListStacks(gomock.Any()).Return( + []types.StackSummary{ + { + StackName: aws.String("TestStack1"), + StackStatus: types.StackStatusCreateComplete, + RootId: aws.String("test-stack-root"), + }, + { + StackName: aws.String("TestStack2"), + StackStatus: types.StackStatusCreateComplete, + RootId: aws.String("test-stack-root"), + }, + }, + nil, + ) + }, + want: want{ + filteredStacks: []string{}, + err: nil, + }, + wantErr: false, + }, { name: "list stacks filtered by lower keyword successfully", args: args{