From 8fad3cec2fa796917ba6f300bfbdecede2f50f95 Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Mon, 25 Jul 2016 15:42:58 +0200 Subject: [PATCH 1/4] Removed unneeded outputs and the corresponding tests. Added postCreate function for easy hook later. --- .../compile/events/apiGateway/lib/methods.js | 21 +++++---- .../events/apiGateway/tests/methods.js | 26 +++++----- lib/plugins/aws/deploy/lib/core-cf.json | 9 ---- lib/plugins/aws/deploy/lib/createStack.js | 27 ++--------- lib/plugins/aws/deploy/tests/createStack.js | 47 ++----------------- 5 files changed, 33 insertions(+), 97 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js index 5c70d5827..a2a16093d 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js @@ -33,15 +33,6 @@ module.exports = { method.substr(1).toLowerCase(); const extractedResourceId = resourceLogicalId.match(/\d+$/)[0]; - const serviceName = this.serverless.service.service; - const awsAccountNumber = this.serverless.service - .getVariables(this.options.stage, this.options.region).iamRoleArnLambda - .match(/(.*):(.*):(.*):(.*):(.*):role\/.*/)[5]; - - const lambdaUri = `arn:aws:apigateway:${ - this.options.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${ - this.options.region}:${awsAccountNumber}:function:${ - serviceName}-${this.options.stage}-${functionName}/invocations`; // universal velocity template // provides `{body, method, headers, query, path, identity, stageVariables} = event` @@ -94,7 +85,17 @@ module.exports = { "Integration" : { "IntegrationHttpMethod" : "POST", "Type" : "AWS", - "Uri" : "${lambdaUri}", + "Uri" : { + "Fn::Join": [ "", + [ + "arn:aws:apigateway:", + {"Ref" : "AWS::Region"}, + ":lambda:path/2015-03-31/functions/", + {"Fn::GetAtt" : ["${functionName}", "Arn"]}, + "/invocations" + ] + ] + }, "RequestTemplates" : { "application/json" : ${JSON.stringify(DEFAULT_JSON_REQUEST_TEMPLATE)} }, diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index eb14853c7..48f2a74df 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -162,21 +162,25 @@ describe('#compileMethods()', () => { }); it('should set the correct lambdaUri', () => { - const lambdaUri = `arn:aws:apigateway:${ - awsCompileApigEvents.options.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${ - awsCompileApigEvents.options.region}:12345678:function:${ - awsCompileApigEvents.serverless.service.service}-${ - awsCompileApigEvents.options.stage}-first/invocations`; + const lambdaUriObject = { + 'Fn::Join': ['', [ + 'arn:aws:apigateway:', + { Ref: 'AWS::Region' }, + ':lambda:path/2015-03-31/functions/', + { 'Fn::GetAtt': ['first', 'Arn'] }, + '/invocations' // eslint-disable-line comma-dangle + ]] // eslint-disable-line comma-dangle + }; return awsCompileApigEvents.compileMethods().then(() => { expect( - awsCompileApigEvents.serverless.service.resources.Resources.PostMethodApigEvent0.Properties - .Integration.Uri - ).to.equal(lambdaUri); + JSON.stringify(awsCompileApigEvents.serverless.service.resources + .Resources.PostMethodApigEvent0.Properties.Integration.Uri + )).to.equal(JSON.stringify(lambdaUriObject)); expect( - awsCompileApigEvents.serverless.service.resources.Resources.GetMethodApigEvent1.Properties - .Integration.Uri - ).to.equal(lambdaUri); + JSON.stringify(awsCompileApigEvents.serverless.service.resources + .Resources.GetMethodApigEvent1.Properties.Integration.Uri + )).to.equal(JSON.stringify(lambdaUriObject)); }); }); }); diff --git a/lib/plugins/aws/deploy/lib/core-cf.json b/lib/plugins/aws/deploy/lib/core-cf.json index b3d5a10fe..04440c4d5 100644 --- a/lib/plugins/aws/deploy/lib/core-cf.json +++ b/lib/plugins/aws/deploy/lib/core-cf.json @@ -54,15 +54,6 @@ } }, "Outputs": { - "IamRoleArnLambda": { - "Description": "ARN of the lambda IAM role", - "Value": { - "Fn::GetAtt": [ - "IamRoleLambda", - "Arn" - ] - } - }, "ServerlessDeploymentBucketName": { "Value": { "Ref": "ServerlessDeploymentBucket" diff --git a/lib/plugins/aws/deploy/lib/createStack.js b/lib/plugins/aws/deploy/lib/createStack.js index a5448bb20..adb446d77 100644 --- a/lib/plugins/aws/deploy/lib/createStack.js +++ b/lib/plugins/aws/deploy/lib/createStack.js @@ -1,8 +1,6 @@ 'use strict'; const BbPromise = require('bluebird'); -const path = require('path'); -const lowerFirst = require('lodash').lowerFirst; const async = require('async'); module.exports = { @@ -73,28 +71,9 @@ module.exports = { }); }, - addOutputVars(cfData) { + postCreate() { this.serverless.cli.log('Stack successfully created.'); - - const serverlessEnvYamlPath = path - .join(this.serverless.config.servicePath, 'serverless.env.yaml'); - return this.serverless.yamlParser.parse(serverlessEnvYamlPath).then(parsedServerlessEnvYaml => { - const serverlessEnvYaml = parsedServerlessEnvYaml; - cfData.Outputs.forEach((output) => { - const varName = lowerFirst(output.OutputKey); - - // add vars to memory - const regionVars = this.serverless.service - .getVariables(this.options.stage, this.options.region); - regionVars[varName] = output.OutputValue; - - // add vars to file system - serverlessEnvYaml.stages[this.options.stage] - .regions[this.options.region].vars = regionVars; - }); - this.serverless.utils.writeFileSync(serverlessEnvYamlPath, serverlessEnvYaml); - return BbPromise.resolve(); - }); + return BbPromise.resolve(); }, createStack() { @@ -111,7 +90,7 @@ module.exports = { return BbPromise.bind(this) .then(this.create) .then(this.monitorCreate) - .then(this.addOutputVars); + .then(this.postCreate); } throw new this.serverless.classes.Error(e); diff --git a/lib/plugins/aws/deploy/tests/createStack.js b/lib/plugins/aws/deploy/tests/createStack.js index 0cec52b8e..05bb5f238 100644 --- a/lib/plugins/aws/deploy/tests/createStack.js +++ b/lib/plugins/aws/deploy/tests/createStack.js @@ -131,45 +131,6 @@ describe('createStack', () => { }); }); - describe('#addOutputVars()', () => { - it('should addOutputVariables to memory and serverless.env.yaml', () => { - const cfDataMock = { - Outputs: [ - { - OutputKey: 'IamRoleArnLambda', - OutputValue: 'someValue', - }, - ], - }; - - awsDeploy.serverless.service.environment = { - vars: {}, - stages: { - dev: { - vars: {}, - regions: { - 'us-east-1': { - vars: {}, - }, - }, - }, - }, - }; - - return awsDeploy.addOutputVars(cfDataMock) - .then(() => awsDeploy.serverless.yamlParser.parse(serverlessEnvYamlPath)) - .then((yaml) => { - // assert var added to memory - expect(awsDeploy.serverless.service.environment.stages.dev - .regions['us-east-1'].vars.iamRoleArnLambda).to.be.equal('someValue'); - - // assert var added to file - expect(yaml.stages.dev - .regions['us-east-1'].vars.iamRoleArnLambda).to.be.equal('someValue'); - }); - }); - }); - describe('#createStack()', () => { it('should resolve if stack already created', () => { const createStub = sinon @@ -205,17 +166,17 @@ describe('createStack', () => { .stub(awsDeploy, 'create').returns(BbPromise.resolve()); const monitorStub = sinon .stub(awsDeploy, 'monitorCreate').returns(BbPromise.resolve()); - const addOutputVarsStub = sinon - .stub(awsDeploy, 'addOutputVars').returns(BbPromise.resolve()); + const postCreateStub = sinon + .stub(awsDeploy, 'postCreate').returns(BbPromise.resolve()); return awsDeploy.createStack().then(() => { expect(createStub.calledOnce).to.be.equal(true); expect(monitorStub.calledAfter(createStub)).to.be.equal(true); - expect(addOutputVarsStub.calledAfter(monitorStub)).to.be.equal(true); + expect(postCreateStub.calledAfter(monitorStub)).to.be.equal(true); awsDeploy.create.restore(); awsDeploy.monitorCreate.restore(); - awsDeploy.addOutputVars.restore(); + awsDeploy.postCreate.restore(); awsDeploy.sdk.request.restore(); }); }); From 3d8154c47bdcee7b2be774cf0dfb90ab3d43a43d Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Wed, 27 Jul 2016 15:04:35 +0200 Subject: [PATCH 2/4] cleanup json notation, make eslint happy --- .../compile/events/apiGateway/tests/methods.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index 48f2a74df..ea30263a9 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -163,13 +163,13 @@ describe('#compileMethods()', () => { it('should set the correct lambdaUri', () => { const lambdaUriObject = { - 'Fn::Join': ['', [ - 'arn:aws:apigateway:', - { Ref: 'AWS::Region' }, - ':lambda:path/2015-03-31/functions/', - { 'Fn::GetAtt': ['first', 'Arn'] }, - '/invocations' // eslint-disable-line comma-dangle - ]] // eslint-disable-line comma-dangle + 'Fn::Join': [ + '', [ + 'arn:aws:apigateway:', { Ref: 'AWS::Region' }, + ':lambda:path/2015-03-31/functions/', { 'Fn::GetAtt': ['first', 'Arn'] }, + '/invocations', + ], + ], }; return awsCompileApigEvents.compileMethods().then(() => { From 5ea59d4ed601d4ae0dfe723c5954b9534c2c0c9b Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Mon, 25 Jul 2016 15:42:58 +0200 Subject: [PATCH 3/4] Removed unneeded outputs and the corresponding tests. Added postCreate function for easy hook later. --- .../compile/events/apiGateway/lib/methods.js | 21 +++++---- .../events/apiGateway/tests/methods.js | 26 +++++----- lib/plugins/aws/deploy/lib/core-cf.json | 9 ---- lib/plugins/aws/deploy/lib/createStack.js | 34 ++------------ lib/plugins/aws/deploy/tests/createStack.js | 47 ++----------------- 5 files changed, 33 insertions(+), 104 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js index 5c70d5827..a2a16093d 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js @@ -33,15 +33,6 @@ module.exports = { method.substr(1).toLowerCase(); const extractedResourceId = resourceLogicalId.match(/\d+$/)[0]; - const serviceName = this.serverless.service.service; - const awsAccountNumber = this.serverless.service - .getVariables(this.options.stage, this.options.region).iamRoleArnLambda - .match(/(.*):(.*):(.*):(.*):(.*):role\/.*/)[5]; - - const lambdaUri = `arn:aws:apigateway:${ - this.options.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${ - this.options.region}:${awsAccountNumber}:function:${ - serviceName}-${this.options.stage}-${functionName}/invocations`; // universal velocity template // provides `{body, method, headers, query, path, identity, stageVariables} = event` @@ -94,7 +85,17 @@ module.exports = { "Integration" : { "IntegrationHttpMethod" : "POST", "Type" : "AWS", - "Uri" : "${lambdaUri}", + "Uri" : { + "Fn::Join": [ "", + [ + "arn:aws:apigateway:", + {"Ref" : "AWS::Region"}, + ":lambda:path/2015-03-31/functions/", + {"Fn::GetAtt" : ["${functionName}", "Arn"]}, + "/invocations" + ] + ] + }, "RequestTemplates" : { "application/json" : ${JSON.stringify(DEFAULT_JSON_REQUEST_TEMPLATE)} }, diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index eb14853c7..48f2a74df 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -162,21 +162,25 @@ describe('#compileMethods()', () => { }); it('should set the correct lambdaUri', () => { - const lambdaUri = `arn:aws:apigateway:${ - awsCompileApigEvents.options.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${ - awsCompileApigEvents.options.region}:12345678:function:${ - awsCompileApigEvents.serverless.service.service}-${ - awsCompileApigEvents.options.stage}-first/invocations`; + const lambdaUriObject = { + 'Fn::Join': ['', [ + 'arn:aws:apigateway:', + { Ref: 'AWS::Region' }, + ':lambda:path/2015-03-31/functions/', + { 'Fn::GetAtt': ['first', 'Arn'] }, + '/invocations' // eslint-disable-line comma-dangle + ]] // eslint-disable-line comma-dangle + }; return awsCompileApigEvents.compileMethods().then(() => { expect( - awsCompileApigEvents.serverless.service.resources.Resources.PostMethodApigEvent0.Properties - .Integration.Uri - ).to.equal(lambdaUri); + JSON.stringify(awsCompileApigEvents.serverless.service.resources + .Resources.PostMethodApigEvent0.Properties.Integration.Uri + )).to.equal(JSON.stringify(lambdaUriObject)); expect( - awsCompileApigEvents.serverless.service.resources.Resources.GetMethodApigEvent1.Properties - .Integration.Uri - ).to.equal(lambdaUri); + JSON.stringify(awsCompileApigEvents.serverless.service.resources + .Resources.GetMethodApigEvent1.Properties.Integration.Uri + )).to.equal(JSON.stringify(lambdaUriObject)); }); }); }); diff --git a/lib/plugins/aws/deploy/lib/core-cf.json b/lib/plugins/aws/deploy/lib/core-cf.json index b3d5a10fe..04440c4d5 100644 --- a/lib/plugins/aws/deploy/lib/core-cf.json +++ b/lib/plugins/aws/deploy/lib/core-cf.json @@ -54,15 +54,6 @@ } }, "Outputs": { - "IamRoleArnLambda": { - "Description": "ARN of the lambda IAM role", - "Value": { - "Fn::GetAtt": [ - "IamRoleLambda", - "Arn" - ] - } - }, "ServerlessDeploymentBucketName": { "Value": { "Ref": "ServerlessDeploymentBucket" diff --git a/lib/plugins/aws/deploy/lib/createStack.js b/lib/plugins/aws/deploy/lib/createStack.js index 276181440..adb446d77 100644 --- a/lib/plugins/aws/deploy/lib/createStack.js +++ b/lib/plugins/aws/deploy/lib/createStack.js @@ -1,8 +1,6 @@ 'use strict'; const BbPromise = require('bluebird'); -const path = require('path'); -const lowerFirst = require('lodash').lowerFirst; const async = require('async'); module.exports = { @@ -73,35 +71,9 @@ module.exports = { }); }, - addOutputVars(cfData) { + postCreate() { this.serverless.cli.log('Stack successfully created.'); - - let serverlessEnvYmlPath = path - .join(this.serverless.config.servicePath, 'serverless.env.yml'); - - // change to serverless.env.yaml if the file could not be found - if (!this.serverless.utils.fileExistsSync(serverlessEnvYmlPath)) { - serverlessEnvYmlPath = path - .join(this.serverless.config.servicePath, 'serverless.env.yaml'); - } - - return this.serverless.yamlParser.parse(serverlessEnvYmlPath).then(parsedServerlessEnvYml => { - const serverlessEnvYml = parsedServerlessEnvYml; - cfData.Outputs.forEach((output) => { - const varName = lowerFirst(output.OutputKey); - - // add vars to memory - const regionVars = this.serverless.service - .getVariables(this.options.stage, this.options.region); - regionVars[varName] = output.OutputValue; - - // add vars to file system - serverlessEnvYml.stages[this.options.stage] - .regions[this.options.region].vars = regionVars; - }); - this.serverless.utils.writeFileSync(serverlessEnvYmlPath, serverlessEnvYml); - return BbPromise.resolve(); - }); + return BbPromise.resolve(); }, createStack() { @@ -118,7 +90,7 @@ module.exports = { return BbPromise.bind(this) .then(this.create) .then(this.monitorCreate) - .then(this.addOutputVars); + .then(this.postCreate); } throw new this.serverless.classes.Error(e); diff --git a/lib/plugins/aws/deploy/tests/createStack.js b/lib/plugins/aws/deploy/tests/createStack.js index 86567c0d9..d5c468b2b 100644 --- a/lib/plugins/aws/deploy/tests/createStack.js +++ b/lib/plugins/aws/deploy/tests/createStack.js @@ -131,45 +131,6 @@ describe('createStack', () => { }); }); - describe('#addOutputVars()', () => { - it('should addOutputVariables to memory and serverless.env.yml', () => { - const cfDataMock = { - Outputs: [ - { - OutputKey: 'IamRoleArnLambda', - OutputValue: 'someValue', - }, - ], - }; - - awsDeploy.serverless.service.environment = { - vars: {}, - stages: { - dev: { - vars: {}, - regions: { - 'us-east-1': { - vars: {}, - }, - }, - }, - }, - }; - - return awsDeploy.addOutputVars(cfDataMock) - .then(() => awsDeploy.serverless.yamlParser.parse(serverlessEnvYmlPath)) - .then((yaml) => { - // assert var added to memory - expect(awsDeploy.serverless.service.environment.stages.dev - .regions['us-east-1'].vars.iamRoleArnLambda).to.be.equal('someValue'); - - // assert var added to file - expect(yaml.stages.dev - .regions['us-east-1'].vars.iamRoleArnLambda).to.be.equal('someValue'); - }); - }); - }); - describe('#createStack()', () => { it('should resolve if stack already created', () => { const createStub = sinon @@ -205,17 +166,17 @@ describe('createStack', () => { .stub(awsDeploy, 'create').returns(BbPromise.resolve()); const monitorStub = sinon .stub(awsDeploy, 'monitorCreate').returns(BbPromise.resolve()); - const addOutputVarsStub = sinon - .stub(awsDeploy, 'addOutputVars').returns(BbPromise.resolve()); + const postCreateStub = sinon + .stub(awsDeploy, 'postCreate').returns(BbPromise.resolve()); return awsDeploy.createStack().then(() => { expect(createStub.calledOnce).to.be.equal(true); expect(monitorStub.calledAfter(createStub)).to.be.equal(true); - expect(addOutputVarsStub.calledAfter(monitorStub)).to.be.equal(true); + expect(postCreateStub.calledAfter(monitorStub)).to.be.equal(true); awsDeploy.create.restore(); awsDeploy.monitorCreate.restore(); - awsDeploy.addOutputVars.restore(); + awsDeploy.postCreate.restore(); awsDeploy.sdk.request.restore(); }); }); From c98d8d81bf822b3390cffe1815401fc136f9b9c1 Mon Sep 17 00:00:00 2001 From: Sander van de Graaf Date: Wed, 27 Jul 2016 15:04:35 +0200 Subject: [PATCH 4/4] cleanup json notation, make eslint happy --- .../compile/events/apiGateway/tests/methods.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index 48f2a74df..ea30263a9 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -163,13 +163,13 @@ describe('#compileMethods()', () => { it('should set the correct lambdaUri', () => { const lambdaUriObject = { - 'Fn::Join': ['', [ - 'arn:aws:apigateway:', - { Ref: 'AWS::Region' }, - ':lambda:path/2015-03-31/functions/', - { 'Fn::GetAtt': ['first', 'Arn'] }, - '/invocations' // eslint-disable-line comma-dangle - ]] // eslint-disable-line comma-dangle + 'Fn::Join': [ + '', [ + 'arn:aws:apigateway:', { Ref: 'AWS::Region' }, + ':lambda:path/2015-03-31/functions/', { 'Fn::GetAtt': ['first', 'Arn'] }, + '/invocations', + ], + ], }; return awsCompileApigEvents.compileMethods().then(() => {