From 21950a6b9139ac1e118e57387ab6cd9241384ffa Mon Sep 17 00:00:00 2001 From: Florian Zeidler Date: Thu, 13 Apr 2017 16:47:13 +0200 Subject: [PATCH] CLOUD-1043: Remove Topics and acknowledged subscriptions --- resources/listers.go | 4 ++++ resources/sns-subscriptions.go | 43 ++++++++++++++++++++++++++++++++++ resources/sns-topics.go | 38 ++++++++++++++++++++++++++++++ resources/types.go | 5 ++++ 4 files changed, 90 insertions(+) create mode 100644 resources/sns-subscriptions.go create mode 100644 resources/sns-topics.go diff --git a/resources/listers.go b/resources/listers.go index 4015510e5..ee0e2bfaf 100644 --- a/resources/listers.go +++ b/resources/listers.go @@ -13,6 +13,7 @@ import ( "github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/route53" "github.com/aws/aws-sdk-go/service/s3" + "github.com/aws/aws-sdk-go/service/sns" ) func GetListers(sess *session.Session) []ResourceLister { @@ -28,6 +29,7 @@ func GetListers(sess *session.Session) []ResourceLister { rds = RDSNuke{rds.New(sess)} route53 = Route53Nuke{route53.New(sess)} s3 = S3Nuke{s3.New(sess)} + sns = SNSNuke{sns.New(sess)} ) return []ResourceLister{ @@ -77,5 +79,7 @@ func GetListers(sess *session.Session) []ResourceLister { route53.ListResourceRecords, s3.ListBuckets, s3.ListObjects, + sns.ListSubscriptions, + sns.ListTopics, } } diff --git a/resources/sns-subscriptions.go b/resources/sns-subscriptions.go new file mode 100644 index 000000000..eb06b748d --- /dev/null +++ b/resources/sns-subscriptions.go @@ -0,0 +1,43 @@ +package resources + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/service/sns" +) + +func (n *SNSNuke) ListSubscriptions() ([]Resource, error) { + resp, err := n.Service.ListSubscriptions(nil) + if err != nil { + return nil, err + } + resources := make([]Resource, 0) + for _, subscription := range resp.Subscriptions { + if *subscription.SubscriptionArn != "PendingConfirmation" { + resources = append(resources, &SNSSubscription{ + svc: n.Service, + id: subscription.SubscriptionArn, + name: subscription.Owner, + }) + } + + } + return resources, nil +} + +type SNSSubscription struct { + svc *sns.SNS + id *string + name *string +} + +func (subs *SNSSubscription) Remove() error { + _, err := subs.svc.Unsubscribe(&sns.UnsubscribeInput{ + SubscriptionArn: subs.id, + }) + return err +} + +func (subs *SNSSubscription) String() string { + return fmt.Sprintf("Owner: %s ARN: %s", *subs.name, *subs.id) +} diff --git a/resources/sns-topics.go b/resources/sns-topics.go new file mode 100644 index 000000000..0bb1d2b9f --- /dev/null +++ b/resources/sns-topics.go @@ -0,0 +1,38 @@ +package resources + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/service/sns" +) + +func (n *SNSNuke) ListTopics() ([]Resource, error) { + resp, err := n.Service.ListTopics(nil) + if err != nil { + return nil, err + } + resources := make([]Resource, 0) + for _, topic := range resp.Topics { + resources = append(resources, &SNSTopic{ + svc: n.Service, + id: topic.TopicArn, + }) + } + return resources, nil +} + +type SNSTopic struct { + svc *sns.SNS + id *string +} + +func (topic *SNSTopic) Remove() error { + _, err := topic.svc.DeleteTopic(&sns.DeleteTopicInput{ + TopicArn: topic.id, + }) + return err +} + +func (topic *SNSTopic) String() string { + return fmt.Sprintf("TopicARN: %s", *topic.id) +} diff --git a/resources/types.go b/resources/types.go index c75c31b5c..d8f3953ca 100644 --- a/resources/types.go +++ b/resources/types.go @@ -12,6 +12,7 @@ import ( "github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/route53" "github.com/aws/aws-sdk-go/service/s3" + "github.com/aws/aws-sdk-go/service/sns" ) type AutoScalingNuke struct { @@ -57,3 +58,7 @@ type Route53Nuke struct { type S3Nuke struct { Service *s3.S3 } + +type SNSNuke struct { + Service *sns.SNS +}