From 7053c49d114913468039194122c6846fb34211cd Mon Sep 17 00:00:00 2001 From: Jorge Montanez Date: Thu, 16 Dec 2021 17:08:53 -0500 Subject: [PATCH] - Adds support of GCP minInstances - Adds test covering minInstances - Updates google provider schema to support minInstances --- package/lib/compileFunctions.js | 4 + package/lib/compileFunctions.test.js | 109 +++++++++++++++++++++++++++ provider/googleProvider.js | 1 + 3 files changed, 114 insertions(+) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 97809ee..caec35b 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -73,6 +73,10 @@ module.exports = { funcTemplate.properties.maxInstances = funcObject.maxInstances; } + if (funcObject.minInstances) { + funcTemplate.properties.minInstances = funcObject.minInstances; + } + if (!_.size(funcTemplate.properties.environmentVariables)) { delete funcTemplate.properties.environmentVariables; } diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index d52d9a0..c7ce612 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -766,6 +766,115 @@ describe('CompileFunctions', () => { ).toEqual(compiledResources); }); }); + + it('should set min instances on the function configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + memorySize: 128, + runtime: 'nodejs10', + minInstances: 5, + vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + 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: 'nodejs10', + function: 'my-service-dev-func1', + entryPoint: 'func1', + availableMemoryMb: 128, + timeout: '60s', + minInstances: 5, + 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', + }, + }, + ]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.called).toEqual(true); + expect( + googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources + ).toEqual(compiledResources); + }); + }); + + it('should not require min instances on each function configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + memorySize: 128, + runtime: 'nodejs10', + vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + events: [{ http: 'foo' }], + }, + func2: { + handler: 'func2', + memorySize: 128, + runtime: 'nodejs10', + minInstances: 5, + vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + events: [{ http: 'bar' }], + }, + }; + + const compiledResources = [ + { + type: 'gcp-types/cloudfunctions-v1:projects.locations.functions', + name: 'my-service-dev-func1', + properties: { + parent: 'projects/myProject/locations/us-central1', + runtime: 'nodejs10', + 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', + }, + }, + { + type: 'gcp-types/cloudfunctions-v1:projects.locations.functions', + name: 'my-service-dev-func2', + properties: { + parent: 'projects/myProject/locations/us-central1', + runtime: 'nodejs10', + function: 'my-service-dev-func2', + entryPoint: 'func2', + availableMemoryMb: 128, + timeout: '60s', + minInstances: 5, + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'bar', + }, + labels: {}, + vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc', + }, + }, + ]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.called).toEqual(true); + expect( + googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources + ).toEqual(compiledResources); + }); + }); }); it('should allow vpc as short name', () => { diff --git a/provider/googleProvider.js b/provider/googleProvider.js index 199dfe2..dbdaf37 100644 --- a/provider/googleProvider.js +++ b/provider/googleProvider.js @@ -129,6 +129,7 @@ class GoogleProvider { serviceAccountEmail: { type: 'string' }, // Override provider configuration memorySize: { $ref: '#/definitions/cloudFunctionMemory' }, // Override provider configuration timeout: { type: 'string' }, // Override provider configuration + minInstances: { type: 'number' }, environment: { $ref: '#/definitions/cloudFunctionEnvironmentVariables' }, // Override provider configuration vpc: { type: 'string' }, // Override provider configuration vpcEgress: { $ref: '#/definitions/cloudFunctionVpcEgress' }, // Can be overridden by function configuration