Skip to content

Commit

Permalink
add support for cloud-nuke-excluded tag in VPC resource, fix #810 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wakeful authored Dec 17, 2024
1 parent d434a9c commit ffd2e75
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | ❌ | ❌ | ❌ |
Expand Down
1 change: 1 addition & 0 deletions aws/resources/ec2_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
65 changes: 65 additions & 0 deletions aws/resources/ec2_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ffd2e75

Please sign in to comment.