From 7678f0ba6315ee469a9e7c02fe1a7626588915dd Mon Sep 17 00:00:00 2001 From: Ken Keller Date: Mon, 1 Feb 2021 07:43:57 -0600 Subject: [PATCH] updte SNS with properties and pages (#592) --- resources/sns-topics.go | 49 +++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/resources/sns-topics.go b/resources/sns-topics.go index 360c89d80..9177e15d0 100644 --- a/resources/sns-topics.go +++ b/resources/sns-topics.go @@ -5,8 +5,15 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sns" + "github.com/rebuy-de/aws-nuke/pkg/types" ) +type SNSTopic struct { + svc *sns.SNS + id *string + tags []*sns.Tag +} + func init() { register("SNSTopic", ListSNSTopics) } @@ -14,25 +21,38 @@ func init() { func ListSNSTopics(sess *session.Session) ([]Resource, error) { svc := sns.New(sess) - resp, err := svc.ListTopics(nil) + topics := make([]*sns.Topic, 0) + + params := &sns.ListTopicsInput{} + + err := svc.ListTopicsPages(params, func(page *sns.ListTopicsOutput, lastPage bool) bool { + for _, out := range page.Topics { + topics = append(topics, out) + } + return true + }) if err != nil { return nil, err } resources := make([]Resource, 0) - for _, topic := range resp.Topics { + for _, topic := range topics { + tags, err := svc.ListTagsForResource(&sns.ListTagsForResourceInput{ + ResourceArn: topic.TopicArn, + }) + + if err != nil { + continue + } + resources = append(resources, &SNSTopic{ - svc: svc, - id: topic.TopicArn, + svc: svc, + id: topic.TopicArn, + tags: tags.Tags, }) } 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, @@ -40,6 +60,17 @@ func (topic *SNSTopic) Remove() error { return err } +func (topic *SNSTopic) Properties() types.Properties { + properties := types.NewProperties() + + for _, tag := range topic.tags { + properties.SetTag(tag.Key, tag.Value) + } + properties.Set("TopicARN", topic.id) + + return properties +} + func (topic *SNSTopic) String() string { return fmt.Sprintf("TopicARN: %s", *topic.id) }