Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Skips deleted and in deletion NAT gateways from nuking, adds test for config file lookup #313

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aws/nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func getAllNatGateways(session *session.Session, excludeAfter time.Time, configO
return !lastPage
},
)

return allNatGateways, errors.WithStackTrace(err)
}

Expand All @@ -40,6 +41,11 @@ func shouldIncludeNatGateway(ngw *ec2.NatGateway, excludeAfter time.Time, config
return false
}

ngwState := aws.StringValue(ngw.State)
if ngwState == ec2.NatGatewayStateDeleted || ngwState == ec2.NatGatewayStateDeleting {
return false
}

if ngw.CreateTime != nil && excludeAfter.Before(*ngw.CreateTime) {
return false
}
Expand Down
60 changes: 59 additions & 1 deletion aws/nat_gateway_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package aws

import (
"regexp"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
terraws "github.com/gruntwork-io/terratest/modules/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -31,6 +34,51 @@ func TestListNatGateways(t *testing.T) {
assert.Contains(t, aws.StringValueSlice(natGatewayIDs), aws.StringValue(ngwID))
}

func createNatGatewayWithName(t *testing.T, svc *ec2.EC2, region string, name string) *string {
ngwID := createNatGateway(t, svc, region)

err := setTagsToResource(t, svc, ngwID, []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(name),
},
})
require.NoError(t, err)

return ngwID
}

func TestListNatGatewaysWithConfigFile(t *testing.T) {
t.Parallel()

region, err := getRandomRegion()
require.NoError(t, err)

session, err := session.NewSession(&aws.Config{Region: aws.String(region)})
require.NoError(t, err)
svc := ec2.New(session)

includedNatGatewayName := "cloud-nuke-test-include-" + util.UniqueID()
excludedNatGatewayName := "cloud-nuke-test-" + util.UniqueID()
includedNatGatewayID := createNatGatewayWithName(t, svc, region, includedNatGatewayName)
excludedNatGatewayID := createNatGatewayWithName(t, svc, region, excludedNatGatewayName)
defer nukeAllNatGateways(session, []*string{includedNatGatewayID, excludedNatGatewayID})

natGatewayIds, err := getAllNatGateways(session, time.Now().Add(1*time.Hour), config.Config{
NatGateway: config.ResourceType{
IncludeRule: config.FilterRule{
NamesRegExp: []config.Expression{
{RE: *regexp.MustCompile("^cloud-nuke-test-include-.*")},
},
},
},
})

require.NoError(t, err)
require.Equal(t, 1, len(natGatewayIds))
require.Equal(t, aws.StringValue(includedNatGatewayID), aws.StringValue(natGatewayIds[0]))
}

func TestTimeFilterExclusionNewlyCreatedNatGateway(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -109,6 +157,13 @@ func TestNukeNatGatewayMoreThanOne(t *testing.T) {
}

// Helper functions for driving the NAT gateway tests
func setTagsToResource(t *testing.T, svc *ec2.EC2, resourceId *string, tags []*ec2.Tag) error {
_, err := svc.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{resourceId},
Tags: tags,
})
return err
}

// createNatGateway will create a new NAT gateway in the default VPC
func createNatGateway(t *testing.T, svc *ec2.EC2, region string) *string {
Expand All @@ -119,10 +174,13 @@ func createNatGateway(t *testing.T, svc *ec2.EC2, region string) *string {
SubnetId: aws.String(subnet.Id),
ConnectivityType: aws.String(ec2.ConnectivityTypePrivate),
})
require.NoError(t, err)
if err != nil {
assert.Failf(t, "Could not create test NAT gateways", errors.WithStackTrace(err).Error())
}
if resp.NatGateway == nil {
t.Fatalf("Impossible error: AWS returned nil NAT gateway")
}

return resp.NatGateway.NatGatewayId
}

Expand Down