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

Add some tests and change code style as requested #245

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
67 changes: 66 additions & 1 deletion package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
128 changes: 128 additions & 0 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down