From d71a2a3c0a93a3444b454eb55d67cb78c8881aa1 Mon Sep 17 00:00:00 2001 From: Florian Zeidler Date: Tue, 18 Apr 2017 10:10:50 +0200 Subject: [PATCH] CLOUD-1047: Delete CloudWatchEvents. --- resources/cloudwatchevents-rules.go | 39 ++++++++++++++++++++ resources/cloudwatchevents-targets.go | 51 +++++++++++++++++++++++++++ resources/listers.go | 30 +++++++++------- resources/types.go | 5 +++ 4 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 resources/cloudwatchevents-rules.go create mode 100644 resources/cloudwatchevents-targets.go diff --git a/resources/cloudwatchevents-rules.go b/resources/cloudwatchevents-rules.go new file mode 100644 index 000000000..f418bc0af --- /dev/null +++ b/resources/cloudwatchevents-rules.go @@ -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) +} diff --git a/resources/cloudwatchevents-targets.go b/resources/cloudwatchevents-targets.go new file mode 100644 index 000000000..c08d647c0 --- /dev/null +++ b/resources/cloudwatchevents-targets.go @@ -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) +} diff --git a/resources/listers.go b/resources/listers.go index ee0e2bfaf..f141bcb1d 100644 --- a/resources/listers.go +++ b/resources/listers.go @@ -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" @@ -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, diff --git a/resources/types.go b/resources/types.go index d8f3953ca..4544ee2d4 100644 --- a/resources/types.go +++ b/resources/types.go @@ -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" @@ -62,3 +63,7 @@ type S3Nuke struct { type SNSNuke struct { Service *sns.SNS } + +type CloudWatchEventsNuke struct { + Service *cloudwatchevents.CloudWatchEvents +}