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 #17 from rebuy-de/cloud-1047-nuke-cloudwatch
Browse files Browse the repository at this point in the history
CLOUD-1047: Delete CloudWatchEvents.
  • Loading branch information
Florian Zeidler authored Apr 18, 2017
2 parents 13e2ba6 + d71a2a3 commit c79334c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 12 deletions.
39 changes: 39 additions & 0 deletions resources/cloudwatchevents-rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/service/cloudwatchevents"
)

func (n *CloudWatchEventsNuke) ListRules() ([]Resource, error) {
resp, err := n.Service.ListRules(nil)
if err != nil {
return nil, err
}
resources := make([]Resource, 0)
for _, rule := range resp.Rules {
resources = append(resources, &CloudWatchEventsRule{
svc: n.Service,
name: rule.Name,
})

}
return resources, nil
}

type CloudWatchEventsRule struct {
svc *cloudwatchevents.CloudWatchEvents
name *string
}

func (rule *CloudWatchEventsRule) Remove() error {
_, err := rule.svc.DeleteRule(&cloudwatchevents.DeleteRuleInput{
Name: rule.name,
})
return err
}

func (rule *CloudWatchEventsRule) String() string {
return fmt.Sprintf("Rule: %s", *rule.name)
}
51 changes: 51 additions & 0 deletions resources/cloudwatchevents-targets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/service/cloudwatchevents"
)

func (n *CloudWatchEventsNuke) ListTargets() ([]Resource, error) {
resp, err := n.Service.ListRules(nil)
if err != nil {
return nil, err
}
resources := make([]Resource, 0)
for _, rule := range resp.Rules {
targetResp, err := n.Service.ListTargetsByRule(&cloudwatchevents.ListTargetsByRuleInput{
Rule: rule.Name,
})
if err != nil {
return nil, err
}

for _, target := range targetResp.Targets {
resources = append(resources, &CloudWatchEventsTarget{
svc: n.Service,
ruleName: rule.Name,
targetId: target.Id,
})
}
}
return resources, nil
}

type CloudWatchEventsTarget struct {
svc *cloudwatchevents.CloudWatchEvents
targetId *string
ruleName *string
}

func (target *CloudWatchEventsTarget) Remove() error {
ids := []*string{target.targetId}
_, err := target.svc.RemoveTargets(&cloudwatchevents.RemoveTargetsInput{
Ids: ids,
Rule: target.ruleName,
})
return err
}

func (target *CloudWatchEventsTarget) String() string {
return fmt.Sprintf("Rule: %s Target ID: %s", *target.ruleName, *target.targetId)
}
30 changes: 18 additions & 12 deletions resources/listers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/efs"
Expand All @@ -18,23 +19,28 @@ import (

func GetListers(sess *session.Session) []ResourceLister {
var (
autoscaling = AutoScalingNuke{autoscaling.New(sess)}
cloudformation = CloudFormationNuke{cloudformation.New(sess)}
ec2 = EC2Nuke{ec2.New(sess)}
ecr = ECRNuke{ecr.New(sess)}
efs = EFSNuke{efs.New(sess)}
elasticache = ElasticacheNuke{elasticache.New(sess)}
elb = ElbNuke{elb.New(sess)}
iam = IamNuke{iam.New(sess)}
rds = RDSNuke{rds.New(sess)}
route53 = Route53Nuke{route53.New(sess)}
s3 = S3Nuke{s3.New(sess)}
sns = SNSNuke{sns.New(sess)}
autoscaling = AutoScalingNuke{autoscaling.New(sess)}
cloudformation = CloudFormationNuke{cloudformation.New(sess)}

cloudwatchevents = CloudWatchEventsNuke{cloudwatchevents.New(sess)}

ec2 = EC2Nuke{ec2.New(sess)}
ecr = ECRNuke{ecr.New(sess)}
efs = EFSNuke{efs.New(sess)}
elasticache = ElasticacheNuke{elasticache.New(sess)}
elb = ElbNuke{elb.New(sess)}
iam = IamNuke{iam.New(sess)}
rds = RDSNuke{rds.New(sess)}
route53 = Route53Nuke{route53.New(sess)}
s3 = S3Nuke{s3.New(sess)}
sns = SNSNuke{sns.New(sess)}
)

return []ResourceLister{
autoscaling.ListGroups,
cloudformation.ListStacks,
cloudwatchevents.ListRules,
cloudwatchevents.ListTargets,
ec2.ListAddresses,
ec2.ListCustomerGateways,
ec2.ListDhcpOptions,
Expand Down
5 changes: 5 additions & 0 deletions resources/types.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/service/autoscaling"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/efs"
Expand Down Expand Up @@ -62,3 +63,7 @@ type S3Nuke struct {
type SNSNuke struct {
Service *sns.SNS
}

type CloudWatchEventsNuke struct {
Service *cloudwatchevents.CloudWatchEvents
}

0 comments on commit c79334c

Please sign in to comment.