From 8aa0bd37b0363887009ad4158cb1c30d065e72b5 Mon Sep 17 00:00:00 2001 From: David Tabachnikov Date: Fri, 17 Aug 2018 16:45:38 +0200 Subject: [PATCH] feat: add support for environment variables in Cloud Functions --- package/lib/compileFunctions.js | 9 ++++ package/lib/compileFunctions.test.js | 80 ++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 56eb8ac..4c4c194 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -43,6 +43,15 @@ module.exports = { funcTemplate.properties.timeout = _.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s'; + funcTemplate.properties.environmentVariables = _.merge( + _.get(this, 'serverless.service.provider.environment'), + funcObject.environment, + ); + + if (!_.size(funcTemplate.properties.environmentVariables)) { + delete funcTemplate.properties.environmentVariables; + } + funcTemplate.properties.labels = _.assign({}, _.get(this, 'serverless.service.provider.labels') || {}, _.get(funcObject, 'labels') || {}, diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index 30fd6b8..4cfde55 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -360,6 +360,86 @@ describe('CompileFunctions', () => { }); }); + it('should set the environment variables based on the function configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + environment: { + TEST_VAR: 'test', + }, + events: [ + { http: 'foo' }, + ], + }, + }; + + const compiledResources = [{ + type: 'cloudfunctions.v1beta2.function', + name: 'my-service-dev-func1', + properties: { + location: 'us-central1', + runtime: 'nodejs8', + function: 'func1', + availableMemoryMb: 256, + environmentVariables: { + TEST_VAR: 'test', + }, + timeout: '60s', + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'foo', + }, + labels: {}, + }, + }]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.calledOnce).toEqual(true); + expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources) + .toEqual(compiledResources); + }); + }); + + it('should set the environment variables based on the provider configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + events: [ + { http: 'foo' }, + ], + }, + }; + googlePackage.serverless.service.provider.environment = { + TEST_VAR: 'test', + }; + + const compiledResources = [{ + type: 'cloudfunctions.v1beta2.function', + name: 'my-service-dev-func1', + properties: { + location: 'us-central1', + runtime: 'nodejs8', + function: 'func1', + availableMemoryMb: 256, + environmentVariables: { + TEST_VAR: 'test', + }, + timeout: '60s', + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'foo', + }, + labels: {}, + }, + }]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.calledOnce).toEqual(true); + expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources) + .toEqual(compiledResources); + }); + }); + it('should compile "http" events properly', () => { googlePackage.serverless.service.functions = { func1: {