diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 1281582..eac5d09 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -23,6 +23,7 @@ module.exports = { validateHandlerProperty(funcObject, functionName); validateEventsProperty(funcObject, functionName); + validateVpcConnectorProperty(funcObject, functionName); const funcTemplate = getFunctionTemplate( funcObject, @@ -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; } @@ -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', diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index 05ea541..2d622ca 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -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', @@ -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); + }); + }); }); }); +