-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathcompileFunctions.js
More file actions
142 lines (120 loc) · 5.1 KB
/
compileFunctions.js
File metadata and controls
142 lines (120 loc) · 5.1 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
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');
const { validateEventsProperty } = require('../../shared/validate');
module.exports = {
compileFunction(functionName, projectName) {
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.artifactDirectoryName}/${functionName}.zip`
);
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 = this.provider.getRuntime(funcObject);
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables =
this.provider.getConfiguredEnvironment(funcObject);
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.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);
},
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}`;
const allFunctions = this.serverless.service.getAllFunctions();
return BbPromise.each(allFunctions, (functionName) =>
this.compileFunction(functionName, projectName)
);
},
};
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 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);
}
}
};
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: 'nodejs10',
timeout: '60s',
entryPoint: funcObject.handler,
function: funcObject.name,
sourceArchiveUrl,
},
};
};