-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathinvokeFunction.js
71 lines (57 loc) · 1.66 KB
/
invokeFunction.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
'use strict';
/* eslint no-use-before-define: 0 */
const BbPromise = require('bluebird');
const chalk = require('chalk');
module.exports = {
invokeFunction() {
return BbPromise.bind(this).then(this.invoke).then(this.printResult);
},
invoke() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
let func = this.options.function;
const data = this.options.data || '';
func = getGoogleCloudFunctionName(this.serverless.service.functions, func);
const params = {
name: `projects/${project}/locations/${region}/functions/${func}`,
resource: {
data,
},
};
return this.provider.request(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
params
);
},
printResult(result) {
let res = result;
if (!result || !result.result) {
res = {
executionId: 'error',
result: 'An error occurred while executing your function...',
};
}
const log = `${chalk.grey(res.executionId)} ${res.result}`;
if (this.provider.writeText) {
this.provider.writeText(log);
} else {
this.serverless.cli.log(log);
}
return BbPromise.resolve();
},
};
// retrieve the functions name (Google uses our handler property as the function name)
const getGoogleCloudFunctionName = (serviceFunctions, func) => {
if (!serviceFunctions[func]) {
const errorMessage = [
`Function "${func}" not found. `,
'Please check your "serverless.yml" file for the correct function name.',
].join('');
throw new Error(errorMessage);
}
return serviceFunctions[func].name;
};