-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
125 lines (121 loc) · 4.61 KB
/
index.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
'use strict';
var AWS = require('aws-sdk');
var fs = require('fs');
class ServerlessApiGWSqsPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.apiEndpointUrl = "";
this.commands = {
deploy: {
lifecycleEvents: [
'resources'
]
},
};
this.hooks = {
'before:deploy:deploy': this.beforeDeployResources.bind(this),
'before:remove:remove': this.deleteStack.bind(this),
'after:aws:info:displayEndpoints': this.setApiEndpoint.bind(this)
};
}
setCloudFormation() {
AWS.config.update({region:this.serverless.service.provider.region});
this.cloudformation = new AWS.CloudFormation();
}
setApiEndpoint() {
this.setCloudFormation()
var stackName = this.getStackName(this.options.stage, this.serverless.service.service)
var queueName = this.serverless.service.custom.apiGwSqs.queueName
if (!queueName.includes(".")) {
return new Promise((resolve, reject) => {
this.cloudformation.describeStacks({StackName: stackName}, function(err, data) {
if(!err) {
console.log(" POST - "+data["Stacks"][0]["Outputs"][0]["OutputValue"]);
resolve();
}
});
});
}
}
deleteStack() {
this.setCloudFormation()
var stackName = this.getStackName(this.options.stage, this.serverless.service.service)
this.cloudformation.deleteStack({StackName: stackName}, function (err, data) {
if (err) {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] Error: ", err.message);
}
}.bind(this));
}
getStackName(stage, serviceName) {
return stage + "-" + serviceName + "-APIGW-SQS";
}
beforeDeployResources() {
this.setCloudFormation()
return new Promise((resolve, reject) => {
fs.readFile(__dirname + '/' + 'template.yml', 'utf8', (err, contents) => {
var stackName = this.getStackName(this.options.stage, this.serverless.service.service)
var apiEndpoint = this.serverless.service.custom.apiGwSqs.apiEndpoint
var queueName = this.serverless.service.custom.apiGwSqs.queueName
var replaceStageName = new RegExp('STAGE_NAME', 'g');
var replaceApiEndpoint = new RegExp('API_ENDPOINT', 'g');
var replaceQueueName = new RegExp('QUEUE_NAME', 'g');
contents = contents.replace(replaceStageName, this.options.stage);
contents = contents.replace(replaceApiEndpoint, apiEndpoint);
contents = contents.replace(replaceQueueName, queueName);
var params = {
Capabilities: [
'CAPABILITY_IAM'
],
StackName: stackName,
TemplateBody: contents,
};
if (queueName.includes(".")) {
console.log("[CodeRecipe ApiGW SQS Plugin] QueueName Error: Can only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length")
reject()
}
this.cloudformation.createStack(params, (err, data) => {
if (err) {
if(err.code == 'AlreadyExistsException') {
// update stack instead
this.cloudformation.updateStack(params, (err, data) => {
if (err) {
if (err.message == "No updates are to be performed.") {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] No changes to ApiGW -> SQS")
resolve();
}
} else {
this.cloudformation.waitFor('stackCreateComplete', {StackName: stackName}, (err, data) => {
if (err) {
console.log(err.message);
}
else {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] Updated ApiGW -> SQS")
}
resolve();
});
}
});
} else {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] Error: ", err.message);
resolve();
}
}
else {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] Creating Api GW -> SQS")
this.cloudformation.waitFor('stackCreateComplete', {StackName: stackName}, (err, data) => {
if (err) {
console.log(err.message);
}
else {
this.serverless.cli.log("[CodeRecipe ApiGW SQS Plugin] Created Api GW -> SQS")
}
resolve();
});
}
});
});
})
}
}
module.exports = ServerlessApiGWSqsPlugin;