Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {

validateHandlerProperty(funcObject, functionName);
validateEventsProperty(funcObject, functionName);
validateVpcConnectorProperty(funcObject, functionName);

const funcTemplate = getFunctionTemplate(
funcObject,
Expand All @@ -49,6 +50,11 @@ module.exports = {
funcObject.environment // eslint-disable-line comma-dangle
);

if (funcObject.vpc) {
_.assign(funcTemplate.properties, { vpcConnector: _.get(funcObject, 'vpc')
|| _.get(this, 'serverless.service.provider.vpc') });
}

if (!_.size(funcTemplate.properties.environmentVariables)) {
delete funcTemplate.properties.environmentVariables;
}
Expand Down Expand Up @@ -125,6 +131,20 @@ const validateEventsProperty = (funcObject, functionName) => {
}
};

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, region, sourceArchiveUrl) => { //eslint-disable-line
return {
type: 'cloudfunctions.v1beta2.function',
Expand Down
46 changes: 42 additions & 4 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,12 @@ describe('CompileFunctions', () => {
};

const compiledResources = [{
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
parent: 'projects/myProject/locations/us-central1',
location: 'us-central1',
runtime: 'nodejs8',
function: 'my-service-dev-func1',
entryPoint: 'func1',
function: 'func1',
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test_var',
Expand Down Expand Up @@ -598,5 +597,44 @@ describe('CompileFunctions', () => {
.toEqual(compiledResources);
});
});

it('should set vpc connection base on the function configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
memorySize: 128,
runtime: 'nodejs8',
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
events: [
{ http: 'foo' },
],
},
};

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 128,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
labels: {},
vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
},
}];

return googlePackage.compileFunctions().then(() => {
expect(consoleLogStub.called).toEqual(true);
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});
});
});