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/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 362a8a0b1..c4e779781 100644 --- a/resources/cloudformation-stack.go +++ b/resources/cloudformation-stack.go @@ -3,6 +3,7 @@ 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() { @@ -20,25 +21,35 @@ func ListCloudFormationStacks(sess *session.Session) ([]Resource, error) { resources := make([]Resource, 0) for _, stack := range resp.Stacks { resources = append(resources, &CloudFormationStack{ - svc: svc, - name: stack.StackName, + svc: svc, + stack: stack, }) } return resources, nil } type CloudFormationStack struct { - svc *cloudformation.CloudFormation - name *string + svc *cloudformation.CloudFormation + 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.SetTag(tagValue.Key, tagValue.Value) + } + + return properties +} + func (cfs *CloudFormationStack) String() string { - return *cfs.name + return *cfs.stack.StackName }