-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Add support for more resources #5
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
e84d0e4
Move EC2Instances type to ec2 specific types file
tonerdo 9649bae
Add functionality to nuke Auto scaling groups
tonerdo feebc33
Include ASGs in list of supported AWS resources
tonerdo 56101fa
Add tests for Auto Scaling Group nuking functionality
tonerdo 4d683b1
add functionality to nuke elastic load balancers
tonerdo 307d97b
add functionality to nuke V2 elastic load balancers
tonerdo 8bf39d3
add information about deleting load balancers
tonerdo 163170d
add functionality to nuke elastic block store volumes
tonerdo 7fb51a3
add information about deleting EBS volumes
tonerdo a29434e
add tests for nuking elb
tonerdo dcdc88d
ensure ec2 instance is terminated before proceeding
tonerdo eb82cf8
add tests for nuking of EBS volumes
tonerdo 05f79e4
ignore volume not found errors during nuking of ebs volumes
tonerdo 99c91a5
add test for nuking elbv2
tonerdo 1d5c534
ensure all ASGs have been nuked before nuke function returns
tonerdo b51e604
update ebs test to guard against nil pointer
tonerdo c95bad0
ensure elbv2 load balancer is successfully created
tonerdo fa9ad56
ensure ebs volume is deleted before returning from nuke method
tonerdo d7cd7ec
ensure elbv2 is deleted before returning from nuke function
tonerdo e8d7c5a
Run all tests in parallel
tonerdo 69b32f3
add funtion to return a random region and ignore reserved regions
tonerdo ab693b0
use random regions for tests
tonerdo a4048ef
add function to return a linux ami for a specific region
tonerdo e913e17
Move unique id function to separate go package file
tonerdo 90dd2c7
clean up resources created in list tests
tonerdo 2ceecca
add ability to exclude resources in certain regions from being deleted
tonerdo 5d1af22
change name of exclude region flag
tonerdo a3c38be
fix typo
tonerdo 88b5c0f
validate excluded regions
tonerdo 6187bab
createTestEC2Instance should wait for created instance to be in the r…
tonerdo 8bf36f9
improve logging and overall cli UX
tonerdo f18e711
wait until elb is deleted before returning from nuke function
tonerdo d6ce82c
use dynamically selected images to create ec2 instance
tonerdo 146c760
re-order cleanup defer calls
tonerdo e368ad4
use the logger package to print log all info
tonerdo b010c07
update InvalidFlagError to contain more info
tonerdo 9169791
distinguish between ec2 creation errors
tonerdo fa51aa5
update waitUntilElbDeleted function to only return success on LoadBal…
tonerdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package aws | ||
|
||
import ( | ||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/gruntwork-io/aws-nuke/logging" | ||
"github.com/gruntwork-io/gruntwork-cli/errors" | ||
) | ||
|
||
// Returns a formatted string of ASG Names | ||
func getAllAutoScalingGroups(session *session.Session, region string) ([]*string, error) { | ||
svc := autoscaling.New(session) | ||
result, err := svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{}) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
var groupNames []*string | ||
for _, group := range result.AutoScalingGroups { | ||
groupNames = append(groupNames, group.AutoScalingGroupName) | ||
} | ||
|
||
return groupNames, nil | ||
} | ||
|
||
// Deletes all Auto Scaling Groups | ||
func nukeAllAutoScalingGroups(session *session.Session, groupNames []*string) error { | ||
svc := autoscaling.New(session) | ||
|
||
if len(groupNames) == 0 { | ||
logging.Logger.Infof("No Auto Scaling Groups to nuke in region %s", *session.Config.Region) | ||
return nil | ||
} | ||
|
||
logging.Logger.Infof("Deleting all Auto Scaling Groups in region %s", *session.Config.Region) | ||
|
||
for _, groupName := range groupNames { | ||
params := &autoscaling.DeleteAutoScalingGroupInput{ | ||
AutoScalingGroupName: groupName, | ||
ForceDelete: awsgo.Bool(true), | ||
} | ||
|
||
_, err := svc.DeleteAutoScalingGroup(params) | ||
if err != nil { | ||
logging.Logger.Errorf("[Failed] %s", err) | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
logging.Logger.Infof("Deleted Auto Scaling Group: %s", *groupName) | ||
} | ||
|
||
err := svc.WaitUntilGroupNotExists(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: groupNames, | ||
}) | ||
|
||
if err != nil { | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
logging.Logger.Infof("[OK] %d Auto Scaling Group(s) deleted in %s", len(groupNames), *session.Config.Region) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/gruntwork-io/aws-nuke/util" | ||
"github.com/gruntwork-io/gruntwork-cli/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createTestAutoScalingGroup(t *testing.T, session *session.Session, name string) { | ||
svc := autoscaling.New(session) | ||
instance := createTestEC2Instance(t, session, name) | ||
|
||
param := &autoscaling.CreateAutoScalingGroupInput{ | ||
AutoScalingGroupName: &name, | ||
InstanceId: instance.InstanceId, | ||
MinSize: awsgo.Int64(1), | ||
MaxSize: awsgo.Int64(2), | ||
} | ||
|
||
_, err := svc.CreateAutoScalingGroup(param) | ||
if err != nil { | ||
assert.Failf(t, "Could not create test ASG: %s", errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
err = svc.WaitUntilGroupExists(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{&name}, | ||
}) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
} | ||
|
||
func TestListAutoScalingGroups(t *testing.T) { | ||
t.Parallel() | ||
|
||
region := getRandomRegion() | ||
session, err := session.NewSession(&awsgo.Config{ | ||
Region: awsgo.String(region)}, | ||
) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
groupName := "aws-nuke-test-" + util.UniqueID() | ||
createTestAutoScalingGroup(t, session, groupName) | ||
// clean up after this test | ||
defer nukeAllAutoScalingGroups(session, []*string{&groupName}) | ||
|
||
groupNames, err := getAllAutoScalingGroups(session, region) | ||
if err != nil { | ||
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups") | ||
} | ||
|
||
assert.Contains(t, awsgo.StringValueSlice(groupNames), groupName) | ||
} | ||
|
||
func TestNukeAutoScalingGroups(t *testing.T) { | ||
t.Parallel() | ||
|
||
region := getRandomRegion() | ||
session, err := session.NewSession(&awsgo.Config{ | ||
Region: awsgo.String(region)}, | ||
) | ||
svc := autoscaling.New(session) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
groupName := "aws-nuke-test-" + util.UniqueID() | ||
createTestAutoScalingGroup(t, session, groupName) | ||
|
||
_, err = svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{&groupName}, | ||
}) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
if err := nukeAllAutoScalingGroups(session, []*string{&groupName}); err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
groupNames, err := getAllAutoScalingGroups(session, region) | ||
if err != nil { | ||
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups") | ||
} | ||
|
||
assert.NotContains(t, awsgo.StringValueSlice(groupNames), groupName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package aws | ||
|
||
import ( | ||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/gruntwork-io/gruntwork-cli/errors" | ||
) | ||
|
||
// ASGroups - represents all auto scaling groups | ||
type ASGroups struct { | ||
GroupNames []string | ||
} | ||
|
||
// ResourceName - the simple name of the aws resource | ||
func (group ASGroups) ResourceName() string { | ||
return "asg" | ||
} | ||
|
||
// ResourceIdentifiers - The group names of the auto scaling groups | ||
func (group ASGroups) ResourceIdentifiers() []string { | ||
return group.GroupNames | ||
} | ||
|
||
// Nuke - nuke 'em all!!! | ||
func (group ASGroups) Nuke(session *session.Session) error { | ||
if err := nukeAllAutoScalingGroups(session, awsgo.StringSlice(group.GroupNames)); err != nil { | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the tests!
One issue: do the tests clean up after themselves? Or do they leave the instances / ASGs running? You should probably add a
defer
to each test that runs the appropriate clean up functions you already have in this codebase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically I run aws-nuke to clean up the resources the tests leave around. I feel it's a good exercise of the functionality of the tool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we'll be hooking up the tests to run automatically after every commit to the
aws-nuke
repo, so we can't rely on manual cleanup. I'd rather have the tests make an effort to clean up after themselves and we'll have a scheduled run of aws-nuke in our AWS accounts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List tests now cleanup after themselves