-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcompileFunctions.js
142 lines (119 loc) · 4.91 KB
/
compileFunctions.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
'use strict';
/* eslint no-use-before-define: 0 */
const path = require('path');
const _ = require('lodash');
const BbPromise = require('bluebird');
module.exports = {
compileFunctions() {
const artifactFilePath = this.serverless.service.package.artifact;
const fileName = artifactFilePath.split(path.sep).pop();
this.serverless.service.package
.artifactFilePath = `${this.serverless.service.package.artifactDirectoryName}/${fileName}`;
this.serverless.service.getAllFunctions().forEach((functionName) => {
const funcObject = this.serverless.service.getFunction(functionName);
this.serverless.cli
.log(`Compiling function "${functionName}"...`);
validateHandlerProperty(funcObject, functionName);
validateEventsProperty(funcObject, functionName);
const funcTemplate = getFunctionTemplate(
funcObject,
this.serverless.service.provider.region,
`gs://${
this.serverless.service.provider.deploymentBucketName
}/${this.serverless.service.package.artifactFilePath}`);
funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize')
|| _.get(this, 'serverless.service.provider.memorySize')
|| 256;
funcTemplate.properties.location = _.get(funcObject, 'location')
|| _.get(this, 'serverless.service.provider.region')
|| 'us-central1';
funcTemplate.properties.runtime = _.get(funcObject, 'runtime')
|| _.get(this, 'serverless.service.provider.runtime')
|| 'nodejs8';
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
|| _.get(this, 'serverless.service.provider.timeout')
|| '60s';
funcTemplate.properties.environmentVariables = _.merge(
_.get(this, 'serverless.service.provider.environment'),
funcObject.environment,
);
if (!_.size(funcTemplate.properties.environmentVariables)) {
delete funcTemplate.properties.environmentVariables;
}
funcTemplate.properties.labels = _.assign({},
_.get(this, 'serverless.service.provider.labels') || {},
_.get(funcObject, 'labels') || {} // eslint-disable-line comma-dangle
);
const eventType = Object.keys(funcObject.events[0])[0];
if (eventType === 'http') {
const url = funcObject.events[0].http;
funcTemplate.properties.httpsTrigger = {};
funcTemplate.properties.httpsTrigger.url = url;
}
if (eventType === 'event') {
const type = funcObject.events[0].event.eventType;
const path = funcObject.events[0].event.path; //eslint-disable-line
const resource = funcObject.events[0].event.resource;
const failurePolicy = funcObject.events[0].event.failurePolicy;
funcTemplate.properties.eventTrigger = {};
funcTemplate.properties.eventTrigger.eventType = type;
if (path) funcTemplate.properties.eventTrigger.path = path;
funcTemplate.properties.eventTrigger.resource = resource;
if (failurePolicy) funcTemplate.properties.eventTrigger.failurePolicy = failurePolicy;
}
this.serverless.service.provider.compiledConfigurationTemplate.resources.push(funcTemplate);
});
return BbPromise.resolve();
},
};
const validateHandlerProperty = (funcObject, functionName) => {
if (!funcObject.handler) {
const errorMessage = [
`Missing "handler" property for function "${functionName}".`,
' Your function needs a "handler".',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
};
const validateEventsProperty = (funcObject, functionName) => {
if (!funcObject.events || funcObject.events.length === 0) {
const errorMessage = [
`Missing "events" property for function "${functionName}".`,
' Your function needs at least one "event".',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
if (funcObject.events.length > 1) {
const errorMessage = [
`The function "${functionName}" has more than one event.`,
' Only one event per function is supported.',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
const supportedEvents = ['http', 'event'];
const eventType = Object.keys(funcObject.events[0])[0];
if (supportedEvents.indexOf(eventType) === -1) {
const errorMessage = [
`Event type "${eventType}" of function "${functionName}" not supported.`,
` supported event types are: ${supportedEvents.join(', ')}`,
].join('');
throw new Error(errorMessage);
}
};
const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint-disable-line
return {
type: 'cloudfunctions.v1beta2.function',
name: funcObject.name,
properties: {
location: region,
availableMemoryMb: 256,
runtime: 'nodejs8',
timeout: '60s',
function: funcObject.handler,
sourceArchiveUrl,
},
};
};