From e3750e64005271dbc0c38d15a3f18377e782ee2d Mon Sep 17 00:00:00 2001 From: Kostas Bariotis Date: Sun, 30 Oct 2016 10:57:01 +0200 Subject: [PATCH] support for nested Input objects --- docs/providers/aws/events/schedule.md | 8 +++-- .../deploy/compile/events/schedule/index.js | 6 ++++ .../compile/events/schedule/tests/index.js | 33 +++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/docs/providers/aws/events/schedule.md b/docs/providers/aws/events/schedule.md index 3344ac11f..4dc7e6d03 100644 --- a/docs/providers/aws/events/schedule.md +++ b/docs/providers/aws/events/schedule.md @@ -36,9 +36,13 @@ functions: - schedule: rate: rate(10 minutes) enabled: false - input: '{"key": "value"}' + input: + key1: value1 + key2: value2 + stageParams: + stage: dev - schedule: rate: cron(0 12 * * ? *) enabled: false - inputPath: '' + inputPath: '$.stageVariables' ``` diff --git a/lib/plugins/aws/deploy/compile/events/schedule/index.js b/lib/plugins/aws/deploy/compile/events/schedule/index.js index 7d83b9bf5..c0ca0f110 100644 --- a/lib/plugins/aws/deploy/compile/events/schedule/index.js +++ b/lib/plugins/aws/deploy/compile/events/schedule/index.js @@ -42,6 +42,12 @@ class AwsCompileScheduledEvents { State = event.schedule.enabled ? 'ENABLED' : 'DISABLED'; Input = event.schedule.input || ''; InputPath = event.schedule.inputPath || ''; + + if (Input && typeof Input === 'object') { + Input = JSON.stringify(Input); + } + // escape quotes to favor JSON.parse + Input = Input.replace(/\"/g,'\\"'); // eslint-disable-line } else if (typeof event.schedule === 'string') { ScheduleExpression = event.schedule; State = 'ENABLED'; diff --git a/lib/plugins/aws/deploy/compile/events/schedule/tests/index.js b/lib/plugins/aws/deploy/compile/events/schedule/tests/index.js index c4f61f261..4b820a1a0 100644 --- a/lib/plugins/aws/deploy/compile/events/schedule/tests/index.js +++ b/lib/plugins/aws/deploy/compile/events/schedule/tests/index.js @@ -109,7 +109,8 @@ describe('AwsCompileScheduledEvents', () => { schedule: { rate: 'rate(10 minutes)', enabled: false, - input: '{}', + input: '{"key":"value"}', + inputPath: '$.stageVariables', }, }, ], @@ -121,11 +122,37 @@ describe('AwsCompileScheduledEvents', () => { expect(awsCompileScheduledEvents.serverless.service .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule1 .Properties.Targets[0].Input - ).to.equal('{}'); + ).to.equal('{"key":"value"}'); expect(awsCompileScheduledEvents.serverless.service .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule1 .Properties.Targets[0].InputPath - ).to.equal(''); + ).to.equal('$.stageVariables'); + }); + + it('should respect input variable as an object', () => { + awsCompileScheduledEvents.serverless.service.functions = { + first: { + events: [ + { + schedule: { + rate: 'rate(10 minutes)', + enabled: false, + input: { + key: 'value', + }, + inputPath: '$.stageVariables', + }, + }, + ], + }, + }; + + awsCompileScheduledEvents.compileScheduledEvents(); + + expect(awsCompileScheduledEvents.serverless.service + .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule1 + .Properties.Targets[0].Input + ).to.equal('{"key":"value"}'); }); it('should not create corresponding resources when scheduled events are not given', () => {