-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
73 lines (68 loc) · 1.9 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
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
class ServerlessDeleteLoggroups {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
this.service = this.serverless.service.service;
this.region = this.provider.getRegion();
this.stage = this.provider.getStage();
this.commands = {
remove: {
commands: {
logs: {
usage: 'Delete all Loggroups',
lifecycleEvents: [
'remove',
],
options: {
stage: {
usage: 'Stage of the service',
shortcut: 's',
},
region: {
usage: 'Region of the service',
shortcut: 'r',
},
},
},
},
},
};
this.hooks = {
'remove:logs:remove': () => BbPromise.bind(this)
.then(this.removeLogGroups),
};
}
removeLogGroups() {
const logGroupNames = [];
_.forEach(this.serverless.service.functions, (value, key) => {
logGroupNames.push(`/aws/lambda/${value.name}`);
});
return BbPromise.map(logGroupNames, (value) => this.removeLogGroup(value))
.then(() => {
this.serverless.cli.log('Finished');
return BbPromise.resolve();
});
}
removeLogGroup(name) {
return this.provider.request('CloudWatchLogs',
'deleteLogGroup',
{
logGroupName: name,
},
this.options.stage,
this.options.region)
.then(() => {
this.serverless.cli.log(`Successfully removed logGroup ${name}`);
return BbPromise.resolve()
})
.catch((e) => {
this.serverless.cli.log(`Failed to remove logGroup ${name}: ${e.message}`);
return BbPromise.resolve()
});
}
}
module.exports = ServerlessDeleteLoggroups;