-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
180 lines (158 loc) · 4.86 KB
/
utils.js
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
const {
clone,
concat,
difference,
fromPairs,
head,
is,
isNil,
map,
merge,
mergeDeepRight,
pick,
reduce,
tail,
toPairs,
toUpper
} = require('ramda')
const titelize = (string) => `${toUpper(head(string))}${tail(string)}`
const getDefaults = ({ defaults, accountId, arn }) => {
const response = clone(defaults)
response.policy.Statement[0].Resource = arn
response.policy.Statement[0].Condition.StringEquals['AWS:SourceOwner'] = accountId
return response
}
const getTopic = async ({ sns, arn }) => {
let topicAttributes = {}
try {
const response = await sns.getTopicAttributes({ TopicArn: arn }).promise()
topicAttributes = response.Attributes
} catch (error) {
if (error.code !== 'NotFound') {
throw error
}
}
return topicAttributes
}
const getAccountId = async (aws) => {
const STS = new aws.STS()
const res = await STS.getCallerIdentity({}).promise()
return res.Account
}
const getArn = ({ name, region, accountId }) => {
return `arn:aws:sns:${region}:${accountId}:${name}`
}
const resolveInSequence = async (functionsToExecute) =>
reduce(
(promise, functionToExecute) =>
promise.then((result) => functionToExecute().then(Array.prototype.concat.bind(result))),
Promise.resolve([]),
functionsToExecute
)
const updateTopicAttributes = async (sns, { topicAttributes, arn }) =>
Promise.all(
map(([key, value]) => {
const params = {
TopicArn: arn,
AttributeName: key,
AttributeValue: !is(String, value) ? JSON.stringify(value) : value
}
return sns.setTopicAttributes(params).promise()
}, topicAttributes)
)
const updateDeliveryStatusAttributes = async (sns, { deliveryStatusAttributes, arn }) =>
// run update requests sequentially because setTopicAttributes
// fails to update when rate exceeds https://github.com/serverless/components/issues/174#issuecomment-390463523
resolveInSequence(
map(
([key, value]) => () => {
const params = {
TopicArn: arn,
AttributeName: titelize(key),
AttributeValue: !is(String, value) ? JSON.stringify(value) : value
}
return sns.setTopicAttributes(params).promise()
},
deliveryStatusAttributes
)
)
const updateAttributes = async (
sns,
{ displayName, policy, deliveryPolicy, deliveryStatusAttributes = [], arn },
prevInstance
) => {
const previousTopicAttributes = map(
([key, value]) => [key, /Policy/.test(key) ? JSON.parse(value) : value],
toPairs(pick(['DisplayName', 'Policy', 'DeliveryPolicy'], prevInstance))
)
const currentTopicAttributes = map(
([key, value]) => [titelize(key), value],
toPairs({ displayName, policy, deliveryPolicy })
)
const changedTopicAttributes = difference(currentTopicAttributes, previousTopicAttributes)
const mergedTopicAttributes = mergeDeepRight(
fromPairs(previousTopicAttributes),
fromPairs(currentTopicAttributes)
)
await updateTopicAttributes(sns, { topicAttributes: changedTopicAttributes, arn })
const currentDeliveryStatusAttributes = map(
([key, value]) => [key, value.toString()],
reduce((acc, attribute) => concat(acc, toPairs(attribute)), [], deliveryStatusAttributes)
)
const previousDeliveryStatusAttributes = toPairs(
pick(
[
'ApplicationSuccessFeedbackRoleArn',
'ApplicationSuccessFeedbackSampleRate',
'ApplicationFailureFeedbackRoleArn',
'HTTPSuccessFeedbackRoleArn',
'HTTPSuccessFeedbackSampleRate',
'HTTPFailureFeedbackRoleArn',
'LambdaSuccessFeedbackRoleArn',
'LambdaSuccessFeedbackSampleRate',
'LambdaFailureFeedbackRoleArn',
'SQSSuccessFeedbackRoleArn',
'SQSSuccessFeedbackSampleRate',
'SQSFailureFeedbackRoleArn'
],
prevInstance
)
)
const removableDeliveryStatusAttributes = map(([key, value]) => {
return [
key,
isNil(currentDeliveryStatusAttributes[key]) && !/SampleRate/.test(key) ? '' : value
]
}, difference(previousDeliveryStatusAttributes, currentDeliveryStatusAttributes))
const changedDeliveryStatusAttributes = concat(
difference(currentDeliveryStatusAttributes, previousDeliveryStatusAttributes),
removableDeliveryStatusAttributes
)
await updateDeliveryStatusAttributes(sns, {
deliveryStatusAttributes: changedDeliveryStatusAttributes,
arn
})
return merge(mergedTopicAttributes, currentDeliveryStatusAttributes)
}
const createTopic = async ({ sns, name }) => {
const { TopicArn: arn } = await sns.createTopic({ Name: name }).promise()
return { arn }
}
const deleteTopic = async ({ sns, arn }) => {
try {
await sns.deleteTopic({ TopicArn: arn }).promise()
} catch (error) {
if (error.code !== 'NotFound') {
throw error
}
}
}
module.exports = {
createTopic,
deleteTopic,
getAccountId,
getArn,
getDefaults,
getTopic,
updateAttributes
}