diff --git a/lib/event_sources.json.example b/lib/event_sources.json.example index 8944af9f..6e774869 100644 --- a/lib/event_sources.json.example +++ b/lib/event_sources.json.example @@ -1,8 +1,18 @@ -[ +{ + "EventSourceMappings": [ { - "EventSourceArn": "your event source arn", - "StartingPosition": "LATEST", - "BatchSize": 100, - "Enabled": true + "EventSourceArn": "your event source arn", + "StartingPosition": "LATEST", + "BatchSize": 100, + "Enabled": true } -] \ No newline at end of file + ], + "ScheduleEvents": [ + { + "FunctionArnPrefix": "arn:aws:lambda:us-west-2:XXX:function:", + "ScheduleName": "node-lambda-test-schedule", + "ScheduleState": "ENABLED", + "ScheduleExpression": "rate(1 hour)" + } + ] +} diff --git a/lib/main.js b/lib/main.js index 9ddfeb41..46df3f8a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -145,13 +145,30 @@ Lambda.prototype._params = function (program, buffer) { Lambda.prototype._eventSourceList = function (program) { if (!program.eventSourceFile) { - return []; + return { + EventSourceMappings: [], + ScheduleEvents: [] + }; } - try { - return fs.readJsonSync(program.eventSourceFile); - } catch(err) { - throw err; + const list = (function() { + try { + return fs.readJsonSync(program.eventSourceFile); + } catch(err) { + throw err; + } + })(); + + if (Object.prototype.toString.call(list) === '[object Array]') { + // backward-compatible + return { EventSourceMappings: list }; + } + if (!list.EventSourceMappings) { + list.EventSourceMappings = []; + } + if (!list.ScheduleEvents) { + list.ScheduleEvents = []; } + return list; }; /** @@ -607,7 +624,7 @@ Lambda.prototype.deploy = function (program) { console.log(results); // Updating event source(s) - _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList, function(err, results) { + _this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function(err, results) { cb(null, results); }); }); @@ -632,7 +649,7 @@ Lambda.prototype.deploy = function (program) { }); }, function(_callback) { - _this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList, function(err, results) { + _this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function(err, results) { _callback(err, results); }); } diff --git a/test/main.js b/test/main.js index addc05c7..1c8713cd 100644 --- a/test/main.js +++ b/test/main.js @@ -507,18 +507,10 @@ describe('node-lambda', function () { describe('_eventSourceList', function () { it('program.eventSourceFile is empty value', function () { program.eventSourceFile = ''; - assert.deepEqual(lambda._eventSourceList(program), []); - }); - - it('program.eventSourceFile is valid value', function () { - program.eventSourceFile = 'event_sources.json'; - const expected = [{ - BatchSize: 100, - Enabled: true, - EventSourceArn: 'your event source arn', - StartingPosition: 'LATEST', - }]; - assert.deepEqual(lambda._eventSourceList(program), expected); + assert.deepEqual( + lambda._eventSourceList(program), + { EventSourceMappings: [], ScheduleEvents: [] } + ); }); it('program.eventSourceFile is invalid value', function () { @@ -529,6 +521,83 @@ describe('node-lambda', function () { "ENOENT: no such file or directory, open '/hoge/fuga'" ); }); + + describe('program.eventSourceFile is valid value', function() { + before(function () { + fs.writeFileSync('only_EventSourceMappings.json', JSON.stringify({ + EventSourceMappings: [{ test: 1 }] + })); + fs.writeFileSync('only_ScheduleEvents.json', JSON.stringify({ + ScheduleEvents: [{ test: 2 }] + })); + }); + + after(function () { + fs.unlinkSync('only_EventSourceMappings.json'); + fs.unlinkSync('only_ScheduleEvents.json'); + }); + + it('only EventSourceMappings', function () { + program.eventSourceFile = 'only_EventSourceMappings.json'; + const expected = { + EventSourceMappings: [{ test: 1 }], + ScheduleEvents: [], + }; + assert.deepEqual(lambda._eventSourceList(program), expected); + }); + + it('only ScheduleEvents', function () { + program.eventSourceFile = 'only_ScheduleEvents.json'; + const expected = { + EventSourceMappings: [], + ScheduleEvents: [{ test: 2 }], + }; + assert.deepEqual(lambda._eventSourceList(program), expected); + }); + + it('EventSourceMappings & ScheduleEvents', function () { + program.eventSourceFile = 'event_sources.json'; + const expected = { + EventSourceMappings: [{ + BatchSize: 100, + Enabled: true, + EventSourceArn: 'your event source arn', + StartingPosition: 'LATEST', + }], + ScheduleEvents: [{ + FunctionArnPrefix: 'arn:aws:lambda:us-west-2:XXX:function:', + ScheduleName: 'node-lambda-test-schedule', + ScheduleState: 'ENABLED', + ScheduleExpression: 'rate(1 hour)', + }], + }; + assert.deepEqual(lambda._eventSourceList(program), expected); + }); + }); + + describe('old style event_sources.json', function () { + const oldStyleValue = [{ + BatchSize: 100, + Enabled: true, + EventSourceArn: 'your event source arn', + StartingPosition: 'LATEST', + }]; + const fileName = 'event_sources_old_style.json'; + + before(function () { + fs.writeFileSync(fileName, JSON.stringify(oldStyleValue)); + }); + + after(function () { + fs.unlinkSync(fileName); + }); + + it('program.eventSourceFile is valid value', function () { + program.eventSourceFile = fileName; + const expected = { EventSourceMappings: oldStyleValue }; + assert.deepEqual(lambda._eventSourceList(program), expected); + }); + }); }); });