forked from inokappa/serverless-tag-sns-topic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (58 loc) · 1.98 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
'use strict';
const _ = require('lodash');
let _snsService = null;
let _cloudFormationService = null;
class ServerlessSNSTopicTagPlugin {
get stackName() {
return `${this.serverless.service.service}-${this.options.stage}`;
}
get snsService() {
if(!_snsService)
_snsService = new this.awsService.sdk.SNS({ region: this.options.region });
return _snsService;
}
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.tagTopics(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);
});
});
}
tagTopics(data) {
const snsResources = _.filter(data.StackResources, { ResourceType: 'AWS::SNS::Topic' });
const promises = _.map(snsResources, item => {
return new Promise((resolve, reject) => {
const params = {
ResourceArn: item.PhysicalResourceId,
Tags: this.serverless.service.custom.snsTopicTags
};
this.snsService.tagResource(params, (err, apiData) => {
if(err) return reject(err);
resolve(`Tagged SNS Topic ${item.LogicalResourceId}`);
});
});
});
return Promise.all(promises);
}
}
module.exports = ServerlessSNSTopicTagPlugin;