Skip to content

Commit

Permalink
fix(aws-events): support newlines in textTemplate
Browse files Browse the repository at this point in the history
Support newlines in CloudWatch Events textTemplate as intended, by
making a newline-separated list of JSON strings.

Fixes #1514.
  • Loading branch information
rix0rrr committed Jan 18, 2019
1 parent 33c2a6d commit ef52892
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
3 changes: 3 additions & 0 deletions examples/cdk-examples-typescript/cloudwatch-events/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "node index"
}
25 changes: 25 additions & 0 deletions examples/cdk-examples-typescript/cloudwatch-events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import events = require('@aws-cdk/aws-events');
import sns = require('@aws-cdk/aws-sns');
import cdk = require('@aws-cdk/cdk');

const app = new cdk.App();

class CloudWatchEventsExample extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);

const topic = new sns.Topic(this, 'TestTopic');

const event = new events.EventRule(this, 'Rule', {
scheduleExpression: 'rate(1 minute)'
});

event.addTarget(topic, {
textTemplate: 'one line\nsecond line'
});
}
}

new CloudWatchEventsExample(app, 'CWE-Example');

app.run();
1 change: 1 addition & 0 deletions examples/cdk-examples-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@aws-cdk/aws-rds": "^0.22.0",
"@aws-cdk/aws-s3": "^0.22.0",
"@aws-cdk/aws-sns": "^0.22.0",
"@aws-cdk/aws-events": "^0.22.0",
"@aws-cdk/aws-sqs": "^0.22.0",
"@aws-cdk/cdk": "^0.22.0",
"@aws-cdk/runtime-values": "^0.22.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-events/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export class EventRule extends Construct implements IEventRule {
if (inputOptions.jsonTemplate) {
inputTemplate = inputOptions.jsonTemplate;
} else if (typeof(inputOptions.textTemplate) === 'string') {
inputTemplate = JSON.stringify(inputOptions.textTemplate);
// Newline separated list of JSON-encoded strings
inputTemplate = inputOptions.textTemplate.split('\n').map(x => JSON.stringify(x)).join('\n');
} else {
inputTemplate = `"${inputOptions.textTemplate}"`;
}
Expand Down
94 changes: 92 additions & 2 deletions packages/@aws-cdk/aws-events/test/test.rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/cdk');
import { Stack } from '@aws-cdk/cdk';
import { Test } from 'nodeunit';
Expand Down Expand Up @@ -351,5 +351,95 @@ export = {
test.deepEqual(importedRule.ruleArn, 'arn:of:rule');

test.done();
}
},

'text templates': {
'strings with newlines are serialized to a newline-delimited list of JSON strings'(test: Test) {
// GIVEN
const stack = new Stack();
const rule = new EventRule(stack, 'Rule', {
scheduleExpression: 'rate(1 minute)'
});

// WHEN
rule.addTarget(new SomeTarget(), {
textTemplate: 'I have\nmultiple lines',
});

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputTemplate: "\"I have\"\n\"multiple lines\""
},
}
]
}));

test.done();
},

'escaped newlines are not interpreted as newlines'(test: Test) {
// GIVEN
const stack = new Stack();
const rule = new EventRule(stack, 'Rule', {
scheduleExpression: 'rate(1 minute)'
});

// WHEN
rule.addTarget(new SomeTarget(), {
textTemplate: 'this is not\\na real newline',
});

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputTemplate: "\"this is not\\\\na real newline\""
},
}
]
}));

test.done();
},

'can use Tokens in text templates'(test: Test) {
// GIVEN
const stack = new Stack();
const rule = new EventRule(stack, 'Rule', {
scheduleExpression: 'rate(1 minute)'
});

const world = new cdk.Token(() => 'world');

// WHEN
rule.addTarget(new SomeTarget(), {
textTemplate: `hello ${world}`,
});

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputTemplate: "\"hello world\""
},
}
]
}));

test.done();
}
},
};

class SomeTarget implements IEventRuleTarget {
public asEventRuleTarget() {
return {
id: 'T1', arn: 'ARN1', kinesisParameters: { partitionKeyPath: 'partitionKeyPath' }
};
}
}

0 comments on commit ef52892

Please sign in to comment.