Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions lib/schedule_events.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

const aws = require('aws-sdk');
const lambda = new aws.Lambda({
apiVersion: '2015-03-31'
});
const cloudwatchevents = new aws.CloudWatchEvents({
apiVersion: '2015-10-07'
});
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'
});
};

const ScheduleEvents = {
ScheduleEvents.prototype = {
_ruleDescription: (params) => {
return `${params.ScheduleName} - ${params.ScheduleExpression}`;
},
Expand All @@ -17,20 +19,21 @@ const ScheduleEvents = {
return params.FunctionArnPrefix + params.FunctionName;
},

_putRulePrams: (params) => {
_putRulePrams: function(params) {
return {
Name: params.ScheduleName,
Description: ScheduleEvents._ruleDescription(params),
Description: this._ruleDescription(params),
State: params.ScheduleState,
ScheduleExpression: params.ScheduleExpression
};
},

_putRule: (params) => {
_putRule: function(params) {
const _this = this;
// return RuleArn if created
return new Promise((resolve) => {
const _params = ScheduleEvents._putRulePrams(params);
cloudwatchevents.putRule(_params, (err, rule) => {
const _params = _this._putRulePrams(params);
_this.cloudwatchevents.putRule(_params, (err, rule) => {
if (err) throw err;
resolve(rule);
});
Expand All @@ -47,10 +50,11 @@ const ScheduleEvents = {
};
},

_addPermission: (params) => {
_addPermission: function(params) {
const _this = this;
return new Promise((resolve) => {
const _params = ScheduleEvents._addPermissionParams(params);
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.
Expand All @@ -61,34 +65,36 @@ const ScheduleEvents = {
});
},

_putTargetsParams: (params) => {
_putTargetsParams: function(params) {
return {
Rule: params.ScheduleName,
Targets: [{
Arn: ScheduleEvents._functionArn(params),
Arn: this._functionArn(params),
Id: params.FunctionName
}]
};
},

_putTargets: (params) => {
_putTargets: function(params) {
const _this = this;
return new Promise((resolve) => {
const _params = ScheduleEvents._putTargetsParams(params);
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: (params) => {
add: function(params) {
const _this = this;
return Promise.resolve().then(() => {
return ScheduleEvents._putRule(params);
return _this._putRule(params);
}).then((rule) => {
return ScheduleEvents._addPermission(Object.assign(params, rule));
return _this._addPermission(Object.assign(params, rule));
}).then((data) => {
return ScheduleEvents._putTargets(params);
return _this._putTargets(params);
});
},
};
Expand Down
3 changes: 2 additions & 1 deletion test/schedule_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('chai').assert;
const path = require('path');
const aws = require('aws-sdk-mock');
aws.setSDK(path.resolve('node_modules/aws-sdk'));
const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events'));

const params = {
FunctionName: 'node-lambda-test-function',
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('schedule_events', () => {
callback(null, mockResponse.addPermission);
});

schedule = require(path.join('..', 'lib', 'schedule_events'));
schedule = new ScheduleEvents(require('aws-sdk'));
});

describe('_ruleDescription', () => {
Expand Down