From fe5edf1a2b2bc12efbfde63c574f424564ba51aa Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Tue, 14 Jun 2016 14:31:04 +0200 Subject: [PATCH] Add compileMethods functionality --- .../awsCompileApigEvents.js | 6 +- .../awsCompileApigEvents/lib/compileMethod.js | 10 --- .../lib/compileMethods.js | 48 +++++++++++ lib/plugins/awsCompileApigEvents/tests/all.js | 1 + .../tests/compileMethods.js | 85 +++++++++++++++++++ 5 files changed, 137 insertions(+), 13 deletions(-) delete mode 100644 lib/plugins/awsCompileApigEvents/lib/compileMethod.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileMethods.js create mode 100644 lib/plugins/awsCompileApigEvents/tests/compileMethods.js diff --git a/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js index c7033c6e7..4dd23d77a 100644 --- a/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js +++ b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js @@ -7,7 +7,7 @@ const compileRestApi = require('./lib/compileRestApi'); const compileDeployment = require('./lib/compileDeployment'); const compileStage = require('./lib/compileStage'); const compileResources = require('./lib/compileResources'); -const compileMethod = require('./lib/compileMethod'); +const compileMethods = require('./lib/compileMethods'); const compilePermission = require('./lib/compilePermission'); class AwsCompileApigEvents { @@ -22,7 +22,7 @@ class AwsCompileApigEvents { compileDeployment, compileStage, compileResources, - compileMethod, + compileMethods, compilePermission ); @@ -33,7 +33,7 @@ class AwsCompileApigEvents { .then(this.compileDeployment) .then(this.compileStage) .then(this.compileResources) - .then(this.compileMethod) + .then(this.compileMethods) .then(this.compilePermission), }; } diff --git a/lib/plugins/awsCompileApigEvents/lib/compileMethod.js b/lib/plugins/awsCompileApigEvents/lib/compileMethod.js deleted file mode 100644 index 72218f0c9..000000000 --- a/lib/plugins/awsCompileApigEvents/lib/compileMethod.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -const BbPromise = require('bluebird'); - -module.exports = { - compileMethod() { - - return BbPromise.resolve(); - }, -}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileMethods.js b/lib/plugins/awsCompileApigEvents/lib/compileMethods.js new file mode 100644 index 000000000..98e03615d --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileMethods.js @@ -0,0 +1,48 @@ +'use strict'; + +const BbPromise = require('bluebird'); +const forEach = require('lodash').forEach; +const merge = require('lodash').merge; + +module.exports = { + compileMethods() { + let methodIndex = 0; + + forEach(this.serverless.service.functions, (functionObject, functionName) => { + forEach(functionObject.events.aws.http_endpoints, (path, method) => { + const resourceId = this.resourceLogicalIds[path]; + + const methodTemplate = ` + { + "Type" : "AWS::ApiGateway::Method", + "Properties" : { + "AuthorizationType" : "NONE", + "HttpMethod" : "${method.toUpperCase()}", + "MethodResponses" : [ + { + "ResponseModels" : {}, + "ResponseParameters" : {}, + "StatusCode" : "200" + } + ], + "RequestParameters" : {}, + "ResourceId" : { "Ref": "${resourceId}" }, + "RestApiId" : { "Ref": "RestApiApigEvent" } + } + } + `; + + const methodObject = { + [`${functionName}Method${methodIndex}ApigEvent`]: JSON.parse(methodTemplate), + }; + + merge(this.serverless.service.resources.aws.Resources, + methodObject); + + methodIndex += 1; + }); + }); + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/tests/all.js b/lib/plugins/awsCompileApigEvents/tests/all.js index ceba920ac..485fe1445 100644 --- a/lib/plugins/awsCompileApigEvents/tests/all.js +++ b/lib/plugins/awsCompileApigEvents/tests/all.js @@ -5,3 +5,4 @@ require('./compileRestApi'); require('./compileDeployment'); require('./compileStage'); require('./compileResources'); +require('./compileMethods'); diff --git a/lib/plugins/awsCompileApigEvents/tests/compileMethods.js b/lib/plugins/awsCompileApigEvents/tests/compileMethods.js new file mode 100644 index 000000000..afec3bd53 --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/tests/compileMethods.js @@ -0,0 +1,85 @@ +'use strict'; + +const expect = require('chai').expect; +const AwsCompileApigEvents = require('../awsCompileApigEvents'); +const Serverless = require('../../../Serverless'); + +describe('#compileMethods()', () => { + let serverless; + let awsCompileApigEvents; + + const serviceResourcesAwsResourcesObjectMock = { + Resources: { + firstMethod0ApigEvent: { + Type: 'AWS::ApiGateway::Method', + Properties: { + AuthorizationType: 'NONE', + HttpMethod: 'POST', + MethodResponses: [ + { + ResponseModels: {}, + ResponseParameters: {}, + StatusCode: '200', + }, + ], + RequestParameters: {}, + ResourceId: { Ref: 'firstResource0ApigEvent' }, + RestApiId: { Ref: 'RestApiApigEvent' }, + }, + }, + firstMethod1ApigEvent: { + Type: 'AWS::ApiGateway::Method', + Properties: { + AuthorizationType: 'NONE', + HttpMethod: 'GET', + MethodResponses: [ + { + ResponseModels: {}, + ResponseParameters: {}, + StatusCode: '200', + }, + ], + RequestParameters: {}, + ResourceId: { Ref: 'firstResource1ApigEvent' }, + RestApiId: { Ref: 'RestApiApigEvent' }, + }, + }, + }, + }; + + beforeEach(() => { + serverless = new Serverless(); + serverless.init(); + serverless.service = { + functions: { + first: { + events: { + aws: { + http_endpoints: { + post: 'users/create', + get: 'users/create/list', + }, + }, + }, + }, + }, + resources: { + aws: { + Resources: {}, + }, + }, + }; + awsCompileApigEvents = new AwsCompileApigEvents(serverless); + awsCompileApigEvents.resourceLogicalIds = { + 'users/create': 'firstResource0ApigEvent', + 'users/create/list': 'firstResource1ApigEvent', + }; + }); + + it('should create method resources', () => { + awsCompileApigEvents.compileMethods().then(() => { + expect(JSON.stringify(serviceResourcesAwsResourcesObjectMock.Resources)) + .to.equal(JSON.stringify(awsCompileApigEvents.serverless.service.resources.aws.Resources)); + }); + }); +});