-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsns.go
More file actions
231 lines (194 loc) · 6.69 KB
/
sns.go
File metadata and controls
231 lines (194 loc) · 6.69 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package window
import (
"encoding/json"
"net/url"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
)
type (
SNSTopic struct {
DeliveryPolicy string
DisplayName string
EffectiveDeliveryPolicyJSON string
Owner string
PolicyJSON string
SubscriptionsConfirmed int64
SubscriptionsDeleted int64
SubscriptionsPending int64
TopicArn string
Name string
Id string
State string
TopicName string // tail of arn
Region *Region
Policy *SNSPolicy
EffectiveDeliveryPolicies SNSDeliveryPolicies
Subscribers []*SNSSubscription
Stats *TopicStats
CloudWatchAlarms []*CloudWatchAlarm
}
SNSSubscription struct {
// The subscription's endpoint (format depends on the protocol).
Endpoint string
// The subscription's owner.
Owner string
// The subscription's protocol.
Protocol string
// The subscription's ARN.
SubscriptionArn string
// The ARN of the subscription's topic.
TopicArn string
Name string
Region *Region
}
SNSPolicy struct {
ID string `json:"Id"`
Statements []*SNSPolicyStatement `json:"Statement"`
Version string `json:"Version"`
}
SNSPolicyStatement struct {
Action string `json:"Action"`
ConditionJSON json.RawMessage `json:"Condition"`
Condition string `json:"-"`
Effect string `json:"Effect"`
Principal struct {
AWS string `json:"AWS"`
} `json:"Principal"`
Resource string `json:"Resource"`
Sid string `json:"Sid"`
}
SNSDeliveryPolicies map[string]*SNSDeliveryPolicy
SNSDeliveryPolicy struct {
DefaultHealthyRetryPolicy struct {
BackoffFunction string `json:"backoffFunction"`
MaxDelayTarget int `json:"maxDelayTarget"`
MinDelayTarget int `json:"minDelayTarget"`
NumMaxDelayRetries int `json:"numMaxDelayRetries"`
NumMinDelayRetries int `json:"numMinDelayRetries"`
NumNoDelayRetries int `json:"numNoDelayRetries"`
NumRetries int `json:"numRetries"`
} `json:"defaultHealthyRetryPolicy"`
DisableSubscriptionOverrides bool `json:"disableSubscriptionOverrides"`
}
SNSTopicByNameAsc []*SNSTopic
SNSSubscriptionByNameAsc []*SNSSubscription
)
func (a SNSTopicByNameAsc) Len() int { return len(a) }
func (a SNSTopicByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SNSTopicByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func (a SNSSubscriptionByNameAsc) Len() int { return len(a) }
func (a SNSSubscriptionByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SNSSubscriptionByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func LoadSNSTopics(input *sns.ListTopicsInput) (map[string]*SNSTopic, error) {
snsts := map[string]*SNSTopic{}
if err := SNSClient.ListTopicsPages(input, func(p *sns.ListTopicsOutput, _ bool) bool {
for _, topic := range p.Topics {
snsts[*topic.TopicArn] = &SNSTopic{TopicArn: *topic.TopicArn}
}
return true
}); err != nil {
return nil, err
}
errs := make(chan error, len(snsts))
for arn, snst := range snsts {
go func(arn string, snst *SNSTopic) {
resp, err := SNSClient.GetTopicAttributes(&sns.GetTopicAttributesInput{TopicArn: aws.String(arn)})
if err == nil {
snst.DeliveryPolicy = aws.StringValue(resp.Attributes["DeliveryPolicy"])
snst.DisplayName = aws.StringValue(resp.Attributes["DisplayName"])
snst.EffectiveDeliveryPolicyJSON = aws.StringValue(resp.Attributes["EffectiveDeliveryPolicy"])
snst.Owner = aws.StringValue(resp.Attributes["Owner"])
snst.PolicyJSON = aws.StringValue(resp.Attributes["Policy"])
snst.TopicArn = aws.StringValue(resp.Attributes["TopicArn"])
snst.SubscriptionsConfirmed, _ = strconv.ParseInt(aws.StringValue(resp.Attributes["SubscriptionsConfirmed"]), 10, 64)
snst.SubscriptionsDeleted, _ = strconv.ParseInt(aws.StringValue(resp.Attributes["SubscriptionsDeleted"]), 10, 64)
snst.SubscriptionsPending, _ = strconv.ParseInt(aws.StringValue(resp.Attributes["SubscriptionsPending"]), 10, 64)
snst.TopicName = snst.TopicArn[strings.LastIndex(snst.TopicArn, ":")+1:]
snst.Id = "sns:" + snst.TopicArn
if len(snst.DisplayName) > 0 {
snst.Name = snst.DisplayName
} else {
snst.Name = snst.TopicName
}
snst.Policy = &SNSPolicy{}
json.NewDecoder(strings.NewReader(snst.PolicyJSON)).Decode(snst.Policy)
for _, statement := range snst.Policy.Statements {
statement.Condition = string(statement.ConditionJSON)
}
snst.EffectiveDeliveryPolicies = SNSDeliveryPolicies{}
json.NewDecoder(strings.NewReader(snst.EffectiveDeliveryPolicyJSON)).Decode(&snst.EffectiveDeliveryPolicies)
}
errs <- err
}(arn, snst)
}
for i := 0; i < cap(errs); i++ {
if err := <-errs; err != nil {
return nil, err
}
}
return snsts, nil
}
func LoadSNSSubscriptions(input *sns.ListSubscriptionsInput) (map[string]*SNSSubscription, error) {
subs := map[string]*SNSSubscription{}
if err := SNSClient.ListSubscriptionsPages(input, func(p *sns.ListSubscriptionsOutput, _ bool) bool {
for _, sub := range p.Subscriptions {
ss := &SNSSubscription{
Endpoint: aws.StringValue(sub.Endpoint),
Owner: aws.StringValue(sub.Owner),
Protocol: aws.StringValue(sub.Protocol),
SubscriptionArn: aws.StringValue(sub.SubscriptionArn),
TopicArn: aws.StringValue(sub.TopicArn),
}
ss.Name = ss.Endpoint
subs[ss.SubscriptionArn] = ss
}
return true
}); err != nil {
return nil, err
}
return subs, nil
}
func (t *SNSTopic) Poll() []chan error {
var errs []chan error
t.Stats = &TopicStats{}
for _, m := range SNSTopicMetrics {
m := m
errs = append(errs, t.Region.Throttle.do(t.Name+":"+*m.name+" SNSTopic METRICS POLL", func() error {
return m.RunFor(t)
}))
}
return errs
}
func (s *SNSSubscription) TargetName() string {
switch strings.ToLower(s.Protocol) {
case "http", "https":
u, err := url.Parse(s.Endpoint)
if err == nil {
return u.Host
}
return s.Endpoint
case "email", "email-json", "sms":
return s.Endpoint
case "sqs":
for _, q := range s.Region.SQSQueues {
if q.QueueArn == s.Endpoint {
return q.Name
}
}
if i := strings.LastIndexByte(s.Endpoint, ':'); i > -1 {
return "ext://" + s.Endpoint[i+1:]
}
return "ext://" + s.Endpoint
}
return s.Endpoint
}
func (t *SNSTopic) Inactive() bool {
return len(t.Subscribers) == 0 ||
(t.Stats != nil && t.Stats.PublishedPerSecond == 0)
}