Skip to content

Commit 66ba98f

Browse files
feat: Support provider.minInstances(#283)
1 parent f10060d commit 66ba98f

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

package/lib/compileFunctions.js

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ module.exports = {
7373
funcTemplate.properties.maxInstances = funcObject.maxInstances;
7474
}
7575

76+
if (funcObject.minInstances) {
77+
funcTemplate.properties.minInstances = funcObject.minInstances;
78+
}
79+
7680
if (!_.size(funcTemplate.properties.environmentVariables)) {
7781
delete funcTemplate.properties.environmentVariables;
7882
}

package/lib/compileFunctions.test.js

+109
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,115 @@ describe('CompileFunctions', () => {
766766
).toEqual(compiledResources);
767767
});
768768
});
769+
770+
it('should set min instances on the function configuration', () => {
771+
googlePackage.serverless.service.functions = {
772+
func1: {
773+
handler: 'func1',
774+
memorySize: 128,
775+
runtime: 'nodejs10',
776+
minInstances: 5,
777+
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
778+
events: [{ http: 'foo' }],
779+
},
780+
};
781+
782+
const compiledResources = [
783+
{
784+
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
785+
name: 'my-service-dev-func1',
786+
properties: {
787+
parent: 'projects/myProject/locations/us-central1',
788+
runtime: 'nodejs10',
789+
function: 'my-service-dev-func1',
790+
entryPoint: 'func1',
791+
availableMemoryMb: 128,
792+
timeout: '60s',
793+
minInstances: 5,
794+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
795+
httpsTrigger: {
796+
url: 'foo',
797+
},
798+
labels: {},
799+
vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
800+
},
801+
},
802+
];
803+
804+
return googlePackage.compileFunctions().then(() => {
805+
expect(consoleLogStub.called).toEqual(true);
806+
expect(
807+
googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources
808+
).toEqual(compiledResources);
809+
});
810+
});
811+
812+
it('should not require min instances on each function configuration', () => {
813+
googlePackage.serverless.service.functions = {
814+
func1: {
815+
handler: 'func1',
816+
memorySize: 128,
817+
runtime: 'nodejs10',
818+
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
819+
events: [{ http: 'foo' }],
820+
},
821+
func2: {
822+
handler: 'func2',
823+
memorySize: 128,
824+
runtime: 'nodejs10',
825+
minInstances: 5,
826+
vpc: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
827+
events: [{ http: 'bar' }],
828+
},
829+
};
830+
831+
const compiledResources = [
832+
{
833+
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
834+
name: 'my-service-dev-func1',
835+
properties: {
836+
parent: 'projects/myProject/locations/us-central1',
837+
runtime: 'nodejs10',
838+
function: 'my-service-dev-func1',
839+
entryPoint: 'func1',
840+
availableMemoryMb: 128,
841+
timeout: '60s',
842+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
843+
httpsTrigger: {
844+
url: 'foo',
845+
},
846+
labels: {},
847+
vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
848+
},
849+
},
850+
{
851+
type: 'gcp-types/cloudfunctions-v1:projects.locations.functions',
852+
name: 'my-service-dev-func2',
853+
properties: {
854+
parent: 'projects/myProject/locations/us-central1',
855+
runtime: 'nodejs10',
856+
function: 'my-service-dev-func2',
857+
entryPoint: 'func2',
858+
availableMemoryMb: 128,
859+
timeout: '60s',
860+
minInstances: 5,
861+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
862+
httpsTrigger: {
863+
url: 'bar',
864+
},
865+
labels: {},
866+
vpcConnector: 'projects/pg-us-n-app-123456/locations/us-central1/connectors/my-vpc',
867+
},
868+
},
869+
];
870+
871+
return googlePackage.compileFunctions().then(() => {
872+
expect(consoleLogStub.called).toEqual(true);
873+
expect(
874+
googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources
875+
).toEqual(compiledResources);
876+
});
877+
});
769878
});
770879

771880
it('should allow vpc as short name', () => {

provider/googleProvider.js

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class GoogleProvider {
129129
serviceAccountEmail: { type: 'string' }, // Override provider configuration
130130
memorySize: { $ref: '#/definitions/cloudFunctionMemory' }, // Override provider configuration
131131
timeout: { type: 'string' }, // Override provider configuration
132+
minInstances: { type: 'number' },
132133
environment: { $ref: '#/definitions/cloudFunctionEnvironmentVariables' }, // Override provider configuration
133134
vpc: { type: 'string' }, // Override provider configuration
134135
vpcEgress: { $ref: '#/definitions/cloudFunctionVpcEgress' }, // Can be overridden by function configuration

0 commit comments

Comments
 (0)