diff --git a/lib/schedule_events.js b/lib/schedule_events.js index b39d51f4..1c6ac503 100644 --- a/lib/schedule_events.js +++ b/lib/schedule_events.js @@ -1,49 +1,48 @@ 'use strict' -const ScheduleEvents = function (aws) { - // Authenticated `aws` object in `lib/main.js` - this.lambda = new aws.Lambda({ - apiVersion: '2015-03-31' - }) - this.cloudwatchevents = new aws.CloudWatchEvents({ - apiVersion: '2015-10-07' - }) -} +class ScheduleEvents { + constructor (aws) { + // Authenticated `aws` object in `lib/main.js` + this.lambda = new aws.Lambda({ + apiVersion: '2015-03-31' + }) + this.cloudwatchevents = new aws.CloudWatchEvents({ + apiVersion: '2015-10-07' + }) + } -ScheduleEvents.prototype = { - _ruleDescription: (params) => { + _ruleDescription (params) { if ('ScheduleDescription' in params && params.ScheduleDescription != null) { return `${params.ScheduleDescription}` } return `${params.ScheduleName} - ${params.ScheduleExpression}` - }, + } - _functionName: (params) => { + _functionName (params) { return params.FunctionArn.split(':').pop() - }, + } - _putRulePrams: function (params) { + _putRulePrams (params) { return { Name: params.ScheduleName, Description: this._ruleDescription(params), State: params.ScheduleState, ScheduleExpression: params.ScheduleExpression } - }, + } - _putRule: function (params) { - const _this = this + _putRule (params) { // return RuleArn if created return new Promise((resolve) => { - const _params = _this._putRulePrams(params) - _this.cloudwatchevents.putRule(_params, (err, rule) => { + const _params = this._putRulePrams(params) + this.cloudwatchevents.putRule(_params, (err, rule) => { if (err) throw err resolve(rule) }) }) - }, + } - _addPermissionParams: function (params) { + _addPermissionParams (params) { return { Action: 'lambda:InvokeFunction', FunctionName: this._functionName(params), @@ -51,13 +50,12 @@ ScheduleEvents.prototype = { SourceArn: params.RuleArn, StatementId: params.ScheduleName } - }, + } - _addPermission: function (params) { - const _this = this + _addPermission (params) { return new Promise((resolve) => { - const _params = _this._addPermissionParams(params) - _this.lambda.addPermission(_params, (err, data) => { + const _params = this._addPermissionParams(params) + this.lambda.addPermission(_params, (err, data) => { if (err) { if (err.code !== 'ResourceConflictException') throw err // If it exists it will result in an error but there is no problem. @@ -66,9 +64,9 @@ ScheduleEvents.prototype = { resolve(data) }) }) - }, + } - _putTargetsParams: function (params) { + _putTargetsParams (params) { return { Rule: params.ScheduleName, Targets: [{ @@ -76,28 +74,26 @@ ScheduleEvents.prototype = { Id: this._functionName(params) }] } - }, + } - _putTargets: function (params) { - const _this = this + _putTargets (params) { return new Promise((resolve) => { - const _params = _this._putTargetsParams(params) - _this.cloudwatchevents.putTargets(_params, (err, data) => { + const _params = this._putTargetsParams(params) + this.cloudwatchevents.putTargets(_params, (err, data) => { // even if it is already registered, it will not be an error. if (err) throw (err) resolve(data) }) }) - }, + } - add: function (params) { - const _this = this + add (params) { return Promise.resolve().then(() => { - return _this._putRule(params) + return this._putRule(params) }).then((rule) => { - return _this._addPermission(Object.assign(params, rule)) + return this._addPermission(Object.assign(params, rule)) }).then((data) => { - return _this._putTargets(params) + return this._putTargets(params) }) } }