Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(events): detailType doesnt allow filtering patterns #30222

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@
]
},
"detail-type": [
"detailType1"
"detailType1",
{
"prefix": "detailPrefix"
}
],
"id": [
"id1",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ new Rule(stack, 'MyRule', {
suffix: Match.suffix('.com'),
equalsIgnoreCase: Match.equalsIgnoreCase('ignore case'),
},
detailType: ['detailType1'],
detailType: ['detailType1', { prefix: 'detailPrefix' }],
id: ['id1', 'id2'],
region: ['region1', 'region2', 'region3'],
resources: ['r1'],
Expand Down
6 changes: 4 additions & 2 deletions packages/aws-cdk-lib/aws-events/lib/event-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ export interface EventPattern {

/**
* Identifies, in combination with the source field, the fields and values
* that appear in the detail field.
* that appear in the detail field. Can be a list of strings or JSON event
* filtering patterns.
*
* Represents the "detail-type" event field.
*
* @see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html
* @default - No filtering on detail type
*/
readonly detailType?: string[];
readonly detailType?: (string|{ [key: string]: any })[];

/**
* Identifies the service that sourced the event. All events sourced from
Expand Down
55 changes: 55 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,30 @@ describe('rule', () => {
});
});

test('eventPattern detailType supports filtering patterns as inputs', () => {
const stack = new cdk.Stack();

new Rule(stack, 'MyRule', {
eventPattern: {
detailType: [{ 'prefix': 'detailType1' }, 'anotherDetailType', { 'anything-but': 'initializing' }],
},
});

Template.fromStack(stack).templateMatches({
'Resources': {
'MyRuleA44AB831': {
'Type': 'AWS::Events::Rule',
'Properties': {
'EventPattern': {
'detail-type': [{ 'prefix': 'detailType1' }, 'anotherDetailType', { 'anything-but': 'initializing' }],
},
'State': 'ENABLED',
},
},
},
});
});

test('fails synthesis if neither eventPattern nor scheduleExpression are specified', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
Expand Down Expand Up @@ -317,6 +341,37 @@ describe('rule', () => {
});
});

test('addEventPattern can support event matching patterns in detailType', () => {
const stack = new cdk.Stack();

const rule = new Rule(stack, 'MyRule');
rule.addEventPattern({
detailType: [{ 'prefix': 'event_prefix' }],
});

rule.addEventPattern({
detailType: ['EC2 Instance State-change Notification', 'AWS API Call via CloudTrail'],
});

Template.fromStack(stack).templateMatches({
'Resources': {
'MyRuleA44AB831': {
'Type': 'AWS::Events::Rule',
'Properties': {
'EventPattern': {
'detail-type': [{
'prefix': 'event_prefix',
},
'EC2 Instance State-change Notification',
'AWS API Call via CloudTrail'],
},
'State': 'ENABLED',
},
},
},
});
});

test('targets can be added via props or addTarget with input transformer', () => {
const stack = new cdk.Stack();
const t1: IRuleTarget = {
Expand Down
Loading