-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (175 loc) · 5.04 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
const request = require('request');
const LambdaInfo = require('./lib/lambda-info');
const AnnouncerException = require('./lib/announcer-exception');
/* eslint-disable require-jsdoc */
// TODO: write proper jsdoc
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.service = this.serverless.service;
this.provider = this.serverless.getProvider('aws');
this.servicePath = this.serverless.config.servicePath || '';
this.hooks = {
'after:deploy:deploy': this.handle.bind(this),
};
this.httpRequester = {};
// Set httpRequester object to be `request` by default
this.setRequestObject(request);
}
/**
* A setter function to set the requester object to be used for the
* http request - could be used and adjusted for different implementations
* like using `request` or `fetch` or any other package
* @param {Function | any} httpRequester
*/
setRequestObject(httpRequester) {
this.httpRequester = httpRequester;
}
/**
* Handles the announcement of the service
* @return {Promise}
*/
handle() {
const sls = this.serverless;
sls.cli.consoleLog('----- Lambda Announcer -----');
const announcer = this.resolveAnnouncerHook(this.service, sls);
const contract = this.resolveContractDefinition(this.service, sls);
if (!announcer.hook) {
sls.cli.log('!_Announcer hook is not specified correctly_!');
return;
}
return LambdaInfo
.getLambdaInfo(sls)
.then((info) => {
this.outputLambdaInfo(sls, info, announcer);
return (contract) ? this.mergeInfoAndContract(info, contract) : info;
})
.then((info) => {
return this.announce(announcer, info);
})
.then((result) => {
sls.cli.log(`Announcement result: ${result.statusCode}`);
return true;
})
.then((result) => {
sls.cli.consoleLog(`----- ----- ----- -----`);
})
.catch((error) => {
sls.cli.log(error);
});
}
/**
* Fires the hook specified in the announcer with the data passed as info
* @param {{hook: String}} announcer
* @param {Array} info
* @return {Promise}
*/
announce(announcer, info) {
const options = {
method: 'post',
body: info,
json: true,
url: announcer.hook,
};
return new Promise((resolve, reject) => {
this.httpRequester(options, (err, res) => {
if (err) {
reject(new AnnouncerException('Failed to post an announcement'));
return;
}
resolve(res);
return;
});
}
);
}
/**
* Gathers the configuration of the announcer from serverless configuration
* @param {Object} service
* @param {Object} sls
* @return {Object}
*/
getAnnouncerConfiguration(service, sls) {
// service.custom can be an object or an array
const custom = service.custom;
let announcers = [];
if (Array.isArray(custom)) {
announcers = custom
.filter((s) => s.announcer)
.map((s) => s.announcer);
} else {
announcers = custom.announcer ? [custom.announcer] : [];
}
if (announcers.length != 1) {
sls.cli
.consoleLog('!_Number of found announcer settings is not one..._!');
return {};
}
return announcers.pop();
}
/**
* Returns the hook specified in the announcer
* @param {*} service
* @param {*} sls
* @return {{hook: String}}
*/
resolveAnnouncerHook(service, sls) {
const announcer = this.getAnnouncerConfiguration(service, sls);
return {
hook: announcer.hook || '',
};
}
/**
* Returns the contract specification from the serverless config
* @param {*} service
* @param {*} sls
* @return {*}
*/
resolveContractDefinition(service, sls) {
// Currently uses internal endpoint documentation approach.
const announcer = this.getAnnouncerConfiguration(service, sls);
if (announcer.contract) {
sls.cli.log('found contract');
sls.cli.consoleLog(announcer.contract);
} else {
sls.cli.log('no contract found');
}
return announcer.contract || {};
}
/**
* Merges the function information with related contract information (if any)
* @param {Array} info
* @param {*} contract
* @return {Array}
*/
mergeInfoAndContract(info, contract) {
if (!contract) {
return info;
}
return info.map((i) => {
const functionContract = {contract: contract[`/${i.identifier}`]};
if (!functionContract.contract) {
// no matching contract found
return i;
} else {
return Object.assign(functionContract, i);
}
});
}
/**
* Prints the results of findings to the console.
* @param {*} sls
* @param {*} info
* @param {*} announcer
*/
outputLambdaInfo(sls, info, announcer) {
sls.cli.log('Gathered Lambda Info for functions:');
sls.cli.consoleLog(info.map((i) => {
return i.name;
}));
sls.cli.log(`Announcing to: ${announcer.hook}`);
}
}
module.exports = ServerlessPlugin;