From 29d1a70ca295ac7ecf35fa47b512a2564ed0169a Mon Sep 17 00:00:00 2001 From: "Jeremy Minhua Bao (US - ADVS)" Date: Tue, 3 Dec 2019 16:00:44 +0800 Subject: [PATCH 1/2] 1. Fix the unit test in compileFunction.test.js 2.Add vpc support in compile the cloud function --- package/lib/compileFunctions.js | 20 ++++++++++++ package/lib/compileFunctions.test.js | 46 +++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 1281582..6d58700 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -23,6 +23,7 @@ module.exports = { validateHandlerProperty(funcObject, functionName); validateEventsProperty(funcObject, functionName); + validateVpcConnectorProprety(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 validateVpcConnectorProprety = (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); + }); + }); }); }); + From 23ec71c2b2bbf3729358c880439a9001a23b2aac Mon Sep 17 00:00:00 2001 From: "Jeremy Minhua Bao (US - ADVS)" Date: Tue, 3 Dec 2019 16:33:46 +0800 Subject: [PATCH 2/2] fix spelling issue --- package/lib/compileFunctions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 6d58700..eac5d09 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -23,7 +23,7 @@ module.exports = { validateHandlerProperty(funcObject, functionName); validateEventsProperty(funcObject, functionName); - validateVpcConnectorProprety(funcObject, functionName); + validateVpcConnectorProperty(funcObject, functionName); const funcTemplate = getFunctionTemplate( funcObject, @@ -131,13 +131,13 @@ const validateEventsProperty = (funcObject, functionName) => { } }; -const validateVpcConnectorProprety = (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}', + ' 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);