Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 930987c

Browse files
authored
Merge pull request #191 from bao1018/190_VPC_Support
GCP VPC Support and Unit Test fix
2 parents e080694 + 23ec71c commit 930987c

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

‎package/lib/compileFunctions.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323

2424
validateHandlerProperty(funcObject, functionName);
2525
validateEventsProperty(funcObject, functionName);
26+
validateVpcConnectorProperty(funcObject, functionName);
2627

2728
const funcTemplate = getFunctionTemplate(
2829
funcObject,
@@ -49,6 +50,11 @@ module.exports = {
4950
funcObject.environment // eslint-disable-line comma-dangle
5051
);
5152

53+
if (funcObject.vpc) {
54+
_.assign(funcTemplate.properties, { vpcConnector: _.get(funcObject, 'vpc')
55+
|| _.get(this, 'serverless.service.provider.vpc') });
56+
}
57+
5258
if (!_.size(funcTemplate.properties.environmentVariables)) {
5359
delete funcTemplate.properties.environmentVariables;
5460
}
@@ -125,6 +131,20 @@ const validateEventsProperty = (funcObject, functionName) => {
125131
}
126132
};
127133

134+
const validateVpcConnectorProperty = (funcObject, functionName) => {
135+
if (funcObject.vpc && typeof funcObject.vpc === 'string') {
136+
const vpcNamePattern = /projects\/[\s\S]*\/locations\/[\s\S]*\/connectors\/[\s\S]*/i;
137+
if (!vpcNamePattern.test(funcObject.vpc)) {
138+
const errorMessage = [
139+
`The function "${functionName}" has invalid vpc connection name`,
140+
' VPC Connector name should follow projects/{project_id}/locations/{region}/connectors/{connector_name}',
141+
' Please check the docs for more info.',
142+
].join('');
143+
throw new Error(errorMessage);
144+
}
145+
}
146+
};
147+
128148
const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint-disable-line
129149
return {
130150
type: 'cloudfunctions.v1beta2.function',

‎package/lib/compileFunctions.test.js‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,12 @@ describe('CompileFunctions', () => {
459459
};
460460

461461
const compiledResources = [{
462-
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
462+
type: 'cloudfunctions.v1beta2.function',
463463
name: 'my-service-dev-func1',
464464
properties: {
465-
parent: 'projects/myProject/locations/us-central1',
465+
location: 'us-central1',
466466
runtime: 'nodejs8',
467-
function: 'my-service-dev-func1',
468-
entryPoint: 'func1',
467+
function: 'func1',
469468
availableMemoryMb: 256,
470469
environmentVariables: {
471470
TEST_VAR: 'test_var',
@@ -598,5 +597,44 @@ describe('CompileFunctions', () => {
598597
.toEqual(compiledResources);
599598
});
600599
});
600+
601+
it('should set vpc connection base on the function configuration', () => {
602+
googlePackage.serverless.service.functions = {
603+
func1: {
604+
handler: 'func1',
605+
memorySize: 128,
606+
runtime: 'nodejs8',
607+
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
608+
events: [
609+
{ http: 'foo' },
610+
],
611+
},
612+
};
613+
614+
const compiledResources = [{
615+
type: 'cloudfunctions.v1beta2.function',
616+
name: 'my-service-dev-func1',
617+
properties: {
618+
location: 'us-central1',
619+
runtime: 'nodejs8',
620+
function: 'func1',
621+
availableMemoryMb: 128,
622+
timeout: '60s',
623+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
624+
httpsTrigger: {
625+
url: 'foo',
626+
},
627+
labels: {},
628+
vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
629+
},
630+
}];
631+
632+
return googlePackage.compileFunctions().then(() => {
633+
expect(consoleLogStub.called).toEqual(true);
634+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
635+
.toEqual(compiledResources);
636+
});
637+
});
601638
});
602639
});
640+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /