-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (99 loc) · 3.73 KB
/
index.js
File metadata and controls
118 lines (99 loc) · 3.73 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
var Lambda = require('./resources/lambda');
var Ec2 = require('./resources/ec2');
var DynamoDb = require('./resources/dynamodb');
var S3 = require('./resources/s3');
class ServerlessCloudwatchDashboardPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.serverless.getProvider('aws');
this.resources = {
lambda: new Lambda(this,this.getConfig("lambda")||{}),
ec2: new Ec2(this,this.getConfig("ec2")||{}),
dynamodb: new DynamoDb(this,this.getConfig("dynamodb")||{}),
s3: new S3(this,this.getConfig("s3")||{})
}
this.resourcemapping = {
"AWS::Lambda::Function": this.resources.lambda,
"AWS::EC2::Instance": this.resources.ec2,
"AWS::DynamoDB::Table": this.resources.dynamodb,
"AWS::S3::Bucket": this.resources.s3
}
this.hooks = {
'before:deploy:deploy': this.injectDashboard.bind(this),
};
}
log(logitem) {
this.serverless.cli.consoleLog("serverless-cloudwatch-dashboard: "+logitem);
}
getServiceName() {
return this.serverless.service.getServiceName()+"-"+this.serverless.getProvider('aws').getStage();
}
getConfig(configItem) {
if (this.serverless.service.custom && this.serverless.service.custom["serverless-cloudwatch-dashboard"])
return this.serverless.service.custom["serverless-cloudwatch-dashboard"][configItem];
else
return {};
}
createWidgets(resources) {
var widgets = [];
for (var x in resources) {
widgets=widgets.concat(this.createWidget(x,resources[x]));
}
return widgets;
}
getValidWidgets(resources) {
var widgets = this.createWidgets(resources);
widgets=widgets.reduce((list,curVal,index) => {
if (curVal)
{
var delimiter = ",";
if (curVal.shouldBeDelimited)
list.push(`${curVal.value}${delimiter}`);
else
list.push(curVal.value);
}
return list;
},[])
widgets[widgets.length-1]=widgets[widgets.length-1].slice(0,-1); // Remove trailing comma, not very elegant
return widgets;
}
injectDashboard(args) {
this.log("Setting up dashboards for "+this.getServiceName());
var resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
this.log("Resources : "+Object.keys(resources).join(","));
var dashboard = this.getDashboardTemplate();
dashboard.Properties.DashboardBody["Fn::Join"][1].splice(
1,
0,
...this.getValidWidgets(resources)
);
this.serverless.service.provider.compiledCloudFormationTemplate.Resources["serverlesscloudwatchdashboardplugin"]=dashboard;
}
createWidget(name,resource) {
if (name=="ServerlessDeploymentBucket") {
this.log("Skipping deployment bucket");
return [];
}
else {
return this.createWidgetByType(name,resource);
}
}
createWidgetByType(name,resource){
var mapping = this.resourcemapping[resource.Type];
if (mapping) return mapping.createWidget(name,resource);
else this.log("Skipping "+name+" - unsupported resource type.");
}
getDashboardTemplate() {
return {
"Type": "AWS::CloudWatch::Dashboard",
"Properties": {
"DashboardName": this.getServiceName(),
"DashboardBody": {
"Fn::Join": [ "",['{"widgets":[',']}']]
}
}
}
}
}
module.exports=ServerlessCloudwatchDashboardPlugin;