From ffd2e750f762da98b679702156a1261c06dd1e57 Mon Sep 17 00:00:00 2001 From: AJ Date: Tue, 17 Dec 2024 03:26:41 +0000 Subject: [PATCH] add support for `cloud-nuke-excluded` tag in VPC resource, fix #810 (#811) --- README.md | 2 +- aws/resources/ec2_vpc.go | 1 + aws/resources/ec2_vpc_test.go | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c4c96307..72358274 100644 --- a/README.md +++ b/README.md @@ -655,7 +655,7 @@ of the file that are supported are listed here. | transit-gateway | TransitGateway | ❌ | ✅ (Creation Time) | ❌ | ✅ | | transit-gateway-route-table | TransitGatewayRouteTable | ❌ | ✅ (Creation Time) | ❌ | ✅ | | transit-gateway-attachment | TransitGatewaysVpcAttachment | ❌ | ✅ (Creation Time) | ❌ | ✅ | -| vpc | VPC | ✅ (EC2 Name Tag) | ✅ (First Seen Tag Time) | ❌ | ❌ | +| vpc | VPC | ✅ (EC2 Name Tag) | ✅ (First Seen Tag Time) | ✅ | ❌ | | route53-hosted-zone | Route53HostedZone | ✅ (Hosted zone name) | ❌ | ❌ | ❌ | | route53-cidr-collection | Route53CIDRCollection | ✅ (Cidr collection name) | ❌ | ❌ | ❌ | | route53-traffic-policy | Route53TrafficPolicy | ✅ (Traffic policy name) | ❌ | ❌ | ❌ | diff --git a/aws/resources/ec2_vpc.go b/aws/resources/ec2_vpc.go index 12eaa629..abcfe3db 100644 --- a/aws/resources/ec2_vpc.go +++ b/aws/resources/ec2_vpc.go @@ -53,6 +53,7 @@ func (v *EC2VPCs) getAll(c context.Context, configObj config.Config) ([]*string, if configObj.VPC.ShouldInclude(config.ResourceValue{ Time: firstSeenTime, Name: util.GetEC2ResourceNameTagValue(vpc.Tags), + Tags: util.ConvertTypesTagsToMap(vpc.Tags), }) { ids = append(ids, vpc.VpcId) } diff --git a/aws/resources/ec2_vpc_test.go b/aws/resources/ec2_vpc_test.go index b5865677..597dd156 100644 --- a/aws/resources/ec2_vpc_test.go +++ b/aws/resources/ec2_vpc_test.go @@ -56,6 +56,71 @@ func (m mockedEC2VPCs) DescribeVpcEndpointServiceConfigurations(ctx context.Cont func (m mockedEC2VPCs) DeleteVpcEndpointServiceConfigurations(ctx context.Context, input *ec2.DeleteVpcEndpointServiceConfigurationsInput, optFns ...func(*ec2.Options)) (*ec2.DeleteVpcEndpointServiceConfigurationsOutput, error) { return &m.DeleteVpcEndpointServiceConfigurationsOutput, nil } + +func TestEC2VPC_Exclude_tag(t *testing.T) { + + t.Parallel() + + ctx := context.WithValue(context.Background(), util.ExcludeFirstSeenTagKey, true) + + testName1 := "test-vpc-name1" + testName2 := "test-vpc-name2" + testId1 := "test-vpc-id1" + testId2 := "test-vpc-id2" + vpc := EC2VPCs{ + Client: mockedEC2VPCs{ + DescribeVpcsOutput: ec2.DescribeVpcsOutput{ + Vpcs: []types.Vpc{ + { + VpcId: awsgo.String(testId1), + Tags: []types.Tag{ + { + Key: awsgo.String("Name"), + Value: awsgo.String(testName1), + }, + { + Key: awsgo.String("cloud-nuke-excluded"), + Value: awsgo.String("true"), + }, + }, + }, + { + VpcId: awsgo.String(testId2), + Tags: []types.Tag{ + { + Key: awsgo.String("Name"), + Value: awsgo.String(testName2), + }, + }, + }, + }, + }, + }, + } + + tests := map[string]struct { + ctx context.Context + configObj config.EC2ResourceType + expected []string + }{ + "emptyFilter": { + ctx: ctx, + configObj: config.EC2ResourceType{}, + expected: []string{testId2}, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + names, err := vpc.getAll(tc.ctx, config.Config{ + VPC: tc.configObj, + }) + require.NoError(t, err) + require.Equal(t, tc.expected, awsgo.ToStringSlice(names)) + }) + } + +} + func TestEC2VPC_GetAll(t *testing.T) { t.Parallel()