diff --git a/examples/cdk-examples-typescript/cloudwatch-events/cdk.json b/examples/cdk-examples-typescript/cloudwatch-events/cdk.json new file mode 100644 index 0000000000000..2f0e44c6fd27b --- /dev/null +++ b/examples/cdk-examples-typescript/cloudwatch-events/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "node index" +} diff --git a/examples/cdk-examples-typescript/cloudwatch-events/index.ts b/examples/cdk-examples-typescript/cloudwatch-events/index.ts new file mode 100644 index 0000000000000..4c2c5219d3412 --- /dev/null +++ b/examples/cdk-examples-typescript/cloudwatch-events/index.ts @@ -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(); diff --git a/examples/cdk-examples-typescript/package.json b/examples/cdk-examples-typescript/package.json index 91f5dc2d3f5b0..358e5badf0621 100644 --- a/examples/cdk-examples-typescript/package.json +++ b/examples/cdk-examples-typescript/package.json @@ -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" diff --git a/packages/@aws-cdk/aws-events/lib/rule.ts b/packages/@aws-cdk/aws-events/lib/rule.ts index 56b58e73c96c5..27f6cb66cfe66 100644 --- a/packages/@aws-cdk/aws-events/lib/rule.ts +++ b/packages/@aws-cdk/aws-events/lib/rule.ts @@ -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}"`; } diff --git a/packages/@aws-cdk/aws-events/test/test.rule.ts b/packages/@aws-cdk/aws-events/test/test.rule.ts index 870194bde5ead..e04ea6ed864a6 100644 --- a/packages/@aws-cdk/aws-events/test/test.rule.ts +++ b/packages/@aws-cdk/aws-events/test/test.rule.ts @@ -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'; @@ -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' } + }; + } +} \ No newline at end of file