Skip to content

Commit

Permalink
feat(aws-events): Adds EventBus resources (#4609)
Browse files Browse the repository at this point in the history
Adds the L2 resource for `CfnEventBus`
  • Loading branch information
dehli authored and mergify[bot] committed Oct 29, 2019
1 parent c770d3c commit bbec8c5
Show file tree
Hide file tree
Showing 4 changed files with 434 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ event when the pipeline changes it's state.
enables different parts of an organization to look for and process the events
that are of interest to them. A rule can customize the JSON sent to the
target, by passing only certain parts or by overwriting it with a constant.
* __EventBuses__: An event bus can receive events from your own custom applications
or it can receive events from applications and services created by AWS SaaS partners.
See [Creating an Event Bus](https://docs.aws.amazon.com/eventbridge/latest/userguide/create-event-bus.html).

The `Rule` construct defines a CloudWatch events rule which monitors an
event based on an [event
Expand Down
225 changes: 225 additions & 0 deletions packages/@aws-cdk/aws-events/lib/event-bus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import { Construct, IResource, Lazy, Resource, Stack } from '@aws-cdk/core';
import { CfnEventBus } from './events.generated';

/**
* Interface which all EventBus based classes MUST implement
*/
export interface IEventBus extends IResource {
/**
* The physical ID of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
*/
readonly eventBusName: string;

/**
* The ARN of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Arn-fn::getatt
*/
readonly eventBusArn: string;

/**
* The JSON policy of this event bus resource
*
* @attribute
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Policy-fn::getatt
*/
readonly eventBusPolicy: string;

/**
* The partner event source to associate with this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
*/
readonly eventSourceName?: string;
}

/**
* Properties to define an event bus
*/
export interface EventBusProps {
/**
* The name of the event bus you are creating
* Note: If 'eventSourceName' is passed in, you cannot set this
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
* @default - automatically generated name
*/
readonly eventBusName?: string;

/**
* The partner event source to associate with this event bus resource
* Note: If 'eventBusName' is passed in, you cannot set this
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
* @default - no partner event source
*/
readonly eventSourceName?: string;
}

/**
* Interface with properties necessary to import a reusable EventBus
*/
export interface EventBusAttributes {
/**
* The physical ID of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-name
*/
readonly eventBusName: string;

/**
* The ARN of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Arn-fn::getatt
*/
readonly eventBusArn: string;

/**
* The JSON policy of this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#Policy-fn::getatt
*/
readonly eventBusPolicy: string;

/**
* The partner event source to associate with this event bus resource
*
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-eventbus.html#cfn-events-eventbus-eventsourcename
* @default - no partner event source
*/
readonly eventSourceName?: string;
}

/**
* Define a CloudWatch EventBus
*
* @resource AWS::Events::EventBus
*/
export class EventBus extends Resource implements IEventBus {

/**
* Import an existing event bus resource
* @param scope Parent construct
* @param id Construct ID
* @param eventBusArn ARN of imported event bus
*/
public static fromEventBusArn(scope: Construct, id: string, eventBusArn: string): IEventBus {
const parts = Stack.of(scope).parseArn(eventBusArn);

class Import extends Resource implements IEventBus {
public readonly eventBusArn = eventBusArn;
public readonly eventBusName = parts.resourceName || '';
public readonly eventBusPolicy = '';
}

return new Import(scope, id);
}

/**
* Import an existing event bus resource
* @param scope Parent construct
* @param id Construct ID
* @param attrs Imported event bus properties
*/
public static fromEventBusAttributes(scope: Construct, id: string, attrs: EventBusAttributes): IEventBus {
class Import extends Resource implements IEventBus {
public readonly eventBusArn = attrs.eventBusArn;
public readonly eventBusName = attrs.eventBusName;
public readonly eventBusPolicy = attrs.eventBusPolicy;
public readonly eventSourceName = attrs.eventSourceName;
}

return new Import(scope, id);
}

private static eventBusProps(defaultEventBusName: string, props?: EventBusProps) {
if (props) {
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`
);
} else if (eventBusName !== undefined) {
if (eventBusName === 'default') {
throw new Error(
`'eventBusName' must not be 'default'`
);
} else if (eventBusName.indexOf('/') > -1) {
throw new Error(
`'eventBusName' must not contain '/'`
);
} else if (!eventBusNameRegex.test(eventBusName)) {
throw new Error(
`'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 };
}
}
return { eventBusName: defaultEventBusName };
}

/**
* The physical ID of this event bus resource
*/
public readonly eventBusName: string;

/**
* The ARN of the event bus, such as:
* arn:aws:events:us-east-2:123456789012:event-bus/aws.partner/PartnerName/acct1/repo1.
*/
public readonly eventBusArn: string;

/**
* The policy for the event bus in JSON form.
*/
public readonly eventBusPolicy: string;

/**
* The name of the partner event source
*/
public readonly eventSourceName?: string;

constructor(scope: Construct, id: string, props?: EventBusProps) {
const { eventBusName, eventSourceName } = EventBus.eventBusProps(
Lazy.stringValue({ produce: () => this.node.uniqueId }),
props
);

super(scope, id, { physicalName: eventBusName });

const eventBus = new CfnEventBus(this, 'Resource', {
name: eventBusName,
eventSourceName
});

this.eventBusArn = this.getResourceArnAttribute(eventBus.attrArn, {
service: 'events',
resource: 'event-bus',
resourceName: eventBus.name
});

this.eventBusName = this.getResourceNameAttribute(eventBus.ref);
this.eventBusPolicy = eventBus.attrPolicy;
this.eventSourceName = eventBus.eventSourceName;
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './input';
export * from './rule';
export * from './rule-ref';
export * from './target';
export * from './event-bus';
export * from './event-pattern';
export * from './schedule';
export * from './on-event-options';
Expand Down
Loading

0 comments on commit bbec8c5

Please sign in to comment.