-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gruntwork-io/v0.0.2
aws-nuke v0.0.2
- Loading branch information
Showing
21 changed files
with
1,099 additions
and
44 deletions.
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.