From 9eff958b24fc06e183ddf8277f60043beffad8da Mon Sep 17 00:00:00 2001 From: Nune Isabekyan Date: Wed, 1 Aug 2018 14:06:07 +0200 Subject: [PATCH 1/3] Initial version of tag-based filterring --- .gitignore | 2 ++ config/example.yaml | 4 ++++ resources/cloudformation-stack.go | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ccca5c9e5..700bc6375 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /aws-nuke /aws-nuke-* .glide +.idea +.envrc diff --git a/config/example.yaml b/config/example.yaml index 7a5c175cb..b1aeee44c 100644 --- a/config/example.yaml +++ b/config/example.yaml @@ -16,6 +16,7 @@ resource-types: - S3Object - Route53HostedZone - EC2Instance + - CloudFormationStack accounts: 555133742: @@ -37,3 +38,6 @@ accounts: - property: Name type: "glob" value: "*.zone.loc." + CloudFormationStack: + - property: "tag:team" + value: "myTeam" diff --git a/resources/cloudformation-stack.go b/resources/cloudformation-stack.go index 362a8a0b1..c1eadd1aa 100644 --- a/resources/cloudformation-stack.go +++ b/resources/cloudformation-stack.go @@ -3,7 +3,8 @@ package resources import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudformation" -) + "github.com/rebuy-de/aws-nuke/pkg/types" + ) func init() { register("CloudFormationStack", ListCloudFormationStacks) @@ -21,7 +22,7 @@ func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) { for _, stack := range resp.Stacks { resources = append(resources, &CloudFormationStack{ svc: svc, - name: stack.StackName, + stack: stack, }) } return resources, nil @@ -29,16 +30,26 @@ func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) { type CloudFormationStack struct { svc *cloudformation.CloudFormation - name *string + stack *cloudformation.Stack } func (cfs *CloudFormationStack) Remove() error { _, err := cfs.svc.DeleteStack(&cloudformation.DeleteStackInput{ - StackName: cfs.name, + StackName: cfs.stack.StackName, }) return err } +func (cfs *CloudFormationStack) Properties() types.Properties { + properties := types.NewProperties() + properties. + Set("Name", cfs.stack.StackName) + for _, tagValue := range cfs.stack.Tags { + properties.Set("tag:"+*tagValue.Key, tagValue.Value); + } + return properties; +} + func (cfs *CloudFormationStack) String() string { - return *cfs.name + return *cfs.stack.StackName } From 8af7fb0e1dceb990ef913973874b6cc7c97ecd65 Mon Sep 17 00:00:00 2001 From: Nune Isabekyan Date: Wed, 1 Aug 2018 14:40:49 +0200 Subject: [PATCH 2/3] Revert for .gitignore. SetTag in properties --- .gitignore | 2 -- pkg/types/properties.go | 16 ++++++++++++++++ resources/cloudformation-stack.go | 11 ++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 700bc6375..ccca5c9e5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,3 @@ /aws-nuke /aws-nuke-* .glide -.idea -.envrc diff --git a/pkg/types/properties.go b/pkg/types/properties.go index 748f9d536..ed3d669f4 100644 --- a/pkg/types/properties.go +++ b/pkg/types/properties.go @@ -57,6 +57,22 @@ func (p Properties) Set(key string, value interface{}) Properties { return p } +func (p Properties) SetTag(tagKey *string, tagValue interface{}) Properties { + if tagKey == nil { + return p + } + + keyStr := *tagKey + + sanitizedTagKey := strings.Trim(keyStr, " ") + if sanitizedTagKey == "" { + return p + } + + key := "tag:" + keyStr + return p.Set(key, tagValue) +} + func (p Properties) Get(key string) string { value, ok := p[key] if !ok { diff --git a/resources/cloudformation-stack.go b/resources/cloudformation-stack.go index c1eadd1aa..2b547821c 100644 --- a/resources/cloudformation-stack.go +++ b/resources/cloudformation-stack.go @@ -4,7 +4,7 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/rebuy-de/aws-nuke/pkg/types" - ) +) func init() { register("CloudFormationStack", ListCloudFormationStacks) @@ -21,7 +21,7 @@ func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) { resources := make([]Resource, 0) for _, stack := range resp.Stacks { resources = append(resources, &CloudFormationStack{ - svc: svc, + svc: svc, stack: stack, }) } @@ -29,7 +29,7 @@ func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) { } type CloudFormationStack struct { - svc *cloudformation.CloudFormation + svc *cloudformation.CloudFormation stack *cloudformation.Stack } @@ -45,9 +45,10 @@ func (cfs *CloudFormationStack) Properties() types.Properties { properties. Set("Name", cfs.stack.StackName) for _, tagValue := range cfs.stack.Tags { - properties.Set("tag:"+*tagValue.Key, tagValue.Value); + properties.SetTag(tagValue.Key, tagValue.Value) } - return properties; + + return properties } func (cfs *CloudFormationStack) String() string { From ae54a9fdcf8abe3cd34fa3d91fde4aefcc3295e3 Mon Sep 17 00:00:00 2001 From: Nune Isabekyan Date: Wed, 1 Aug 2018 15:23:20 +0200 Subject: [PATCH 3/3] Accidental newline removed --- resources/cloudformation-stack.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/cloudformation-stack.go b/resources/cloudformation-stack.go index 2b547821c..c4e779781 100644 --- a/resources/cloudformation-stack.go +++ b/resources/cloudformation-stack.go @@ -42,8 +42,7 @@ func (cfs *CloudFormationStack) Remove() error { func (cfs *CloudFormationStack) Properties() types.Properties { properties := types.NewProperties() - properties. - Set("Name", cfs.stack.StackName) + properties.Set("Name", cfs.stack.StackName) for _, tagValue := range cfs.stack.Tags { properties.SetTag(tagValue.Key, tagValue.Value) }