diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index c9dd256..413f137 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -24,6 +24,8 @@ module.exports = { validateHandlerProperty(funcObject, functionName); validateEventsProperty(funcObject, functionName); validateVpcConnectorProperty(funcObject, functionName); + validateVpcEgressProperty(funcObject, functionName); + validateVpcIngressProperty(funcObject, functionName); const funcTemplate = getFunctionTemplate( funcObject, @@ -57,11 +59,25 @@ module.exports = { } if (funcObject.vpc) { - _.assign(funcTemplate.properties, { + Object.assign(funcTemplate.properties, { vpcConnector: _.get(funcObject, 'vpc') || _.get(this, 'serverless.service.provider.vpc'), }); } + if (funcObject.egress) { + Object.assign(funcTemplate.properties, { + vpcConnectorEgressSettings: + _.get(funcObject, 'egress') || _.get(this, 'serverless.service.provider.egress'), + }); + } + + if (funcObject.ingress) { + Object.assign(funcTemplate.properties, { + ingressSettings: + _.get(funcObject, 'ingress') || _.get(this, 'serverless.service.provider.ingress'), + }); + } + if (funcObject.maxInstances) { funcTemplate.properties.maxInstances = funcObject.maxInstances; } @@ -157,6 +173,55 @@ const validateVpcConnectorProperty = (funcObject, functionName) => { } }; +const validEgressTypes = new Set([ + 'VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED', + 'PRIVATE_RANGES_ONLY', + 'ALL_TRAFFIC', +]); +/** + * 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 (typeof funcObject.egress === 'string') { + if (!validEgressTypes.has(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); + } + } +}; + +const validIngressTypes = new Set([ + 'INGRESS_SETTINGS_UNSPECIFIED', + 'ALLOW_ALL', + 'ALLOW_INTERNAL_ONLY', + 'ALLOW_INTERNAL_AND_GCLB', +]); +/** + * 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 (typeof funcObject.ingress === 'string') { + if (!validIngressTypes.has(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 { diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index a33e771..ea4cc2e 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -93,6 +93,50 @@ describe('CompileFunctions', () => { expect(() => googlePackage.compileFunctions()).toThrow(Error); }); + it('should throw an error if the vpc connector property is invalid', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + memorySize: 128, + runtime: 'nodejs8', + vpc: 'project/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + events: [{ http: 'foo' }], + }, + }; + + expect(() => googlePackage.compileFunctions()).toThrow(Error); + }); + + it('should throw an error if the vpc connector egress property is invalid', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + memorySize: 128, + runtime: 'nodejs8', + vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + egress: 'foo', + events: [{ http: 'foo' }], + }, + }; + + expect(() => googlePackage.compileFunctions()).toThrow(Error); + }); + + it('should throw an error if the vpc connector ingress property is invalid', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + memorySize: 128, + runtime: 'nodejs8', + vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + ingress: 'foo', + events: [{ http: 'foo' }], + }, + }; + + expect(() => googlePackage.compileFunctions()).toThrow(Error); + }); + it('should set the memory size based on the functions configuration', () => { googlePackage.serverless.service.functions = { func1: { @@ -658,6 +702,90 @@ describe('CompileFunctions', () => { }); }); + it('should set vpc egress 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', + egress: 'ALL_TRAFFIC', + events: [{ http: 'foo' }], + }, + }; + + const compiledResources = [ + { + type: 'gcp-types/cloudfunctions-v1:projects.locations.functions', + name: 'my-service-dev-func1', + properties: { + parent: 'projects/myProject/locations/us-central1', + runtime: 'nodejs8', + function: 'my-service-dev-func1', + entryPoint: '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', + vpcConnectorEgressSettings: 'ALL_TRAFFIC', + }, + }, + ]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.called).toEqual(true); + expect( + googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources + ).toEqual(compiledResources); + }); + }); + + it('should set vpc ingress 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', + ingress: 'ALLOW_ALL', + events: [{ http: 'foo' }], + }, + }; + + const compiledResources = [ + { + type: 'gcp-types/cloudfunctions-v1:projects.locations.functions', + name: 'my-service-dev-func1', + properties: { + parent: 'projects/myProject/locations/us-central1', + runtime: 'nodejs8', + function: 'my-service-dev-func1', + entryPoint: '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', + ingressSettings: 'ALLOW_ALL', + }, + }, + ]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.called).toEqual(true); + expect( + googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources + ).toEqual(compiledResources); + }); + }); + it('should set max instances on the function configuration', () => { googlePackage.serverless.service.functions = { func1: {