-
Notifications
You must be signed in to change notification settings - Fork 50
/
input_sns.go
117 lines (94 loc) · 3.02 KB
/
input_sns.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package stream
import (
"context"
"fmt"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/cloud/aws/sns"
"github.com/justtrackio/gosoline/pkg/cloud/aws/sqs"
"github.com/justtrackio/gosoline/pkg/dx"
"github.com/justtrackio/gosoline/pkg/log"
)
var _ AcknowledgeableInput = &snsInput{}
type SnsInputSettings struct {
cfg.AppId
QueueId string `cfg:"queue_id"`
MaxNumberOfMessages int32 `cfg:"max_number_of_messages" default:"10" validate:"min=1,max=10"`
WaitTime int32 `cfg:"wait_time"`
RedrivePolicy sqs.RedrivePolicy `cfg:"redrive_policy"`
VisibilityTimeout int `cfg:"visibility_timeout"`
RunnerCount int `cfg:"runner_count"`
ClientName string `cfg:"client_name"`
}
func (s SnsInputSettings) GetAppId() cfg.AppId {
return s.AppId
}
func (s SnsInputSettings) GetClientName() string {
return s.ClientName
}
func (s SnsInputSettings) GetQueueId() string {
return s.QueueId
}
func (s SnsInputSettings) IsFifoEnabled() bool {
return false
}
type SnsInputTarget struct {
cfg.AppId
TopicId string
Attributes map[string]string
ClientName string
}
func (t SnsInputTarget) GetAppId() cfg.AppId {
return t.AppId
}
func (t SnsInputTarget) GetClientName() string {
return t.ClientName
}
func (t SnsInputTarget) GetTopicId() string {
return t.TopicId
}
type snsInput struct {
*sqsInput
}
func NewSnsInput(ctx context.Context, config cfg.Config, logger log.Logger, settings *SnsInputSettings, targets []SnsInputTarget) (*snsInput, error) {
autoSubscribe := dx.ShouldAutoCreate(config)
sqsInput, err := NewSqsInput(ctx, config, logger, &SqsInputSettings{
AppId: settings.AppId,
QueueId: settings.QueueId,
MaxNumberOfMessages: settings.MaxNumberOfMessages,
WaitTime: settings.WaitTime,
VisibilityTimeout: settings.VisibilityTimeout,
RunnerCount: settings.RunnerCount,
RedrivePolicy: settings.RedrivePolicy,
ClientName: settings.ClientName,
Unmarshaller: UnmarshallerSns,
})
if err != nil {
return nil, fmt.Errorf("can not create sqsInput: %w", err)
}
queueArn := sqsInput.GetQueueArn()
if autoSubscribe {
var topic sns.Topic
var topicName string
for _, target := range targets {
if topicName, err = sns.GetTopicName(config, target); err != nil {
return nil, fmt.Errorf("can not get sns topic name for target %s: %w", target.TopicId, err)
}
topicSettings := &sns.TopicSettings{
TopicName: topicName,
ClientName: "default",
}
if topic, err = sns.NewTopic(ctx, config, logger, topicSettings); err != nil {
return nil, fmt.Errorf("can not create topic: %w", err)
}
if err = topic.SubscribeSqs(ctx, queueArn, target.Attributes); err != nil {
return nil, fmt.Errorf("can not subscribe to queue: %w", err)
}
}
}
return NewSnsInputWithInterfaces(sqsInput), nil
}
func NewSnsInputWithInterfaces(sqsInput *sqsInput) *snsInput {
return &snsInput{
sqsInput,
}
}