From 55a62c3e0f66528e3cb82c1b2e531fb9be246b0d Mon Sep 17 00:00:00 2001 From: Tom McLaughlin Date: Mon, 26 Feb 2018 10:31:54 -0600 Subject: [PATCH] Support cloud watch events --- lib/events/CloudWatchEventConverter.js | 42 ++++++++++++++++++++++++++ lib/events/events.js | 14 ++++++--- 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 lib/events/CloudWatchEventConverter.js diff --git a/lib/events/CloudWatchEventConverter.js b/lib/events/CloudWatchEventConverter.js new file mode 100644 index 0000000..9477351 --- /dev/null +++ b/lib/events/CloudWatchEventConverter.js @@ -0,0 +1,42 @@ +/* + * Copyright 2017 Stefano Buliani (@sapessi) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strct' + +const utils = require('../utils'); + +class CloudWatchEventConverter { + constructor(serverless) { + this.serverless = serverless; + } + + getEventType() { + return "CloudWatchEvent"; + } + + convertEvent(event, targetResourceName) { + let CloudWatchEvent = { + Pattern: event + }; + + return { + event: CloudWatchEvent, + resources: {} + } + } +} + +module.exports = CloudWatchEventConverter; + diff --git a/lib/events/events.js b/lib/events/events.js index edb2994..32e51cc 100644 --- a/lib/events/events.js +++ b/lib/events/events.js @@ -22,20 +22,21 @@ const ScheduleEventConverter = require('./ScheduleEventConverter'); const S3EventConverter = require('./S3EventConverter'); const AlexaSkillEventConverter = require('./AlexaSkillEventConverter'); const IoTEventConverter = require('./IoTEventConverter'); +const CloudWatchEventConverter = require('./CloudWatchEventConverter'); let convertersCache = {}; /** - * This function loads the correct event converter based on the given event type. Event converters are used to take + * This function loads the correct event converter based on the given event type. Event converters are used to take * an event from the serverless.yml file and convert it into an event in the SAM template. In some cases, the Serverless * framework generates entirely new resources based on an event definition. For example, when the S3 event is specified * with only a bucket name, the Serverless framework creates an entirely new bucket. - * + * * Event converters must implement the same methods: * * getEventType() -> returns the event type for the SAM template, such as "Api" or "S3" - * * convertEvent(event, targetResourceName) -> Creates the output SAM event give a Serverless event object and a potential + * * convertEvent(event, targetResourceName) -> Creates the output SAM event give a Serverless event object and a potential * target resource. For example, for HTTP events, the target resource is the Lambda function behind the API method. - * + * * The convertEvent() method should return an object that contains two properties: event, and resources. Event is an object * that contains the event definition for the SAM template. Resources is also an object that contains the custom resources * that should be added to the template. @@ -68,10 +69,13 @@ module.exports.getEventConverter = (type, serverless) => { case "iot": converter = new IoTEventConverter(serverless); break; + case "cloudwatchevent": + converter = new CloudWatchEventConverter(serverless); + break; default: throw new Error("Could not find converter for event type: " + type); } convertersCache[type] = converter; return convertersCache[type]; -} \ No newline at end of file +}