From 7c3df5926a6a8e3fea799a9455a4d83bd6d05838 Mon Sep 17 00:00:00 2001 From: Troy Kelly Date: Fri, 20 Nov 2020 14:48:07 +1100 Subject: [PATCH] Adding support for egress and ingress settings Resolves #242 --- package/lib/compileFunctions.js | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index d24afbf..cc025cd 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -62,6 +62,18 @@ module.exports = { }); } + if (funcObject.egress) { + _.assign(funcTemplate.properties, { + vpcConnectorEgressSettings: _.get(funcObject, 'egress') || _.get(this, 'serverless.service.provider.egress'), + }); + } + + if (funcObject.ingress) { + _.assign(funcTemplate.properties, { + ingressSettings: _.get(funcObject, 'ingress') || _.get(this, 'serverless.service.provider.ingress'), + }); + } + if (funcObject.maxInstances) { funcTemplate.properties.maxInstances = funcObject.maxInstances; } @@ -157,6 +169,46 @@ const validateVpcConnectorProperty = (funcObject, functionName) => { } }; +/** + * 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 (funcObject.egress && typeof funcObject.egress === 'string') { + const validTypes = ['VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED', 'PRIVATE_RANGES_ONLY', 'ALL_TRAFFIC']; + if (!validTypes.includes(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); + } + } +}; + +/** + * 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 (funcObject.ingress && typeof funcObject.ingress === 'string') { + const validTypes = ['INGRESS_SETTINGS_UNSPECIFIED', 'ALLOW_ALL', 'ALLOW_INTERNAL_ONLY', 'ALLOW_INTERNAL_AND_GCLB']; + if (!validTypes.includes(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 {