forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sns_topic.go
154 lines (122 loc) · 3.94 KB
/
sns_topic.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sns"
)
func prepareSNSTopic(key, secret, region, topic string) (string, error) {
config := getConfig(key, secret, region)
topicARN, err := findExistingTopicARN(config, topic)
if err != nil {
topicARN, err = createTopic(config, topic)
}
if err != nil {
return "", err
}
return topicARN, nil
}
func prepareSubscription(key, secret, region, topicARN, queueARN string) error {
config := getConfig(key, secret, region)
subARN, err := findSubscription(config, topicARN, queueARN)
if err != nil {
return err
}
if subARN == "" {
subARN, err = createSubscription(config, topicARN, queueARN)
}
if subARN == "" {
return fmt.Errorf("failed to create subscription")
}
return err
}
func createSubscription(config *aws.Config, topicARN, queueARN string) (string, error) {
svc := sns.New(config)
params := &sns.SubscribeInput{
Protocol: aws.String("sqs"),
TopicArn: aws.String(topicARN),
Endpoint: aws.String(queueARN),
}
resp, err := svc.Subscribe(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while creating subscription to SNS topic: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while creating subscription to SNS topic: %v", err)
} else if resp == nil || resp.SubscriptionArn == nil {
return "", fmt.Errorf("error while creating subscription to SNS topic")
}
return *resp.SubscriptionArn, nil
}
func findSubscription(config *aws.Config, topicARN, queueARN string) (string, error) {
svc := sns.New(config)
params := &sns.ListSubscriptionsByTopicInput{
TopicArn: aws.String(topicARN),
}
for {
resp, err := svc.ListSubscriptionsByTopic(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while listing subscriptions to SNS topic: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while listing subscriptions to SNS topic: %v", err)
} else if resp == nil || resp.Subscriptions == nil {
break
}
for _, sub := range resp.Subscriptions {
if sub.Endpoint != nil && *sub.Endpoint == queueARN && sub.Protocol != nil && *sub.Protocol == "sqs" && sub.SubscriptionArn != nil {
return *sub.SubscriptionArn, nil
}
}
if resp.NextToken != nil {
params.NextToken = resp.NextToken
} else {
break
}
}
return "", nil
}
func createTopic(config *aws.Config, topic string) (string, error) {
svc := sns.New(config)
params := &sns.CreateTopicInput{
Name: aws.String(topic),
}
resp, err := svc.CreateTopic(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while creating SNS topic: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while creating SNS topic: %v", err)
} else if resp == nil || resp.TopicArn == nil {
return "", nil
}
return *resp.TopicArn, nil
}
func findExistingTopicARN(config *aws.Config, topic string) (string, error) {
svc := sns.New(nil)
params := &sns.ListTopicsInput{
NextToken: nil,
}
for {
resp, err := svc.ListTopics(params)
if awserr, ok := err.(awserr.Error); ok {
return "", fmt.Errorf("aws error while listing SNS topics: %v %v", awserr.Code(), awserr.Message())
} else if err != nil {
return "", fmt.Errorf("error while listing SNS topics: %v", err)
} else if resp == nil || resp.Topics == nil {
break
}
for _, topicPtr := range resp.Topics {
if topicPtr != nil && topicPtr.TopicArn != nil && strings.HasSuffix(*topicPtr.TopicArn, topic) {
return *topicPtr.TopicArn, nil
}
}
if resp.NextToken != nil {
params.NextToken = resp.NextToken
} else {
break
}
}
return "", nil
}