-
-
Notifications
You must be signed in to change notification settings - Fork 356
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
support deleting cloudwatch log groups #250
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,54 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" | ||
"github.com/gruntwork-io/cloud-nuke/logging" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
) | ||
|
||
func getAllCloudWatchLogGroups(session *session.Session, region string) ([]*string, error) { | ||
svc := cloudwatchlogs.New(session) | ||
|
||
output, err := svc.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{}) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
|
||
var names []*string | ||
for _, loggroup := range output.LogGroups { | ||
names = append(names, loggroup.LogGroupName) | ||
} | ||
|
||
return names, nil | ||
} | ||
|
||
func nukeAllCloudWatchLogGroups(session *session.Session, identifiers []*string) error { | ||
svc := cloudwatchlogs.New(session) | ||
|
||
if len(identifiers) == 0 { | ||
logging.Logger.Infof("No CloudWatch Log Groups to nuke in region %s", *session.Config.Region) | ||
return nil | ||
} | ||
|
||
logging.Logger.Infof("Deleting all CloudWatch Log Groups in region %s", *session.Config.Region) | ||
|
||
var deleteResources = 0 | ||
|
||
for _, name := range identifiers { | ||
_, err := svc.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{ | ||
LogGroupName: name, | ||
}) | ||
if err != nil { | ||
logging.Logger.Errorf("[Failed] %s", err) | ||
} else { | ||
logging.Logger.Infof("[OK] CloudWatch Log Group %s terminated in %s", *name, *session.Config.Region) | ||
deleteResources++ | ||
} | ||
|
||
} | ||
|
||
logging.Logger.Infof("[OK] %d CloudWatch Log Group(s) terminated in %s", deleteResources, *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,121 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" | ||
"github.com/gruntwork-io/cloud-nuke/util" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestListCloudWatchLogGroups(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 := cloudwatchlogs.New(session) | ||
|
||
lgName := createCloudWatchLogGroup(t, svc, region) | ||
defer deleteCloudWatchLogGroup(t, svc, lgName, true) | ||
|
||
lgNames, err := getAllCloudWatchLogGroups(session, region) | ||
require.NoError(t, err) | ||
assert.Contains(t, aws.StringValueSlice(lgNames), aws.StringValue(lgName)) | ||
} | ||
|
||
func TestNukeCloudWatchLogGroupOne(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 := cloudwatchlogs.New(session) | ||
|
||
// We ignore errors in the delete call here, because it is intended to be a stop gap in case there is a bug in nuke. | ||
lgName := createCloudWatchLogGroup(t, svc, region) | ||
defer deleteCloudWatchLogGroup(t, svc, lgName, false) | ||
identifiers := []*string{lgName} | ||
|
||
require.NoError( | ||
t, | ||
nukeAllCloudWatchLogGroups(session, identifiers), | ||
) | ||
|
||
// Make sure the CloudWatch Dashboard is deleted. | ||
assertCloudWatchLogGroupsDeleted(t, svc, identifiers) | ||
} | ||
|
||
func TestNukeCloudWatchLogGroupMoreThanOne(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 := cloudwatchlogs.New(session) | ||
|
||
lgNames := []*string{} | ||
for i := 0; i < 3; i++ { | ||
// We ignore errors in the delete call here, because it is intended to be a stop gap in case there is a bug in nuke. | ||
lgName := createCloudWatchLogGroup(t, svc, region) | ||
defer deleteCloudWatchLogGroup(t, svc, lgName, false) | ||
lgNames = append(lgNames, lgName) | ||
} | ||
|
||
require.NoError( | ||
t, | ||
nukeAllCloudWatchLogGroups(session, lgNames), | ||
) | ||
|
||
// Make sure the CloudWatch Dashboard is deleted. | ||
assertCloudWatchLogGroupsDeleted(t, svc, lgNames) | ||
} | ||
|
||
func createCloudWatchLogGroup(t *testing.T, svc *cloudwatchlogs.CloudWatchLogs, region string) *string { | ||
uniqueID := util.UniqueID() | ||
name := fmt.Sprintf("cloud-nuke-test-%s", strings.ToLower(uniqueID)) | ||
|
||
_, err := svc.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{ | ||
LogGroupName: aws.String(name), | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Add an arbitrary sleep to account for eventual consistency | ||
time.Sleep(15 * time.Second) | ||
return &name | ||
} | ||
|
||
// deleteCloudWatchLogGroup is a function to delete the given CloudWatch Log Group. | ||
func deleteCloudWatchLogGroup(t *testing.T, svc *cloudwatchlogs.CloudWatchLogs, name *string, checkErr bool) { | ||
_, err := svc.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{ | ||
LogGroupName: name, | ||
}) | ||
if checkErr { | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
func assertCloudWatchLogGroupsDeleted(t *testing.T, svc *cloudwatchlogs.CloudWatchLogs, identifiers []*string) { | ||
for _, name := range identifiers { | ||
resp, err := svc.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{ | ||
LogGroupNamePrefix: name, | ||
}) | ||
require.NoError(t, err) | ||
if len(resp.LogGroups) > 0 { | ||
t.Fatalf("Log Group %s is not deleted", aws.StringValue(name)) | ||
} | ||
} | ||
} |
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,36 @@ | ||
package aws | ||
|
||
import ( | ||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
) | ||
|
||
// CloudWatchLogGroup - represents all ec2 instances | ||
type CloudWatchLogGroups struct { | ||
Names []string | ||
} | ||
|
||
// ResourceName - the simple name of the aws resource | ||
func (r CloudWatchLogGroups) ResourceName() string { | ||
return "cloudwatch-loggroup" | ||
} | ||
|
||
// ResourceIdentifiers - The instance ids of the ec2 instances | ||
func (r CloudWatchLogGroups) ResourceIdentifiers() []string { | ||
return r.Names | ||
} | ||
|
||
func (r CloudWatchLogGroups) MaxBatchSize() int { | ||
// Tentative batch size to ensure AWS doesn't throttle | ||
return 200 | ||
} | ||
|
||
// Nuke - nuke 'em all!!! | ||
func (r CloudWatchLogGroups) Nuke(session *session.Session, identifiers []string) error { | ||
if err := nukeAllCloudWatchLogGroups(session, awsgo.StringSlice(identifiers)); err != nil { | ||
return errors.WithStackTrace(err) | ||
} | ||
|
||
return nil | ||
} |
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.
region
not used