Skip to content

Commit

Permalink
add $or comparison to the EventPattern type
Browse files Browse the repository at this point in the history
  • Loading branch information
b-dur committed Dec 14, 2023
1 parent de0d77b commit f223017
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-events/lib/event-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,12 @@ export interface EventPattern {
* @default - No filtering on detail
*/
readonly detail?: { [key: string]: any };

/**
* Comparison operator to use on the root and/or leaf level for multiple
* fields OR comparison.
*
* @default - No OR operator
*/
readonly $or?: EventPattern[];
}
75 changes: 75 additions & 0 deletions packages/aws-cdk-lib/aws-events/test/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,81 @@ describe('rule', () => {
});
});

test('eventPattern with multi-field $or operator is rendered properly', () => {
const stack = new cdk.Stack();

new Rule(stack, 'MyRule', {
eventPattern: {
$or: [
{
account: ['account1'],
detail: {
$or: [
{
rangeMatcher: [{ numeric: ['>=', -1, '<=', 1] }],
stringMatcher: ['I am just a string'],
prefixMatcher: [{ prefix: 'aws.' }],
ipAddress: [{ cidr: '192.0.2.0/24' }],
shouldExist: [{ exists: true }],
shouldNotExist: [{ exists: false }],
}, {
foo: [1],
strings: ['foo'],
},
],
},
},
{
account: ['account2'],
detail: {
foo: [2],
strings: ['bar'],
},
},
],
},
});

Template.fromStack(stack).templateMatches({
'Resources': {
'MyRuleA44AB831': {
'Type': 'AWS::Events::Rule',
'Properties': {
'EventPattern': {
$or: [
{
account: ['account1'],
detail: {
$or: [
{
rangeMatcher: [{ numeric: ['>=', -1, '<=', 1] }],
stringMatcher: ['I am just a string'],
prefixMatcher: [{ prefix: 'aws.' }],
ipAddress: [{ cidr: '192.0.2.0/24' }],
shouldExist: [{ exists: true }],
shouldNotExist: [{ exists: false }],
}, {
foo: [1],
strings: ['foo'],
},
],
},
},
{
account: ['account2'],
detail: {
foo: [2],
strings: ['bar'],
},
},
],
},
},
},
},
});
});

test('fails synthesis if neither eventPattern nor scheduleExpression are specified', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
Expand Down

0 comments on commit f223017

Please sign in to comment.