Skip to content

Commit cbe97ab

Browse files
committed
feat: add inputTransformer option
1 parent da08c31 commit cbe97ab

File tree

4 files changed

+28299
-10207
lines changed

4 files changed

+28299
-10207
lines changed

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = {
2323
let Name;
2424
let EventBusName;
2525
let IamRole;
26+
let InputTransformer;
27+
let InputPathsMap;
28+
let InputTemplate;
2629

2730
if (typeof eventRule === 'object') {
2831
if (!eventRule.event) {
@@ -41,14 +44,17 @@ module.exports = {
4144
}
4245
Input = eventRule.input;
4346
InputPath = eventRule.inputPath;
47+
InputTransformer = eventRule.inputTransformer;
48+
InputPathsMap = InputTransformer && eventRule.inputTransformer.inputPathsMap;
49+
InputTemplate = InputTransformer && eventRule.inputTransformer.inputTemplate;
4450
Description = eventRule.description;
4551
Name = eventRule.name;
4652
EventBusName = JSON.stringify(eventRule.eventBusName);
4753
IamRole = eventRule.iamRole;
4854

49-
if (Input && InputPath) {
55+
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
5056
const errorMessage = [
51-
'You can\'t set both input & inputPath properties at the',
57+
'You can\'t set input, inputPath and inputTransformer properties at the',
5258
'same time for cloudwatch events.',
5359
'Please check the AWS docs for more info',
5460
].join('');
@@ -62,6 +68,16 @@ module.exports = {
6268
// escape quotes to favor JSON.parse
6369
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
6470
}
71+
72+
// no need to escape quotes in inputPathsMap
73+
// because we add it as an object to the template
74+
if (InputPathsMap && typeof InputPathsMap === 'object') {
75+
InputPathsMap = JSON.stringify(InputPathsMap);
76+
}
77+
if (InputTemplate && typeof InputTemplate === 'string') {
78+
// escape quotes to favor JSON.parse
79+
InputTemplate = InputTemplate.replace(/\"/g, '\\"'); // eslint-disable-line
80+
}
6581
} else {
6682
const errorMessage = [
6783
`CloudWatch event of stateMachine "${stateMachineName}" is not an object`,
@@ -92,6 +108,10 @@ module.exports = {
92108
"Targets": [{
93109
${Input ? `"Input": "${Input.replace(/\\n|\\r/g, '')}",` : ''}
94110
${InputPath ? `"InputPath": "${InputPath.replace(/\r?\n/g, '')}",` : ''}
111+
${InputTransformer ? `"InputTransformer": {
112+
"InputPathsMap": ${InputPathsMap},
113+
"InputTemplate": "${InputTemplate}"
114+
},` : ''}
95115
"Arn": { "Ref": "${stateMachineLogicalId}" },
96116
"Id": "${cloudWatchId}",
97117
${IamRole ? `"RoleArn":"${IamRole}"` : `"RoleArn": {

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,43 @@ describe('awsCompileCloudWatchEventEvents', () => {
357357
.Properties.Targets[0].Input).to.equal('{"key":"value"}');
358358
});
359359

360-
itParam('should throw an error when both Input and InputPath are set', ['cloudwatchEvent', 'eventBridge'], (source) => {
360+
itParam('should respect inputTransformer variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
361+
serverlessStepFunctions.serverless.service.stepFunctions = {
362+
stateMachines: {
363+
first: {
364+
events: [
365+
{
366+
[source]: {
367+
event: {
368+
source: ['aws.ec2'],
369+
'detail-type': ['EC2 Instance State-change Notification'],
370+
detail: { state: ['pending'] },
371+
},
372+
enabled: false,
373+
inputTransformer: {
374+
inputPathsMap: {
375+
stage: '$.stageVariables',
376+
},
377+
inputTemplate: '{ "stage": <stage> }',
378+
},
379+
},
380+
},
381+
],
382+
},
383+
},
384+
};
385+
386+
serverlessStepFunctions.compileCloudWatchEventEvents();
387+
388+
expect(serverlessStepFunctions.serverless.service
389+
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
390+
.Properties.Targets[0].InputTransformer.InputPathsMap).to.have.property('stage', '$.stageVariables');
391+
expect(serverlessStepFunctions.serverless.service
392+
.provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleCloudWatchEvent1
393+
.Properties.Targets[0].InputTransformer.InputTemplate).to.equal('{ "stage": <stage> }');
394+
});
395+
396+
itParam('should throw an error when Input and InputPath and InputTransformer are set', ['cloudwatchEvent', 'eventBridge'], (source) => {
361397
serverlessStepFunctions.serverless.service.stepFunctions = {
362398
stateMachines: {
363399
first: {
@@ -374,6 +410,12 @@ describe('awsCompileCloudWatchEventEvents', () => {
374410
key: 'value',
375411
},
376412
inputPath: '$.stageVariables',
413+
inputTransformer: {
414+
inputPathsMap: {
415+
stage: '$.stageVariables',
416+
},
417+
inputTemplate: '{ "stage": <stage> }',
418+
},
377419
},
378420
},
379421
],

0 commit comments

Comments
 (0)