From 93b7aa090ef4b4a2d09892b67cfc331c77a84053 Mon Sep 17 00:00:00 2001 From: Bardur Sigmundarson Date: Thu, 14 Dec 2023 16:11:00 +0100 Subject: [PATCH] add $or comparison to the EventPattern type --- .../aws-events/lib/event-pattern.ts | 8 ++ .../aws-cdk-lib/aws-events/test/rule.test.ts | 75 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/packages/aws-cdk-lib/aws-events/lib/event-pattern.ts b/packages/aws-cdk-lib/aws-events/lib/event-pattern.ts index 93076c7b51726..2630f4361eb41 100644 --- a/packages/aws-cdk-lib/aws-events/lib/event-pattern.ts +++ b/packages/aws-cdk-lib/aws-events/lib/event-pattern.ts @@ -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 + */ + $or?: EventPattern[]; } diff --git a/packages/aws-cdk-lib/aws-events/test/rule.test.ts b/packages/aws-cdk-lib/aws-events/test/rule.test.ts index 80f3c5cbe53d6..2257c6858a0ce 100644 --- a/packages/aws-cdk-lib/aws-events/test/rule.test.ts +++ b/packages/aws-cdk-lib/aws-events/test/rule.test.ts @@ -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');