forked from utilitywarehouse/go-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snssink.go
81 lines (67 loc) · 1.86 KB
/
snssink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package sns
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"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"
"github.com/denislobanov/go-pubsub"
"github.com/pkg/errors"
)
type messageSink struct {
client snsiface.SNSAPI
topic string
}
// NewSNSSink is a constructor for new AWS SNS MessageSink type
func NewSNSSink(conf *aws.Config, topic string) (pubsub.MessageSink, error) {
sess, err := session.NewSession(conf)
if err != nil {
return nil, errors.Wrap(err, "could not construct AWS Session")
}
return &messageSink{
client: sns.New(sess),
topic: topic,
}, nil
}
// PutMessage sends ProducerMessage types to AWS SNS
func (s *messageSink) PutMessage(message pubsub.ProducerMessage) error {
b, err := message.Marshal()
if err != nil {
return errors.Wrap(err, "SNS Sink could not marshal ProducerMessage")
}
input := &sns.PublishInput{
Message: aws.String(string(b)),
TopicArn: &s.topic,
}
_, err = s.client.Publish(input)
if err != nil {
return errors.Wrap(err, "error publishing to SNS")
}
return nil
}
// Status used to check status of connection to AWS SNS
func (s *messageSink) Status() (*pubsub.Status, error) {
topics, err := s.client.ListTopics(&sns.ListTopicsInput{})
if err != nil {
return nil, err
}
status := &pubsub.Status{}
if len(topics.Topics) < 1 {
status.Working = false
status.Problems = append(status.Problems, "no SNS topics")
return status, nil
}
for _, x := range topics.Topics {
if *x.TopicArn == s.topic {
status.Working = true
return status, nil
}
}
status.Working = false
status.Problems = append(status.Problems, fmt.Sprintf("%s not in topic list [%v]", s.topic, topics.Topics))
return status, nil
}
// Close is stubbed as there is no equivalent on the SNS Client.
func (s *messageSink) Close() error {
return nil
}