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): event bus name only generated if no props passed #18153

Merged
merged 4 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 28 additions & 25 deletions packages/@aws-cdk/aws-events/lib/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,18 @@ export class EventBus extends EventBusBase {
});
}

private static eventBusProps(defaultEventBusName: string, props?: EventBusProps) {
if (props) {
const { eventBusName, eventSourceName } = props;
const eventBusNameRegex = /^[\/\.\-_A-Za-z0-9]{1,256}$/;
private static eventBusProps(defaultEventBusName: string, props: EventBusProps = {}) {
const { eventBusName, eventSourceName } = props;
const eventBusNameRegex = /^[\/\.\-_A-Za-z0-9]{1,256}$/;

if (eventBusName !== undefined && eventSourceName !== undefined) {
throw new Error(
'\'eventBusName\' and \'eventSourceName\' cannot both be provided',
);
}

if (eventBusName !== undefined && eventSourceName !== undefined) {
throw new Error(
'\'eventBusName\' and \'eventSourceName\' cannot both be provided',
);
} else if (eventBusName !== undefined && !Token.isUnresolved(eventBusName)) {
if (eventBusName !== undefined) {
if (!Token.isUnresolved(eventBusName)) {
if (eventBusName === 'default') {
throw new Error(
'\'eventBusName\' must not be \'default\'',
Expand All @@ -262,24 +264,25 @@ export class EventBus extends EventBusBase {
`'eventBusName' must satisfy: ${eventBusNameRegex}`,
);
}
return { eventBusName };
} else if (eventSourceName !== undefined) {
// Ex: aws.partner/PartnerName/acct1/repo1
const eventSourceNameRegex = /^aws\.partner(\/[\.\-_A-Za-z0-9]+){2,}$/;
if (!eventSourceNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventSourceNameRegex}`,
);
} else if (!eventBusNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventBusNameRegex}`,
);
}
return { eventBusName: eventSourceName, eventSourceName };
} else {
return { eventBusName: props.eventBusName };
}
return { eventBusName };
}

if (eventSourceName !== undefined) {
// Ex: aws.partner/PartnerName/acct1/repo1
const eventSourceNameRegex = /^aws\.partner(\/[\.\-_A-Za-z0-9]+){2,}$/;
if (!eventSourceNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventSourceNameRegex}`,
);
} else if (!eventBusNameRegex.test(eventSourceName)) {
throw new Error(
`'eventSourceName' must satisfy: ${eventBusNameRegex}`,
);
}
return { eventBusName: eventSourceName, eventSourceName };
}

return { eventBusName: defaultEventBusName };
}

Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-events/test/event-bus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ describe('event bus', () => {
expect(stack).toHaveResource('AWS::Events::EventBus', {
Name: 'Bus',
});
});

test('default event bus with empty props object', () => {
// GIVEN
const stack = new Stack();

// WHEN
new EventBus(stack, 'Bus', {});

// THEN
expect(stack).toHaveResource('AWS::Events::EventBus', {
Name: 'Bus',
});
});

test('named event bus', () => {
Expand Down