2021-02-03 11:53:28 +00:00
|
|
|
package repository
|
2021-02-02 12:02:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sns"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sns/snsiface"
|
2021-06-04 14:33:58 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-02-02 12:02:53 +00:00
|
|
|
)
|
|
|
|
|
2021-02-03 11:53:28 +00:00
|
|
|
type SNSRepository interface {
|
2021-02-02 12:02:53 +00:00
|
|
|
ListAllTopics() ([]*sns.Topic, error)
|
2021-02-05 10:03:45 +00:00
|
|
|
ListAllSubscriptions() ([]*sns.Subscription, error)
|
2021-02-02 12:02:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:18:17 +00:00
|
|
|
type snsRepository struct {
|
2021-02-03 11:53:28 +00:00
|
|
|
client snsiface.SNSAPI
|
2021-06-04 14:33:58 +00:00
|
|
|
cache cache.Cache
|
2021-02-02 12:02:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:33:58 +00:00
|
|
|
func NewSNSClient(session *session.Session, c cache.Cache) *snsRepository {
|
2021-02-03 13:18:17 +00:00
|
|
|
return &snsRepository{
|
2021-02-02 12:02:53 +00:00
|
|
|
sns.New(session),
|
2021-06-04 14:33:58 +00:00
|
|
|
c,
|
2021-02-02 12:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:18:17 +00:00
|
|
|
func (r *snsRepository) ListAllTopics() ([]*sns.Topic, error) {
|
2021-06-04 14:33:58 +00:00
|
|
|
if v := r.cache.Get("snsListAllTopics"); v != nil {
|
|
|
|
return v.([]*sns.Topic), nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:02:53 +00:00
|
|
|
var topics []*sns.Topic
|
|
|
|
input := &sns.ListTopicsInput{}
|
2021-02-03 11:53:28 +00:00
|
|
|
err := r.client.ListTopicsPages(input, func(res *sns.ListTopicsOutput, lastPage bool) bool {
|
2021-02-02 12:02:53 +00:00
|
|
|
topics = append(topics, res.Topics...)
|
|
|
|
return !lastPage
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 14:33:58 +00:00
|
|
|
|
|
|
|
r.cache.Put("snsListAllTopics", topics)
|
2021-02-02 12:02:53 +00:00
|
|
|
return topics, nil
|
|
|
|
}
|
2021-02-05 10:03:45 +00:00
|
|
|
|
|
|
|
func (r *snsRepository) ListAllSubscriptions() ([]*sns.Subscription, error) {
|
2021-06-04 14:33:58 +00:00
|
|
|
if v := r.cache.Get("snsListAllSubscriptions"); v != nil {
|
|
|
|
return v.([]*sns.Subscription), nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:03:45 +00:00
|
|
|
var subscriptions []*sns.Subscription
|
|
|
|
input := &sns.ListSubscriptionsInput{}
|
|
|
|
err := r.client.ListSubscriptionsPages(input, func(res *sns.ListSubscriptionsOutput, lastPage bool) bool {
|
|
|
|
subscriptions = append(subscriptions, res.Subscriptions...)
|
|
|
|
return !lastPage
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 14:33:58 +00:00
|
|
|
|
|
|
|
r.cache.Put("snsListAllSubscriptions", subscriptions)
|
2021-02-05 10:03:45 +00:00
|
|
|
return subscriptions, nil
|
|
|
|
}
|