Skip to content

Commit

Permalink
updte SNS with properties and pages (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkenkeller committed Feb 1, 2021
1 parent 34b7e0c commit 7678f0b
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions resources/sns-topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,72 @@ 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)
}

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,
})
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)
}

0 comments on commit 7678f0b

Please sign in to comment.