This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathserverless.js
149 lines (120 loc) · 3.16 KB
/
serverless.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
const aws = require("aws-sdk");
const { isEmpty, mergeDeepRight, pick } = require("ramda");
const { Component } = require("@serverless/core");
const {
createQueue,
deleteQueue,
getDefaults,
getQueue,
getAccountId,
getArn,
getUrl,
setAttributes
} = require("./utils");
const outputsList = ["arn", "url"];
const defaults = {
name: "serverless",
region: "us-east-1"
};
class AwsSqsQueue extends Component {
async default(inputs = {}) {
const config = mergeDeepRight(getDefaults({ defaults }), inputs);
const accountId = await getAccountId(aws);
const arn = getArn({
aws,
accountId,
name: config.name,
region: config.region
});
const queueUrl = getUrl({
aws,
accountId,
name: config.name,
region: config.region
});
config.arn = arn;
config.url = queueUrl;
this.context.status(`Deploying`);
const sqs = new aws.SQS({
region: config.region,
credentials: this.context.credentials.aws
});
const prevInstance = await getQueue({
sqs,
queueUrl: this.state.url || queueUrl
});
if (isEmpty(prevInstance)) {
this.context.status(`Creating`);
await createQueue({
sqs,
config: config
});
} else {
if (this.state.url === queueUrl) {
this.context.status(`Updating`);
await setAttributes(sqs, queueUrl, config);
} else {
this.context.debug(`The QueueUrl has changed`);
this.context.debug(`Deleting previous queue`);
await deleteQueue({ sqs, queueUrl: this.state.url });
this.context.debug(`Creating new queue`);
await createQueue({
sqs,
config: config
});
}
}
this.state.name = config.name;
this.state.arn = config.arn;
this.state.url = config.url;
this.state.region = config.region;
await this.save();
const outputs = pick(outputsList, config);
return outputs;
}
async addEventSource(functionArn) {
const lambda = new aws.Lambda({
region: this.state.region,
credentials: this.context.credentials.aws
});
const existing = await lambda
.listEventSourceMappings({
EventSourceArn: this.state.arn,
FunctionName: functionArn
})
.promise();
const mappings = existing.EventSourceMappings || [];
if (mappings.length) {
return;
}
await lambda
.createEventSourceMapping({
EventSourceArn: this.state.arn,
FunctionName: functionArn
})
.promise();
}
async remove(inputs = {}) {
const config = mergeDeepRight(defaults, inputs);
config.name = inputs.name || this.state.name || defaults.name;
const sqs = new aws.SQS({
region: config.region,
credentials: this.context.credentials.aws
});
const accountId = await getAccountId(aws);
const queueUrl =
this.state.url ||
getUrl({
aws,
accountId,
name: config.name,
region: config.region
});
this.context.status(`Removing`);
await deleteQueue({ sqs, queueUrl });
this.state = {};
await this.save();
return {};
}
}
module.exports = AwsSqsQueue;