-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
108 lines (92 loc) · 3.44 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
const { clone } = require('ramda')
const getDefaults = ({ defaults }) => {
const response = clone(defaults)
return response
}
const getQueue = async ({ sqs, queueUrl }) => {
let queueAttributes = {}
try {
const response = await sqs.getQueueAttributes({ QueueUrl: queueUrl }).promise()
queueAttributes = response.Attributes
} catch (error) {
if (error.code !== 'AWS.SimpleQueueService.NonExistentQueue') {
throw error
}
}
return queueAttributes
}
const getAccountId = async (aws) => {
const STS = new aws.STS()
const res = await STS.getCallerIdentity({}).promise()
return res.Account
}
const getUrl = ({ name, region, accountId }) => {
return `https://sqs.${region}.amazonaws.com/${accountId}/${name}`
}
const getArn = ({ name, region, accountId }) => {
return `arn:aws:sqs:${region}:${accountId}:${name}`
}
const createAttributeMap = (config) => {
const attributeMap = {}
if (typeof config.visibilityTimeout !== "undefined") attributeMap.VisibilityTimeout = config.visibilityTimeout.toString()
if (typeof config.maximumMessageSize !== "undefined") attributeMap.MaximumMessageSize = config.maximumMessageSize.toString()
if (typeof config.messageRetentionPeriod !== "undefined") attributeMap.MessageRetentionPeriod = config.messageRetentionPeriod.toString()
if (typeof config.delaySeconds !== "undefined") attributeMap.DelaySeconds = config.delaySeconds.toString()
if (typeof config.receiveMessageWaitTimeSeconds !== "undefined") attributeMap.ReceiveMessageWaitTimeSeconds = config.receiveMessageWaitTimeSeconds.toString()
if (typeof config.redrivePolicy !== "undefined") attributeMap.RedrivePolicy = JSON.stringify(config.redrivePolicy) || ''
if (typeof config.policy !== "undefined") attributeMap.Policy = JSON.stringify(config.policy) || ''
if (typeof config.kmsMasterKeyId !== "undefined") attributeMap.KmsMasterKeyId = JSON.stringify(config.kmsMasterKeyId) || ''
if (typeof config.kmsDataKeyReusePeriodSeconds !== "undefined") attributeMap.KmsDataKeyReusePeriodSeconds = JSON.stringify(config.kmsDataKeyReusePeriodSeconds) || '300'
if (config.fifoQueue) {
if (typeof config.kmsDataKeyReusePeriodSeconds !== "undefined") {
attributeMap.ContentBasedDeduplication = JSON.stringify(config.contentBasedDeduplication) || 'false'
}
}
return attributeMap
}
const createQueue = async ({ sqs, config }) => {
const params = { QueueName: config.name, Attributes: createAttributeMap(config) }
if (config.fifoQueue) {
params.Attributes.FifoQueue = 'true'
}
if (config.tags) {
params.tags = config.tags
}
const { QueueArn: arn } = await sqs.createQueue(params).promise()
return { arn }
}
const getAttributes = async (sqs, queueUrl) => {
const params = {
QueueUrl: queueUrl,
AttributeNames: ['All']
}
const { Attributes: queueAttributes } = await sqs.getQueueAttributes(params).promise()
return queueAttributes
}
const setAttributes = async (sqs, queueUrl, config) => {
const params = {
QueueUrl: queueUrl,
Attributes: createAttributeMap(config)
}
await sqs.setQueueAttributes(params).promise()
}
const deleteQueue = async ({ sqs, queueUrl }) => {
try {
await sqs.deleteQueue({ QueueUrl: queueUrl }).promise()
} catch (error) {
if (error.code !== 'AWS.SimpleQueueService.NonExistentQueue') {
throw error
}
}
}
module.exports = {
createQueue,
deleteQueue,
getAccountId,
getArn,
getUrl,
getDefaults,
getQueue,
getAttributes,
setAttributes
}