Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for egress and ingress settings #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ module.exports = {
});
}

if (funcObject.egress) {
_.assign(funcTemplate.properties, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use Object.assign

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just following existing established formatting.
I will come back to these change requests in a few weeks, we will just have to work with our fork for now.

vpcConnectorEgressSettings: _.get(funcObject, 'egress') || _.get(this, 'serverless.service.provider.egress'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.get(funcObject, 'egress') is guaranteed to be truthy at this logic point

});
}

if (funcObject.ingress) {
_.assign(funcTemplate.properties, {
ingressSettings: _.get(funcObject, 'ingress') || _.get(this, 'serverless.service.provider.ingress'),
Comment on lines +71 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

});
}

if (funcObject.maxInstances) {
funcTemplate.properties.maxInstances = funcObject.maxInstances;
}
Expand Down Expand Up @@ -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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do just typeof check (other is superfluous)

const validTypes = ['VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED', 'PRIVATE_RANGES_ONLY', 'ALL_TRAFFIC'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define it as Set instance and outside of function body

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'];
Comment on lines +199 to +200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

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 {
Expand Down