From 6cd49bf1174751bb4f9d5ce2a4dd7d4e05e5dbeb Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Mon, 13 Jun 2016 13:12:05 +0200 Subject: [PATCH] basic structure for apig event --- .../awsCompileApigEvents.js | 41 +++++++++++ .../lib/compileBasePathMapping.js | 10 +++ .../lib/compileDeployment.js | 10 +++ .../awsCompileApigEvents/lib/compileMethod.js | 10 +++ .../lib/compilePermission.js | 10 +++ .../lib/compileResource.js | 10 +++ .../lib/compileRestApi.js | 10 +++ .../awsCompileApigEvents/lib/compileStage.js | 10 +++ .../tests/awsCompileApigEvents.js | 72 +++++++++++++++++++ 9 files changed, 183 insertions(+) create mode 100644 lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileBasePathMapping.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileDeployment.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileMethod.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compilePermission.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileResource.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileRestApi.js create mode 100644 lib/plugins/awsCompileApigEvents/lib/compileStage.js create mode 100644 lib/plugins/awsCompileApigEvents/tests/awsCompileApigEvents.js diff --git a/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js new file mode 100644 index 000000000..ef6ef1ced --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js @@ -0,0 +1,41 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +const compileRestApi = require('./lib/compileRestApi'); +const compileDeployment = require('./lib/compileDeployment'); +const compileStage = require('./lib/compileStage'); +const compileBasePathMapping = require('./lib/compileBasePathMapping'); +const compileResource = require('./lib/compileResource'); +const compileMethod = require('./lib/compileMethod'); +const compilePermission = require('./lib/compilePermission'); + +class AwsCompileApigEvents { + constructor(serverless) { + this.serverless = serverless; + + Object.assign( + this, + compileRestApi, + compileDeployment, + compileStage, + compileBasePathMapping, + compileResource, + compileMethod, + compilePermission + ); + + this.hooks = { + 'deploy:compileEvents': () => BbPromise.bind(this) + .then(this.compileRestApi) + .then(this.compileDeployment) + .then(this.compileStage) + .then(this.compileBasePathMapping) + .then(this.compileResource) + .then(this.compileMethod) + .then(this.compilePermission), + }; + } +} + +module.exports = AwsCompileApigEvents; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileBasePathMapping.js b/lib/plugins/awsCompileApigEvents/lib/compileBasePathMapping.js new file mode 100644 index 000000000..5fec84e1a --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileBasePathMapping.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileStage() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js b/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js new file mode 100644 index 000000000..c1b0cc920 --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileDeployment() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileMethod.js b/lib/plugins/awsCompileApigEvents/lib/compileMethod.js new file mode 100644 index 000000000..72218f0c9 --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileMethod.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileMethod() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compilePermission.js b/lib/plugins/awsCompileApigEvents/lib/compilePermission.js new file mode 100644 index 000000000..da661582f --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compilePermission.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compilePermission() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileResource.js b/lib/plugins/awsCompileApigEvents/lib/compileResource.js new file mode 100644 index 000000000..d0bdff062 --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileResource.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileResource() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js b/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js new file mode 100644 index 000000000..1e096e391 --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileRestApi() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileStage.js b/lib/plugins/awsCompileApigEvents/lib/compileStage.js new file mode 100644 index 000000000..5fec84e1a --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/lib/compileStage.js @@ -0,0 +1,10 @@ +'use strict'; + +const BbPromise = require('bluebird'); + +module.exports = { + compileStage() { + + return BbPromise.resolve(); + }, +}; diff --git a/lib/plugins/awsCompileApigEvents/tests/awsCompileApigEvents.js b/lib/plugins/awsCompileApigEvents/tests/awsCompileApigEvents.js new file mode 100644 index 000000000..eccfd554e --- /dev/null +++ b/lib/plugins/awsCompileApigEvents/tests/awsCompileApigEvents.js @@ -0,0 +1,72 @@ +'use strict'; + +const expect = require('chai').expect; +const AwsCompileScheduledEvents = require('../awsCompileScheduledEvents'); +const Serverless = require('../../../Serverless'); + +describe('awsCompileScheduledEvents', () => { + let serverless; + let awsCompileScheduledEvents; + + beforeEach(() => { + serverless = new Serverless(); + serverless.init(); + serverless.service.resources = { aws: { Resources: {} } }; + awsCompileScheduledEvents = new AwsCompileScheduledEvents(serverless); + awsCompileScheduledEvents.serverless.service.service = 'new-service'; + }); + + describe('#compileScheduledEvents()', () => { + it('should throw an error if the aws resource is not available', () => { + awsCompileScheduledEvents.serverless.service.resources.aws.Resources = false; + expect(() => awsCompileScheduledEvents.compileScheduledEvents()).to.throw(Error); + }); + + it('should compile scheduled events into CF resources', () => { + awsCompileScheduledEvents.serverless.service.functions = { + hello: { + events: { + aws: { + schedule: 'rate(10 minutes)', + }, + }, + }, + }; + + const scheduleResrouce = ` + { + "Type": "AWS::Events::Rule", + "Properties": { + "ScheduleExpression": "rate(10 minutes)", + "State": "ENABLED", + "Targets": [{ + "Arn": { "Fn::GetAtt": ["hello", "Arn"] }, + "Id": "helloScheduleEvent" + }] + } + } + `; + + const permissionResource = ` + { + "Type": "AWS::Lambda::Permission", + "Properties": { + "FunctionName": { "Fn::GetAtt": ["hello", "Arn"] }, + "Action": "lambda:InvokeFunction", + "Principal": "events.amazonaws.com", + "SourceArn": { "Fn::GetAtt": ["helloScheduleEvent", "Arn"] } + } + } + `; + + awsCompileScheduledEvents.compileScheduledEvents(); + + expect(awsCompileScheduledEvents.serverless.service + .resources.aws.Resources.helloScheduleEvent) + .to.deep.equal(JSON.parse(scheduleResrouce)); + expect(awsCompileScheduledEvents.serverless.service + .resources.aws.Resources.helloScheduleEventPermission) + .to.deep.equal(JSON.parse(permissionResource)); + }); + }); +});