-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (50 loc) · 1.71 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
'use strict';
const _ = require('lodash');
class ServerlessApiGatewayXray {
get stackName() {
return `${this.serverless.service.service}-${this.options.stage}`;
}
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.enableApiGatewayXray(data))
.then(data => this.serverless.cli.log(JSON.stringify(data)))
.catch(err => this.serverless.cli.log(JSON.stringify(err)));
}
getStackResources() {
return this.awsService.request('CloudFormation', 'describeStackResources', { StackName: this.stackName });
}
enableApiGatewayXray(data) {
const apiGatewayResources = _.filter(data.StackResources, { ResourceType: 'AWS::ApiGateway::RestApi' });
const promises = _.map(apiGatewayResources, item => {
return new Promise((resolve, reject) => {
const params = {
restApiId: item.PhysicalResourceId,
stageName: this.options.stage,
patchOperations: [
{
op: 'replace',
path: '/tracingEnabled',
value: this.serverless.service.custom.apiGatewayXray.toString()
}
]
};
this.awsService.request('APIGateway', 'updateStage', params).then((data) => {
resolve(`API gateway xray ${this.serverless.service.custom.apiGatewayXray}`);
}).catch((err) => {
console.log(err);
reject(err);
});
});
});
return Promise.all(promises);
}
}
module.exports = ServerlessApiGatewayXray;