-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
81 lines (58 loc) · 2.11 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
'use strict';
const _ = require('lodash');
let _apiGatewayService = null;
let _cloudFormationService = null;
class ServerlessSQSTagPlugin {
get stackName() {
return `${this.serverless.service.service}-${this.options.stage}`;
}
get apiGatewayService() {
if (!_apiGatewayService)
_apiGatewayService = new this.awsService.sdk.APIGateway({ region: this.options.region });
return _apiGatewayService;
}
get cloudFormationService() {
if (!_cloudFormationService)
_cloudFormationService = new this.awsService.sdk.CloudFormation({ region: this.options.region });
return _cloudFormationService;
}
constructor(serverless, options) {
this.options = options;
this.serverless = serverless;
this.awsService = this.serverless.getProvider('aws');
this.hooks = {
'after:deploy:deploy': this.execute.bind(this),
};
}
execute() {
return this.getStackResources()
.then(data => this.tagAPIGateway(data))
.then(data => this.serverless.cli.log(JSON.stringify(data)))
.catch(err => this.serverless.cli.log(JSON.stringify(err)));
}
getStackResources() {
return new Promise((resolve, reject) => {
this.cloudFormationService.describeStackResources({ StackName: this.stackName }, (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
}
tagAPIGateway(data) {
const apiGatewayResources = _.filter(data.StackResources, { ResourceType: 'AWS::ApiGateway::RestApi' });
const promises = _.map(apiGatewayResources, item => {
return new Promise((resolve, reject) => {
const params = {
resourceArn: `arn:aws:apigateway:${this.options.region}::/restapis/${item.PhysicalResourceId}/stages/${this.options.stage}`,
tags: this.serverless.service.custom.apiGatewayTags
};
this.apiGatewayService.tagResource(params, (err, data) => {
if(err) return reject(err);
resolve(`Tagged API gateway ${item.LogicalResourceId}`);
});
});
});
return Promise.all(promises);
}
}
module.exports = ServerlessSQSTagPlugin;