Skip to content

Commit

Permalink
feat(aws-events): update eventfield to support embedded string variables
Browse files Browse the repository at this point in the history
  • Loading branch information
thantos committed Mar 9, 2021
1 parent c4dc3bc commit 3076cf0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('Can use EC2 taskdef as EventRule target', () => {
InputPathsMap: {
'detail-event': '$.detail.event',
},
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo",<detail-event>]}]}',
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo","<detail-event>"]}]}',
},
RoleArn: { 'Fn::GetAtt': ['TaskDefEventsRoleFB3B67B8', 'Arn'] },
Id: 'Target0',
Expand Down Expand Up @@ -134,7 +134,7 @@ test('Can import an EC2 task definition from task definition attributes as Event
InputPathsMap: {
'detail-event': '$.detail.event',
},
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo",<detail-event>]}]}',
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo","<detail-event>"]}]}',
},
RoleArn: { 'Fn::GetAtt': ['TaskDefEventsRoleFB3B67B8', 'Arn'] },
Id: 'Target0',
Expand Down Expand Up @@ -213,7 +213,7 @@ test('Can import a Fargate task definition from task definition attributes as Ev
InputPathsMap: {
'detail-event': '$.detail.event',
},
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo",<detail-event>]}]}',
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo","<detail-event>"]}]}',
},
RoleArn: { 'Fn::GetAtt': ['TaskDefEventsRoleFB3B67B8', 'Arn'] },
Id: 'Target0',
Expand Down Expand Up @@ -292,7 +292,7 @@ test('Can import a Task definition from task definition attributes as EventRule
InputPathsMap: {
'detail-event': '$.detail.event',
},
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo",<detail-event>]}]}',
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo","<detail-event>"]}]}',
},
RoleArn: { 'Fn::GetAtt': ['TaskDefEventsRoleFB3B67B8', 'Arn'] },
Id: 'Target0',
Expand Down Expand Up @@ -361,7 +361,7 @@ test('Can use Fargate taskdef as EventRule target', () => {
InputPathsMap: {
'detail-event': '$.detail.event',
},
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo",<detail-event>]}]}',
InputTemplate: '{"containerOverrides":[{"name":"TheContainer","command":["echo","<detail-event>"]}]}',
},
RoleArn: { 'Fn::GetAtt': ['TaskDefEventsRoleFB3B67B8', 'Arn'] },
Id: 'Target0',
Expand Down
18 changes: 2 additions & 16 deletions packages/@aws-cdk/aws-events/lib/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ class FieldAwareEventInput extends RuleTargetInput {
* return a bracing that's unlikely to occur naturally (like tokens).
*/
private keyPlaceholder(key: string) {
if (this.inputType !== InputType.Object) { return `<${key}>`; }
return UNLIKELY_OPENING_STRING + key + UNLIKELY_CLOSING_STRING;
return `<${key}>`;
}

/**
Expand All @@ -230,19 +229,13 @@ class FieldAwareEventInput extends RuleTargetInput {
}
return resolved;
} else if (typeof(resolved) === 'string') {
return resolved.replace(OPENING_STRING_REGEX, '<').replace(CLOSING_STRING_REGEX, '>');
return resolved;
}
return resolved;
}
}
}

const UNLIKELY_OPENING_STRING = '<<${';
const UNLIKELY_CLOSING_STRING = '}>>';

const OPENING_STRING_REGEX = new RegExp(regexQuote('"' + UNLIKELY_OPENING_STRING), 'g');
const CLOSING_STRING_REGEX = new RegExp(regexQuote(UNLIKELY_CLOSING_STRING + '"'), 'g');

/**
* Represents a field in the event pattern
*/
Expand Down Expand Up @@ -339,10 +332,3 @@ function isEventField(x: any): x is EventField {
}

const EVENT_FIELD_SYMBOL = Symbol.for('@aws-cdk/aws-events.EventField');

/**
* Quote a string for use in a regex
*/
function regexQuote(s: string) {
return s.replace(/[.?*+^$[\]\\(){}|-]/g, '\\$&');
}
80 changes: 79 additions & 1 deletion packages/@aws-cdk/aws-events/test/test.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,85 @@ export = {
'Fn::Join': [
'',
[
'{"data":<f1>,"stackName":"',
'{"data":"<f1>","stackName":"',
{ Ref: 'AWS::StackName' },
'"}',
],
],
},
},
},
],
}));

test.done();
},

'can use joined JSON containing refs in JSON object and concat'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const rule = new Rule(stack, 'Rule', {
schedule: Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule.addTarget(new SomeTarget(RuleTargetInput.fromObject({
data: `more text ${EventField.fromPath('$')}`,
stackName: cdk.Aws.STACK_NAME,
})));

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputPathsMap: {
f1: '$',
},
InputTemplate: {
'Fn::Join': [
'',
[
'{"data":"more text <f1>","stackName":"',
{ Ref: 'AWS::StackName' },
'"}',
],
],
},
},
},
],
}));

test.done();
},

'can use joined JSON containing refs in JSON object and quotes'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const rule = new Rule(stack, 'Rule', {
schedule: Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule.addTarget(new SomeTarget(RuleTargetInput.fromObject({
data: `more text "${EventField.fromPath('$')}"`,
stackName: cdk.Aws.STACK_NAME,
})));

// THEN
expect(stack).to(haveResourceLike('AWS::Events::Rule', {
Targets: [
{
InputTransformer: {
InputPathsMap: {
f1: '$',
},
InputTemplate: {
'Fn::Join': [
'',
[
'{"data":"more text \\\"<f1>\\\"","stackName":"',
{ Ref: 'AWS::StackName' },
'"}',
],
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-events/test/test.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export = {
'InputPathsMap': {
'detail-bar': '$.detail.bar',
},
'InputTemplate': '{"foo":<detail-bar>}',
'InputTemplate': '{"foo":"<detail-bar>"}',
},
},
{
Expand Down

0 comments on commit 3076cf0

Please sign in to comment.