Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to add ScheduleEvents to main.js #228

Merged
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
1 change: 0 additions & 1 deletion lib/event_sources.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
],
"ScheduleEvents": [
{
"FunctionArnPrefix": "arn:aws:lambda:us-west-2:XXX:function:",
"ScheduleName": "node-lambda-test-schedule",
"ScheduleState": "ENABLED",
"ScheduleExpression": "rate(1 hour)"
Expand Down
40 changes: 35 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var path = require('path');
var async = require('async');
var zip = new require('node-zip')();
var dotenv = require('dotenv');
var ScheduleEvents = require('./schedule_events');

var maxBufferSize = 50 * 1024 * 1024;

Expand Down Expand Up @@ -532,6 +533,21 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE
});
};

Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn, scheduleList, cb) {
return async.series(scheduleList.map(function(schedule) {
return function(_cb) {
const params = Object.assign(schedule, { FunctionArn: functionArn });
scheduleEvents.add(params).then(function (data) {
_cb(null, params);
}).catch(function (err) {
_cb(err);
});
};
}), function(err, results) {
cb(err, results);
});
};

Lambda.prototype.package = function (program) {
var _this = this;
if (!program.packageDirectory) {
Expand Down Expand Up @@ -609,6 +625,7 @@ Lambda.prototype.deploy = function (program) {
var lambda = new aws.Lambda({
apiVersion: '2015-03-31'
});
var scheduleEvents = new ScheduleEvents(aws);

// Checking function
return lambda.getFunction({
Expand All @@ -623,9 +640,20 @@ Lambda.prototype.deploy = function (program) {
console.log('=> Zip file(s) done uploading. Results follow: ');
console.log(results);

// Updating event source(s)
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) {
cb(null, results);
async.parallel([
function(_callback) {
// Updating event source(s)
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) {
_callback(null, results);
});
},
function(_callback) {
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) {
_callback(err, results);
});
}
], function(err, results) {
cb(err, results);
});
});
}
Expand All @@ -645,7 +673,9 @@ Lambda.prototype.deploy = function (program) {
}
console.log('=> Zip file(s) done uploading. Results follow: ');
console.log(results);
_callback(err, results);
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function(err, results) {
_callback(err, results);
});
});
},
function(_callback) {
Expand All @@ -663,7 +693,7 @@ Lambda.prototype.deploy = function (program) {
throw err;
}
console.log('=> All tasks done. Results follow: ');
console.log(results);
console.log(JSON.stringify(results, null, ' '));
});
});
};
Expand Down
12 changes: 6 additions & 6 deletions lib/schedule_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ ScheduleEvents.prototype = {
return `${params.ScheduleName} - ${params.ScheduleExpression}`;
},

_functionArn: (params) => {
return params.FunctionArnPrefix + params.FunctionName;
_functionName: (params) => {
return params.FunctionArn.split(':').pop();
},

_putRulePrams: function(params) {
Expand All @@ -40,10 +40,10 @@ ScheduleEvents.prototype = {
});
},

_addPermissionParams: (params) => {
_addPermissionParams: function(params) {
return {
Action: 'lambda:InvokeFunction',
FunctionName: params.FunctionName,
FunctionName: this._functionName(params),
Principal: 'events.amazonaws.com',
SourceArn: params.RuleArn,
StatementId: params.ScheduleName
Expand All @@ -69,8 +69,8 @@ ScheduleEvents.prototype = {
return {
Rule: params.ScheduleName,
Targets: [{
Arn: this._functionArn(params),
Id: params.FunctionName
Arn: params.FunctionArn,
Id: this._functionName(params)
}]
};
},
Expand Down
60 changes: 59 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ describe('node-lambda', function () {
StartingPosition: 'LATEST',
}],
ScheduleEvents: [{
FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:',
ScheduleName: 'node-lambda-test-schedule',
ScheduleState: 'ENABLED',
ScheduleExpression: 'rate(1 hour)',
Expand Down Expand Up @@ -601,6 +600,65 @@ describe('node-lambda', function () {
});
});

describe('_updateScheduleEvents', function () {
const aws = require('aws-sdk-mock');
const ScheduleEvents = require('../lib/schedule_events');
const eventSourcesJsonValue = {
ScheduleEvents: [{
ScheduleName: 'node-lambda-test-schedule',
ScheduleState: 'ENABLED',
ScheduleExpression: 'rate(1 hour)',
}]
};

var schedule = null;

before(function () {
aws.mock('CloudWatchEvents', 'putRule', function (params, callback) {
callback(null, {});
});
aws.mock('CloudWatchEvents', 'putTargets', function (params, callback) {
callback(null, {});
});
aws.mock('Lambda', 'addPermission', function (params, callback) {
callback(null, {});
});

fs.writeFileSync(
'event_sources.json',
JSON.stringify(eventSourcesJsonValue)
);

schedule = new ScheduleEvents(require('aws-sdk'));
});

after(function () {
fs.unlinkSync('event_sources.json');
aws.restore('CloudWatchEvents');
aws.restore('Lambda');
});

it('simple test with mock', function () {
program.eventSourceFile = 'event_sources.json';
const eventSourceList = lambda._eventSourceList(program);
const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function';
return new Promise(function (resolve) {
lambda._updateScheduleEvents(schedule, functionArn, eventSourceList.ScheduleEvents, function(err, results) {
resolve({ err: err, results: results });
});
}).then(function (actual) {
const expected = {
err: undefined,
results: [Object.assign(
eventSourcesJsonValue.ScheduleEvents[0],
{ FunctionArn: functionArn }
)]
};
assert.deepEqual(actual, expected);
});
});
});

describe('check env vars before create sample files', function () {
const filesCreatedBySetup = [
'.env',
Expand Down
9 changes: 4 additions & 5 deletions test/schedule_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ aws.setSDK(path.resolve('node_modules/aws-sdk'));
const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events'));

const params = {
FunctionName: 'node-lambda-test-function',
FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:',
FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function',
ScheduleName: 'node-lambda-test-schedule',
ScheduleState: 'ENABLED',
ScheduleExpression: 'rate(1 hour)'
Expand Down Expand Up @@ -62,11 +61,11 @@ describe('schedule_events', () => {
});
});

describe('_functionArn', () => {
describe('_functionName', () => {
it('correct value', () => {
assert.equal(
schedule._functionArn(params),
'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'
schedule._functionName(params),
'node-lambda-test-function'
);
});
});
Expand Down