From bb36a23bd8aed616f085b7f69b4f8a0f2d160509 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Tue, 16 May 2017 15:17:08 +0200 Subject: [PATCH] Update GoogleDeploy plugin --- deploy/googleDeploy.js | 28 +- deploy/googleDeploy.test.js | 50 +-- deploy/lib/compileFunctions.js | 120 ------- deploy/lib/compileFunctions.test.js | 332 ------------------ deploy/lib/createDeployment.js | 11 - deploy/lib/createDeployment.test.js | 39 +- deploy/lib/generateArtifactDirectoryName.js | 16 - .../lib/generateArtifactDirectoryName.test.js | 33 -- deploy/lib/mergeServiceResources.js | 26 -- deploy/lib/mergeServiceResources.test.js | 101 ------ deploy/lib/prepareDeployment.js | 47 --- deploy/lib/prepareDeployment.test.js | 65 ---- deploy/lib/updateDeployment.js | 11 - deploy/lib/updateDeployment.test.js | 39 +- .../templates/core-configuration-template.yml | 3 - 15 files changed, 18 insertions(+), 903 deletions(-) delete mode 100644 deploy/lib/compileFunctions.js delete mode 100644 deploy/lib/compileFunctions.test.js delete mode 100644 deploy/lib/generateArtifactDirectoryName.js delete mode 100644 deploy/lib/generateArtifactDirectoryName.test.js delete mode 100644 deploy/lib/mergeServiceResources.js delete mode 100644 deploy/lib/mergeServiceResources.test.js delete mode 100644 deploy/lib/prepareDeployment.js delete mode 100644 deploy/lib/prepareDeployment.test.js delete mode 100644 deploy/templates/core-configuration-template.yml diff --git a/deploy/googleDeploy.js b/deploy/googleDeploy.js index 3a1c2aa..5b7b3d6 100644 --- a/deploy/googleDeploy.js +++ b/deploy/googleDeploy.js @@ -5,12 +5,8 @@ const BbPromise = require('bluebird'); const validate = require('../shared/validate'); const utils = require('../shared/utils'); const setDeploymentBucketName = require('../shared/setDeploymentBucketName'); -const prepareDeployment = require('./lib/prepareDeployment'); const createDeployment = require('./lib/createDeployment'); const monitorDeployment = require('../shared/monitorDeployment'); -const generateArtifactDirectoryName = require('./lib/generateArtifactDirectoryName'); -const compileFunctions = require('./lib/compileFunctions'); -const mergeServiceResources = require('./lib/mergeServiceResources'); const uploadArtifacts = require('./lib/uploadArtifacts'); const updateDeployment = require('./lib/updateDeployment'); const cleanupDeploymentBucket = require('./lib/cleanupDeploymentBucket'); @@ -26,36 +22,24 @@ class GoogleDeploy { validate, utils, setDeploymentBucketName, - prepareDeployment, createDeployment, monitorDeployment, - generateArtifactDirectoryName, - compileFunctions, - mergeServiceResources, uploadArtifacts, updateDeployment, cleanupDeploymentBucket); this.hooks = { - 'before:deploy:initialize': () => BbPromise.bind(this) + 'before:deploy:deploy': () => BbPromise.bind(this) .then(this.validate) .then(this.setDefaults), - 'deploy:initialize': () => BbPromise.bind(this) - .then(this.setDeploymentBucketName) - .then(this.prepareDeployment), - - 'deploy:setupProviderConfiguration': () => BbPromise.bind(this) - .then(this.createDeployment), - - 'before:deploy:compileFunctions': () => BbPromise.bind(this) - .then(this.generateArtifactDirectoryName) - .then(this.compileFunctions), - 'deploy:deploy': () => BbPromise.bind(this) - .then(this.mergeServiceResources) + .then(this.setDeploymentBucketName) + .then(this.createDeployment) .then(this.uploadArtifacts) - .then(this.updateDeployment) + .then(this.updateDeployment), + + 'after:deploy:deploy': () => BbPromise.bind(this) .then(this.cleanupDeploymentBucket), }; } diff --git a/deploy/googleDeploy.test.js b/deploy/googleDeploy.test.js index c102208..f38ac10 100644 --- a/deploy/googleDeploy.test.js +++ b/deploy/googleDeploy.test.js @@ -39,11 +39,7 @@ describe('GoogleDeploy', () => { let validateStub; let setDefaultsStub; let setDeploymentBucketNameStub; - let prepareDeploymentStub; let createDeploymentStub; - let generateArtifactDirectoryNameStub; - let compileFunctionsStub; - let mergeServiceResourcesStub; let uploadArtifactsStub; let updateDeploymentStub; let cleanupDeploymentBucketStub; @@ -55,17 +51,8 @@ describe('GoogleDeploy', () => { .returns(BbPromise.resolve()); setDeploymentBucketNameStub = sinon.stub(googleDeploy, 'setDeploymentBucketName') .returns(BbPromise.resolve()); - prepareDeploymentStub = sinon.stub(googleDeploy, 'prepareDeployment') - .returns(BbPromise.resolve()); createDeploymentStub = sinon.stub(googleDeploy, 'createDeployment') .returns(BbPromise.resolve()); - generateArtifactDirectoryNameStub = sinon - .stub(googleDeploy, 'generateArtifactDirectoryName') - .returns(BbPromise.resolve()); - compileFunctionsStub = sinon.stub(googleDeploy, 'compileFunctions') - .returns(BbPromise.resolve()); - mergeServiceResourcesStub = sinon.stub(googleDeploy, 'mergeServiceResources') - .returns(BbPromise.resolve()); uploadArtifactsStub = sinon.stub(googleDeploy, 'uploadArtifacts') .returns(BbPromise.resolve()); updateDeploymentStub = sinon.stub(googleDeploy, 'updateDeployment') @@ -78,46 +65,29 @@ describe('GoogleDeploy', () => { googleDeploy.validate.restore(); googleDeploy.setDefaults.restore(); googleDeploy.setDeploymentBucketName.restore(); - googleDeploy.prepareDeployment.restore(); googleDeploy.createDeployment.restore(); - googleDeploy.generateArtifactDirectoryName.restore(); - googleDeploy.compileFunctions.restore(); - googleDeploy.mergeServiceResources.restore(); googleDeploy.uploadArtifacts.restore(); googleDeploy.updateDeployment.restore(); googleDeploy.cleanupDeploymentBucket.restore(); }); - it('should run "before:deploy:initialize" promise chain', () => googleDeploy - .hooks['before:deploy:initialize']().then(() => { + it('should run "before:deploy:deploy" promise chain', () => googleDeploy + .hooks['before:deploy:deploy']().then(() => { expect(validateStub.calledOnce).toEqual(true); expect(setDefaultsStub.calledAfter(validateStub)).toEqual(true); })); - it('should run "deploy:initialize" promise chain', () => googleDeploy - .hooks['deploy:initialize']().then(() => { - expect(setDeploymentBucketNameStub.calledOnce).toEqual(true); - expect(prepareDeploymentStub.calledAfter(setDeploymentBucketNameStub)).toEqual(true); - })); - - it('it should run "deploy:setupProviderConfiguration" promise chain', () => googleDeploy - .hooks['deploy:setupProviderConfiguration']().then(() => { - expect(createDeploymentStub.calledOnce).toEqual(true); - }), - ); - - it('should run "before:deploy:compileFunctions" promise chain', () => googleDeploy - .hooks['before:deploy:compileFunctions']().then(() => { - expect(generateArtifactDirectoryNameStub.calledOnce).toEqual(true); - expect(compileFunctionsStub.calledAfter(generateArtifactDirectoryNameStub)).toEqual(true); - })); - it('should run "deploy:deploy" promise chain', () => googleDeploy .hooks['deploy:deploy']().then(() => { - expect(mergeServiceResourcesStub.calledOnce).toEqual(true); - expect(uploadArtifactsStub.calledAfter(mergeServiceResourcesStub)).toEqual(true); + expect(setDeploymentBucketNameStub.calledOnce).toEqual(true); + expect(createDeploymentStub.calledAfter(setDeploymentBucketNameStub)).toEqual(true); + expect(uploadArtifactsStub.calledAfter(createDeploymentStub)).toEqual(true); expect(updateDeploymentStub.calledAfter(uploadArtifactsStub)).toEqual(true); - expect(cleanupDeploymentBucketStub.calledAfter(updateDeploymentStub)).toEqual(true); + })); + + it('should run "after:deploy:deploy" promise chain', () => googleDeploy + .hooks['after:deploy:deploy']().then(() => { + expect(cleanupDeploymentBucketStub.calledOnce).toEqual(true); })); }); }); diff --git a/deploy/lib/compileFunctions.js b/deploy/lib/compileFunctions.js deleted file mode 100644 index 54333ad..0000000 --- a/deploy/lib/compileFunctions.js +++ /dev/null @@ -1,120 +0,0 @@ -'use strict'; - -/* eslint no-use-before-define: 0 */ - -const path = require('path'); - -const _ = require('lodash'); -const BbPromise = require('bluebird'); - -module.exports = { - compileFunctions() { - const artifactFilePath = this.serverless.service.package.artifact; - const fileName = artifactFilePath.split(path.sep).pop(); - - this.serverless.service.package - .artifactFilePath = `${this.serverless.service.package.artifactDirectoryName}/${fileName}`; - - this.serverless.service.getAllFunctions().forEach((functionName) => { - const funcObject = this.serverless.service.getFunction(functionName); - - this.serverless.cli - .log(`Compiling function "${functionName}"...`); - - validateHandlerProperty(funcObject, functionName); - validateEventsProperty(funcObject, functionName); - - const funcTemplate = getFunctionTemplate( - funcObject, - this.options.region, - `gs://${ - this.serverless.service.provider.deploymentBucketName - }/${this.serverless.service.package.artifactFilePath}`); - - funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize') - || _.get(this, 'serverless.service.provider.memorySize') - || 256; - funcTemplate.properties.timeout = _.get(funcObject, 'timeout') - || _.get(this, 'serverless.service.provider.timeout') - || '60s'; - - const eventType = Object.keys(funcObject.events[0])[0]; - - if (eventType === 'http') { - const url = funcObject.events[0].http; - - funcTemplate.properties.httpsTrigger = {}; - funcTemplate.properties.httpsTrigger.url = url; - } - if (eventType === 'event') { - const type = funcObject.events[0].event.eventType; - const path = funcObject.events[0].event.path; //eslint-disable-line - const resource = funcObject.events[0].event.resource; - - funcTemplate.properties.eventTrigger = {}; - funcTemplate.properties.eventTrigger.eventType = type; - if (path) funcTemplate.properties.eventTrigger.path = path; - funcTemplate.properties.eventTrigger.resource = resource; - } - - this.serverless.service.provider.compiledConfigurationTemplate.resources.push(funcTemplate); - }); - - return BbPromise.resolve(); - }, -}; - -const validateHandlerProperty = (funcObject, functionName) => { - if (!funcObject.handler) { - const errorMessage = [ - `Missing "handler" property for function "${functionName}".`, - ' Your function needs a "handler".', - ' Please check the docs for more info.', - ].join(''); - throw new Error(errorMessage); - } -}; - -const validateEventsProperty = (funcObject, functionName) => { - if (!funcObject.events || funcObject.events.length === 0) { - const errorMessage = [ - `Missing "events" property for function "${functionName}".`, - ' Your function needs at least one "event".', - ' Please check the docs for more info.', - ].join(''); - throw new Error(errorMessage); - } - - if (funcObject.events.length > 1) { - const errorMessage = [ - `The function "${functionName}" has more than one event.`, - ' Only one event per function is supported.', - ' Please check the docs for more info.', - ].join(''); - throw new Error(errorMessage); - } - - const supportedEvents = ['http', 'event']; - const eventType = Object.keys(funcObject.events[0])[0]; - if (supportedEvents.indexOf(eventType) === -1) { - const errorMessage = [ - `Event type "${eventType}" of function "${functionName}" not supported.`, - ` supported event types are: ${supportedEvents.join(', ')}`, - ].join(''); - throw new Error(errorMessage); - } -}; - -const getFunctionTemplate = (funcObject, region, sourceArchiveUrl) => { //eslint-disable-line - return { - type: 'cloudfunctions.v1beta2.function', - name: funcObject.name, - properties: { - location: region, - availableMemoryMb: 256, - timeout: '60s', - function: funcObject.handler, - sourceArchiveUrl, - }, - }; -}; diff --git a/deploy/lib/compileFunctions.test.js b/deploy/lib/compileFunctions.test.js deleted file mode 100644 index 7368174..0000000 --- a/deploy/lib/compileFunctions.test.js +++ /dev/null @@ -1,332 +0,0 @@ -'use strict'; - -const sinon = require('sinon'); - -const GoogleProvider = require('../../provider/googleProvider'); -const GoogleDeploy = require('../googleDeploy'); -const Serverless = require('../../test/serverless'); - -describe('CompileFunctions', () => { - let serverless; - let googleDeploy; - let consoleLogStub; - - beforeEach(() => { - serverless = new Serverless(); - serverless.service.service = 'my-service'; - serverless.service.package = { - artifact: 'artifact.zip', - artifactDirectoryName: 'some-path', - }; - serverless.service.provider = { - compiledConfigurationTemplate: { - resources: [], - }, - deploymentBucketName: 'sls-my-service-dev-12345678', - }; - serverless.setProvider('google', new GoogleProvider(serverless)); - const options = { - stage: 'dev', - region: 'us-central1', - }; - googleDeploy = new GoogleDeploy(serverless, options); - consoleLogStub = sinon.stub(googleDeploy.serverless.cli, 'log').returns(); - }); - - afterEach(() => { - googleDeploy.serverless.cli.log.restore(); - }); - - describe('#compileFunctions()', () => { - it('should throw an error if the function has no handler property', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: null, - }, - }; - - expect(() => googleDeploy.compileFunctions()).toThrow(Error); - }); - - it('should throw an error if the function has no events property', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: null, - }, - }; - - expect(() => googleDeploy.compileFunctions()).toThrow(Error); - }); - - it('should throw an error if the function has 0 events', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [], - }, - }; - - expect(() => googleDeploy.compileFunctions()).toThrow(Error); - }); - - it('should throw an error if the function has more than 1 event', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { http: 'event1' }, - { http: 'event2' }, - ], - }, - }; - - expect(() => googleDeploy.compileFunctions()).toThrow(Error); - }); - - it('should throw an error if the functions event is not supported', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { invalidEvent: 'event1' }, - ], - }, - }; - - expect(() => googleDeploy.compileFunctions()).toThrow(Error); - }); - - it('should set the memory size based on the functions configuration', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - memorySize: 1024, - events: [ - { http: 'foo' }, - ], - }, - }; - - const compiledResources = [{ - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 1024, - timeout: '60s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - httpsTrigger: { - url: 'foo', - }, - }, - }]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.calledOnce).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - - it('should set the memory size based on the provider configuration', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { http: 'foo' }, - ], - }, - }; - googleDeploy.serverless.service.provider.memorySize = 1024; - - const compiledResources = [{ - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 1024, - timeout: '60s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - httpsTrigger: { - url: 'foo', - }, - }, - }]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.calledOnce).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - - it('should set the timout based on the functions configuration', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - timeout: '120s', - events: [ - { http: 'foo' }, - ], - }, - }; - - const compiledResources = [{ - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 256, - timeout: '120s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - httpsTrigger: { - url: 'foo', - }, - }, - }]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.calledOnce).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - - it('should set the timeout based on the provider configuration', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { http: 'foo' }, - ], - }, - }; - googleDeploy.serverless.service.provider.timeout = '120s'; - - const compiledResources = [{ - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 256, - timeout: '120s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - httpsTrigger: { - url: 'foo', - }, - }, - }]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.calledOnce).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - - it('should compile "http" events properly', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { http: 'foo' }, - ], - }, - }; - - const compiledResources = [{ - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 256, - timeout: '60s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - httpsTrigger: { - url: 'foo', - }, - }, - }]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.calledOnce).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - - it('should compile "event" events properly', () => { - googleDeploy.serverless.service.functions = { - func1: { - handler: 'func1', - events: [ - { - event: { - eventType: 'foo', - path: 'some-path', - resource: 'some-resource', - }, - }, - ], - }, - func2: { - handler: 'func2', - events: [ - { - event: { - eventType: 'foo', - resource: 'some-resource', - }, - }, - ], - }, - }; - - const compiledResources = [ - { - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func1', - properties: { - location: 'us-central1', - function: 'func1', - availableMemoryMb: 256, - timeout: '60s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - eventTrigger: { - eventType: 'foo', - path: 'some-path', - resource: 'some-resource', - }, - }, - }, - { - type: 'cloudfunctions.v1beta2.function', - name: 'my-service-dev-func2', - properties: { - location: 'us-central1', - function: 'func2', - availableMemoryMb: 256, - timeout: '60s', - sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', - eventTrigger: { - eventType: 'foo', - resource: 'some-resource', - }, - }, - }, - ]; - - return googleDeploy.compileFunctions().then(() => { - expect(consoleLogStub.called).toEqual(true); - expect(googleDeploy.serverless.service.provider.compiledConfigurationTemplate.resources) - .toEqual(compiledResources); - }); - }); - }); -}); diff --git a/deploy/lib/createDeployment.js b/deploy/lib/createDeployment.js index 9dfa53d..9480e71 100644 --- a/deploy/lib/createDeployment.js +++ b/deploy/lib/createDeployment.js @@ -8,7 +8,6 @@ const BbPromise = require('bluebird'); module.exports = { createDeployment() { return BbPromise.bind(this) - .then(this.writeCreateTemplateToDisk) .then(this.checkForExistingDeployment) .then(this.createIfNotExists); }, @@ -58,14 +57,4 @@ module.exports = { return this.provider.request('deploymentmanager', 'deployments', 'insert', params) .then(() => this.monitorDeployment(deploymentName, 'create', 5000)); }, - - writeCreateTemplateToDisk() { - const filePath = path.join(this.serverless.config.servicePath, - '.serverless', 'configuration-template-create.yml'); - - this.serverless.utils.writeFileSync(filePath, - this.serverless.service.provider.compiledConfigurationTemplate); - - return BbPromise.resolve(); - }, }; diff --git a/deploy/lib/createDeployment.test.js b/deploy/lib/createDeployment.test.js index b99e465..0e863b3 100644 --- a/deploy/lib/createDeployment.test.js +++ b/deploy/lib/createDeployment.test.js @@ -43,13 +43,10 @@ describe('CreateDeployment', () => { }); describe('#createDeployment()', () => { - let writeCreateTemplateToDiskStub; let checkForExistingDeploymentStub; let createIfNotExistsStub; beforeEach(() => { - writeCreateTemplateToDiskStub = sinon.stub(googleDeploy, 'writeCreateTemplateToDisk') - .returns(BbPromise.resolve()); checkForExistingDeploymentStub = sinon.stub(googleDeploy, 'checkForExistingDeployment') .returns(BbPromise.resolve()); createIfNotExistsStub = sinon.stub(googleDeploy, 'createIfNotExists') @@ -57,15 +54,13 @@ describe('CreateDeployment', () => { }); afterEach(() => { - googleDeploy.writeCreateTemplateToDisk.restore(); googleDeploy.checkForExistingDeployment.restore(); googleDeploy.createIfNotExists.restore(); }); it('should run promise chain', () => googleDeploy .createDeployment().then(() => { - expect(writeCreateTemplateToDiskStub.calledOnce).toEqual(true); - expect(checkForExistingDeploymentStub.calledAfter(writeCreateTemplateToDiskStub)); + expect(checkForExistingDeploymentStub.calledOnce).toEqual(true); expect(createIfNotExistsStub.calledAfter(checkForExistingDeploymentStub)); }), ); @@ -185,36 +180,4 @@ describe('CreateDeployment', () => { }); }); }); - - describe('#writeCreateTemplateToDisk()', () => { - let writeFileSyncStub; - - beforeEach(() => { - writeFileSyncStub = sinon.stub(serverless.utils, 'writeFileSync'); - }); - - afterEach(() => { - serverless.utils.writeFileSync.restore(); - }); - - it('should write the create deployment template to disk', () => { - const compiledConfiguration = { - compiledConfigurationTemplate: { - resources: [ - { someResource: 'foo' }, - ], - }, - }; - serverless.service.provider = { - compiledConfigurationTemplate: compiledConfiguration, - }; - - return googleDeploy.writeCreateTemplateToDisk().then(() => { - expect(writeFileSyncStub.calledWithExactly( - configurationTemplateCreateFilePath, - compiledConfiguration, - )).toEqual(true); - }); - }); - }); }); diff --git a/deploy/lib/generateArtifactDirectoryName.js b/deploy/lib/generateArtifactDirectoryName.js deleted file mode 100644 index f99075c..0000000 --- a/deploy/lib/generateArtifactDirectoryName.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -const BbPromise = require('bluebird'); - -module.exports = { - generateArtifactDirectoryName() { - const date = new Date(); - const serviceWithStage = `${this.serverless.service.service}/${this.options.stage}`; - const dateString = `${date.getTime().toString()}-${date.toISOString()}`; - - this.serverless.service.package - .artifactDirectoryName = `serverless/${serviceWithStage}/${dateString}`; - - return BbPromise.resolve(); - }, -}; diff --git a/deploy/lib/generateArtifactDirectoryName.test.js b/deploy/lib/generateArtifactDirectoryName.test.js deleted file mode 100644 index 5a4ac5f..0000000 --- a/deploy/lib/generateArtifactDirectoryName.test.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const GoogleProvider = require('../../provider/googleProvider'); -const GoogleDeploy = require('../googleDeploy'); -const Serverless = require('../../test/serverless'); - -describe('GenerateArtifactDirectoryName', () => { - let serverless; - let googleDeploy; - - beforeEach(() => { - serverless = new Serverless(); - serverless.service.service = 'my-service'; - serverless.service.package = { - artifactDirectoryName: null, - }; - serverless.setProvider('google', new GoogleProvider(serverless)); - const options = { - stage: 'dev', - region: 'us-central1', - }; - googleDeploy = new GoogleDeploy(serverless, options); - }); - - it('should create a valid artifact directory name', () => { - const expectedRegex = new RegExp('serverless/my-service/dev/.*'); - - return googleDeploy.generateArtifactDirectoryName().then(() => { - expect(serverless.service.package.artifactDirectoryName) - .toMatch(expectedRegex); - }); - }); -}); diff --git a/deploy/lib/mergeServiceResources.js b/deploy/lib/mergeServiceResources.js deleted file mode 100644 index 7b73940..0000000 --- a/deploy/lib/mergeServiceResources.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; - -/* eslint no-use-before-define: 0 */ - -const _ = require('lodash'); -const BbPromise = require('bluebird'); - -module.exports = { - mergeServiceResources() { - const resources = this.serverless.service.resources; - - if ((typeof resources === 'undefined') || _.isEmpty(resources)) return BbPromise.resolve(); - - _.mergeWith( - this.serverless.service.provider.compiledConfigurationTemplate, - resources, - mergeCustomizer); - - return BbPromise.resolve(); - }, -}; - -const mergeCustomizer = (objValue, srcValue) => { - if (_.isArray(objValue)) return objValue.concat(srcValue); - return objValue; -}; diff --git a/deploy/lib/mergeServiceResources.test.js b/deploy/lib/mergeServiceResources.test.js deleted file mode 100644 index ea2e372..0000000 --- a/deploy/lib/mergeServiceResources.test.js +++ /dev/null @@ -1,101 +0,0 @@ -'use stict'; - -const GoogleProvider = require('../../provider/googleProvider'); -const GoogleDeploy = require('../googleDeploy'); -const Serverless = require('../../test/serverless'); - -describe('MergeServiceResources', () => { - let serverless; - let googleDeploy; - - beforeEach(() => { - serverless = new Serverless(); - serverless.service.service = 'my-service'; - serverless.service.provider = { - compiledConfigurationTemplate: {}, - }; - serverless.setProvider('google', new GoogleProvider(serverless)); - const options = { - stage: 'dev', - region: 'us-central1', - }; - googleDeploy = new GoogleDeploy(serverless, options); - }); - - it('should resolve if service resources are not defined', () => googleDeploy - .mergeServiceResources().then(() => { - expect(serverless.service.provider - .compiledConfigurationTemplate).toEqual({}); - })); - - it('should resolve if service resources is empty', () => { - serverless.service.resources = {}; - - return googleDeploy.mergeServiceResources().then(() => { - expect(serverless.service.provider - .compiledConfigurationTemplate).toEqual({}); - }); - }); - - it('should merge all the resources if provided', () => { - serverless.service.provider.compiledConfigurationTemplate = { - resources: [ - { - name: 'resource1', - type: 'type1', - properties: { - property1: 'value1', - }, - }, - ], - }; - - serverless.service.resources = { - resources: [ - { - name: 'resource2', - type: 'type2', - properties: { - property1: 'value1', - }, - }, - ], - imports: [ - { - path: 'path/to/template.jinja', - name: 'my-template', - }, - ], - }; - - const expectedResult = { - resources: [ - { - name: 'resource1', - type: 'type1', - properties: { - property1: 'value1', - }, - }, - { - name: 'resource2', - type: 'type2', - properties: { - property1: 'value1', - }, - }, - ], - imports: [ - { - path: 'path/to/template.jinja', - name: 'my-template', - }, - ], - }; - - return googleDeploy.mergeServiceResources().then(() => { - expect(serverless.service.provider.compiledConfigurationTemplate) - .toEqual(expectedResult); - }); - }); -}); diff --git a/deploy/lib/prepareDeployment.js b/deploy/lib/prepareDeployment.js deleted file mode 100644 index 6ab1259..0000000 --- a/deploy/lib/prepareDeployment.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -/* eslint no-use-before-define: 0 */ - -const path = require('path'); - -const _ = require('lodash'); -const BbPromise = require('bluebird'); - -module.exports = { - prepareDeployment() { - let deploymentTemplate = this.serverless.service.provider.compiledConfigurationTemplate; - - deploymentTemplate = this.serverless.utils.readFileSync( - path.join( - __dirname, - '..', - 'templates', - 'core-configuration-template.yml')); - - const bucket = deploymentTemplate.resources.find(findDeploymentBucket); - - const name = this.serverless.service.provider.deploymentBucketName; - const updatedBucket = updateBucketName(bucket, name); - - const bucketIndex = deploymentTemplate.resources.findIndex(findDeploymentBucket); - - deploymentTemplate.resources[bucketIndex] = updatedBucket; - - this.serverless.service.provider.compiledConfigurationTemplate = deploymentTemplate; - - return BbPromise.resolve(); - }, -}; - -const updateBucketName = (bucket, name) => { - const newBucket = _.cloneDeep(bucket); - newBucket.name = name; - return newBucket; -}; - -const findDeploymentBucket = (resource) => { - const type = 'storage.v1.bucket'; - const name = 'will-be-replaced-by-serverless'; - - return resource.type === type && resource.name === name; -}; diff --git a/deploy/lib/prepareDeployment.test.js b/deploy/lib/prepareDeployment.test.js deleted file mode 100644 index 177e6b4..0000000 --- a/deploy/lib/prepareDeployment.test.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -const sinon = require('sinon'); - -const GoogleProvider = require('../../provider/googleProvider'); -const GoogleDeploy = require('../googleDeploy'); -const Serverless = require('../../test/serverless'); - -describe('PrepareDeployment', () => { - let coreResources; - let serverless; - let googleDeploy; - - beforeEach(() => { - coreResources = { - resources: [ - { - type: 'storage.v1.bucket', - name: 'will-be-replaced-by-serverless', - }, - ], - }; - serverless = new Serverless(); - serverless.service.service = 'my-service'; - serverless.service.provider = { - compiledConfigurationTemplate: coreResources, - deploymentBucketName: 'sls-my-service-dev-12345678', - }; - serverless.setProvider('google', new GoogleProvider(serverless)); - const options = { - stage: 'dev', - region: 'us-central1', - }; - googleDeploy = new GoogleDeploy(serverless, options); - }); - - describe('#prepareDeployment()', () => { - let readFileSyncStub; - - beforeEach(() => { - readFileSyncStub = sinon.stub(serverless.utils, 'readFileSync').returns(coreResources); - }); - - afterEach(() => { - serverless.utils.readFileSync.restore(); - }); - - it('should load the core configuration template into the serverless instance', () => { - const expectedCompiledConfiguration = { - resources: [ - { - type: 'storage.v1.bucket', - name: 'sls-my-service-dev-12345678', - }, - ], - }; - - return googleDeploy.prepareDeployment().then(() => { - expect(readFileSyncStub.calledOnce).toEqual(true); - expect(serverless.service.provider - .compiledConfigurationTemplate).toEqual(expectedCompiledConfiguration); - }); - }); - }); -}); diff --git a/deploy/lib/updateDeployment.js b/deploy/lib/updateDeployment.js index 41c4e9f..d8f0aef 100644 --- a/deploy/lib/updateDeployment.js +++ b/deploy/lib/updateDeployment.js @@ -8,7 +8,6 @@ const BbPromise = require('bluebird'); module.exports = { updateDeployment() { return BbPromise.bind(this) - .then(this.writeUpdateTemplateToDisk) .then(this.getDeployment) .then(this.update); }, @@ -54,14 +53,4 @@ module.exports = { return this.provider.request('deploymentmanager', 'deployments', 'update', params) .then(() => this.monitorDeployment(deploymentName, 'update', 5000)); }, - - writeUpdateTemplateToDisk() { - const filePath = path.join(this.serverless.config.servicePath, - '.serverless', 'configuration-template-update.yml'); - - this.serverless.utils.writeFileSync(filePath, - this.serverless.service.provider.compiledConfigurationTemplate); - - return BbPromise.resolve(); - }, }; diff --git a/deploy/lib/updateDeployment.test.js b/deploy/lib/updateDeployment.test.js index 0f6a1fa..3d61084 100644 --- a/deploy/lib/updateDeployment.test.js +++ b/deploy/lib/updateDeployment.test.js @@ -43,13 +43,10 @@ describe('UpdateDeployment', () => { }); describe('#updateDeployment()', () => { - let writeUpdateTemplateToDiskStub; let getDeploymentStub; let updateStub; beforeEach(() => { - writeUpdateTemplateToDiskStub = sinon.stub(googleDeploy, 'writeUpdateTemplateToDisk') - .returns(BbPromise.resolve()); getDeploymentStub = sinon.stub(googleDeploy, 'getDeployment') .returns(BbPromise.resolve()); updateStub = sinon.stub(googleDeploy, 'update') @@ -57,15 +54,13 @@ describe('UpdateDeployment', () => { }); afterEach(() => { - googleDeploy.writeUpdateTemplateToDisk.restore(); googleDeploy.getDeployment.restore(); googleDeploy.update.restore(); }); it('should run promise chain', () => googleDeploy .updateDeployment().then(() => { - expect(writeUpdateTemplateToDiskStub.calledOnce).toEqual(true); - expect(getDeploymentStub.calledAfter(writeUpdateTemplateToDiskStub)); + expect(getDeploymentStub.calledOnce).toEqual(true); expect(updateStub.calledAfter(getDeploymentStub)); }), ); @@ -167,36 +162,4 @@ describe('UpdateDeployment', () => { }); }); }); - - describe('#writeUpdateTemplateToDisk()', () => { - let writeFileSyncStub; - - beforeEach(() => { - writeFileSyncStub = sinon.stub(serverless.utils, 'writeFileSync'); - }); - - afterEach(() => { - serverless.utils.writeFileSync.restore(); - }); - - it('should write the update deployment template to disk', () => { - const compiledConfiguration = { - compiledConfigurationTemplate: { - resources: [ - { someResource: 'foo' }, - ], - }, - }; - serverless.service.provider = { - compiledConfigurationTemplate: compiledConfiguration, - }; - - return googleDeploy.writeUpdateTemplateToDisk().then(() => { - expect(writeFileSyncStub.calledWithExactly( - configurationTemplateUpdateFilePath, - compiledConfiguration, - )).toEqual(true); - }); - }); - }); }); diff --git a/deploy/templates/core-configuration-template.yml b/deploy/templates/core-configuration-template.yml deleted file mode 100644 index 12f779b..0000000 --- a/deploy/templates/core-configuration-template.yml +++ /dev/null @@ -1,3 +0,0 @@ -resources: -- type: storage.v1.bucket - name: will-be-replaced-by-serverless