Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #237 from nisabek/feature-218
Browse files Browse the repository at this point in the history
Initial version of tag-based filterring
  • Loading branch information
svenwltr authored Aug 2, 2018
2 parents d621ebd + ae54a9f commit 057edc5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ resource-types:
- S3Object
- Route53HostedZone
- EC2Instance
- CloudFormationStack

accounts:
555133742:
Expand All @@ -37,3 +38,6 @@ accounts:
- property: Name
type: "glob"
value: "*.zone.loc."
CloudFormationStack:
- property: "tag:team"
value: "myTeam"
16 changes: 16 additions & 0 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 17 additions & 6 deletions resources/cloudformation-stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}

0 comments on commit 057edc5

Please sign in to comment.