Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (116 loc) · 3.98 KB
/
Copy pathindex.js
File metadata and controls
135 lines (116 loc) · 3.98 KB
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
126
127
128
129
130
131
132
133
134
135
const Dashboard = require('./Dashboard');
const ApiGateway = require('./ApiGateway');
const Widget = require('./Widget');
class DashboardPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.commands = {
dashboard: {
commands: {
create: {
lifecycleEvents: ['init'],
usage: 'This command takes the current structure of the application and creates/updates CloudWatch dashboard.'
},
remove: {
lifecycleEvents: ['init'],
usage: 'This command removes the dashboard from CloudWatch'
}
}
}
};
this.hooks = {
'after:deploy:finalize': () => this.initializeDashboardCreate(),
'remove:remove': () => this.initializeDashboardRemove(),
'dashboard:create:init': () => this.initializeDashboardCreate(),
'dashboard:remove:init': () => this.initializeDashboardRemove()
};
}
initializeDashboardCreate() {
const configuration = this.mergeConfiguration(this.serverless.service.custom.serverlessDashboard, this.options);
return this.createDashboard(
this.serverless.service.functions,
this.serverless.service.service,
this.serverless.service.provider.region,
this.serverless.service.provider.stage,
configuration
);
}
initializeDashboardRemove() {
return this.removeDashboard(
this.serverless.service.service,
this.serverless.service.provider.region,
this.serverless.service.provider.stage
);
}
mergeConfiguration(yamlConfiguration, cliConfiguration) {
const configuration = yamlConfiguration || {};
configuration.lambda = configuration.lambda || { enabled: true };
configuration.apiGateway = configuration.apiGateway || { enabled: true };
if (cliConfiguration.dashboardLambda === 'false') {
configuration.lambda.enabled = false;
} else if (cliConfiguration.dashboardLambda === 'true') {
configuration.lambda.enabled = true;
}
if (cliConfiguration.dashboardApiGateway === 'false') {
configuration.apiGateway.enabled = false;
} else if (cliConfiguration.dashboardApiGateway === 'true') {
configuration.apiGateway.enabled = true;
}
return configuration;
}
checkForAPI(functions) {
const values = Object.values(functions);
let foundAPI = false;
values.map(value => {
value.events && value.events.map(event => {
if (event.http != 'undefined') {
foundAPI = true;
}
});
});
return foundAPI;
}
async getWidgets(functions, apiName, region, configuration) {
const widget = new Widget(region);
const apiGateway = new ApiGateway(apiName, region);
let widgets = [];
if (configuration.lambda.enabled === true) {
const functionNames = this.getFunctionNames(functions);
for (const functionName of functionNames) {
widgets.push(widget.createLambdaWidget(functionName));
}
}
if (configuration.apiGateway.enabled === true) {
if (this.checkForAPI(functions)) {
if (await apiGateway.getApi()) {
widgets.push(widget.createApiGatewayWidget(apiName));
} else {
console.log('ApiGateway not found');
}
}
}
return widgets;
}
getFunctionNames(functions) {
let functionNames = [];
for (let f in functions) {
functionNames.push(functions[f].name);
}
return functionNames;
}
createDashboard(functions, serviceName, region, stage, configuration) {
const apiName = `${stage}-${serviceName}`;
return this.getWidgets(functions, apiName, region, configuration).then(
widgets => {
const dashboard = new Dashboard(`${serviceName}-${stage}`, widgets, region);
return dashboard.createDashboard();
}
);
}
removeDashboard(serviceName, region, stage) {
const dashboard = new Dashboard(`${serviceName}-${stage}`, null, region);
return dashboard.removeDashboard();
}
}
module.exports = DashboardPlugin;