-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcompileFunctions.js
227 lines (197 loc) · 8.29 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'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();
const projectName = _.get(this, 'serverless.service.provider.project');
this.serverless.service.provider.region =
this.serverless.service.provider.region || 'us-central1';
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);
validateVpcConnectorProperty(funcObject, functionName);
const funcTemplate = getFunctionTemplate(
funcObject,
projectName,
this.serverless.service.provider.region,
`gs://${this.serverless.service.provider.deploymentBucketName}/${this.serverless.service.package.artifactFilePath}`
);
funcTemplate.properties.serviceAccountEmail =
_.get(funcObject, 'serviceAccountEmail') ||
_.get(this, 'serverless.service.provider.serviceAccountEmail') ||
null;
funcTemplate.properties.availableMemoryMb =
_.get(funcObject, 'memorySize') ||
_.get(this, 'serverless.service.provider.memorySize') ||
256;
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 // eslint-disable-line comma-dangle
);
if (!funcTemplate.properties.serviceAccountEmail) {
delete funcTemplate.properties.serviceAccountEmail;
}
if (funcObject.vpc) {
_.assign(funcTemplate.properties, {
vpcConnector: _.get(funcObject, 'vpc') || _.get(this, 'serverless.service.provider.vpc'),
});
}
if (funcObject.egress) {
_.assign(funcTemplate.properties, {
vpcConnectorEgressSettings: _.get(funcObject, 'egress') || _.get(this, 'serverless.service.provider.egress'),
});
}
if (funcObject.ingress) {
_.assign(funcTemplate.properties, {
ingressSettings: _.get(funcObject, 'ingress') || _.get(this, 'serverless.service.provider.ingress'),
});
}
if (funcObject.maxInstances) {
funcTemplate.properties.maxInstances = funcObject.maxInstances;
}
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;
funcTemplate.properties.eventTrigger = {};
funcTemplate.properties.eventTrigger.eventType = type;
if (path) funcTemplate.properties.eventTrigger.path = path;
funcTemplate.properties.eventTrigger.resource = resource;
}
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 validateVpcConnectorProperty = (funcObject, functionName) => {
if (funcObject.vpc && typeof funcObject.vpc === 'string') {
const vpcNamePattern = /projects\/[\s\S]*\/locations\/[\s\S]*\/connectors\/[\s\S]*/i;
if (!vpcNamePattern.test(funcObject.vpc)) {
const errorMessage = [
`The function "${functionName}" has invalid vpc connection name`,
' VPC Connector name should follow projects/{project_id}/locations/{region}/connectors/{connector_name}',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
}
};
/**
* Validate the function egress settings per
* https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#vpcconnectoregresssettings
* @param {*} funcObject
* @param {*} functionName
*/
const validateVpcEgressProperty = (funcObject, functionName) => {
if (funcObject.egress && typeof funcObject.egress === 'string') {
const validTypes = ['VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED', 'PRIVATE_RANGES_ONLY', 'ALL_TRAFFIC'];
if (!validTypes.includes(funcObject.egress)) {
const errorMessage = [
`The function "${functionName}" has an invalid egress setting`,
' Egress setting should be ALL_TRAFFIC, PRIVATE_RANGES_ONLY or VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
}
};
/**
* Validate the function ingress settings per
* https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions#ingresssettings
* @param {*} funcObject
* @param {*} functionName
*/
const validateVpcIngressProperty = (funcObject, functionName) => {
if (funcObject.ingress && typeof funcObject.ingress === 'string') {
const validTypes = ['INGRESS_SETTINGS_UNSPECIFIED', 'ALLOW_ALL', 'ALLOW_INTERNAL_ONLY', 'ALLOW_INTERNAL_AND_GCLB'];
if (!validTypes.includes(funcObject.ingress)) {
const errorMessage = [
`The function "${functionName}" has an invalid ingress setting`,
' Ingress setting should be ALLOW_ALL, ALLOW_INTERNAL_ONLY, ALLOW_INTERNAL_AND_GCLB or INGRESS_SETTINGS_UNSPECIFIED',
' Please check the docs for more info.',
].join('');
throw new Error(errorMessage);
}
}
};
const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl) => {
//eslint-disable-line
return {
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
name: funcObject.name,
properties: {
parent: `projects/${projectName}/locations/${region}`,
availableMemoryMb: 256,
runtime: 'nodejs8',
timeout: '60s',
entryPoint: funcObject.handler,
function: funcObject.name,
sourceArchiveUrl,
},
};
};