|
| 1 | +import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha'; |
| 2 | +import { Token } from 'aws-cdk-lib'; |
| 3 | +import { IEventBus } from 'aws-cdk-lib/aws-events'; |
| 4 | +import { IRole } from 'aws-cdk-lib/aws-iam'; |
| 5 | + |
| 6 | +/** |
| 7 | + * EventBridge target properties. |
| 8 | + */ |
| 9 | +export interface EventBridgeTargetParameters { |
| 10 | + /** |
| 11 | + * The input transformation to apply to the message before sending it to the target. |
| 12 | + * |
| 13 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargetparameters.html#cfn-pipes-pipe-pipetargetparameters-inputtemplate |
| 14 | + * @default - none |
| 15 | + */ |
| 16 | + readonly inputTransformation?: IInputTransformation; |
| 17 | + |
| 18 | + /** |
| 19 | + * A free-form string used to decide what fields to expect in the event detail. |
| 20 | + * |
| 21 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-detailtype |
| 22 | + * @default - none |
| 23 | + */ |
| 24 | + readonly detailType?: string; |
| 25 | + |
| 26 | + /** |
| 27 | + * The URL subdomain of the endpoint. |
| 28 | + * |
| 29 | + * @example |
| 30 | + * // if the URL for the endpoint is https://abcde.veo.endpoints.event.amazonaws.com |
| 31 | + * 'abcde.veo' |
| 32 | + * |
| 33 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-endpointid |
| 34 | + * @default - none |
| 35 | + */ |
| 36 | + readonly endpointId?: string; |
| 37 | + |
| 38 | + /** |
| 39 | + * AWS resources, identified by Amazon Resource Name (ARN), which the event primarily concerns. |
| 40 | + * |
| 41 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-resources |
| 42 | + * @default - none |
| 43 | + */ |
| 44 | + readonly resources?: string[]; |
| 45 | + |
| 46 | + /** |
| 47 | + * The source of the event. |
| 48 | + * |
| 49 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-source |
| 50 | + * @default - none |
| 51 | + */ |
| 52 | + readonly source?: string; |
| 53 | + |
| 54 | + /** |
| 55 | + * The time stamp of the event, per RFC3339. |
| 56 | + * |
| 57 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-pipes-pipe-pipetargeteventbridgeeventbusparameters.html#cfn-pipes-pipe-pipetargeteventbridgeeventbusparameters-time |
| 58 | + * @default - the time stamp of the PutEvents call |
| 59 | + */ |
| 60 | + readonly time?: string; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * An EventBridge Pipes target that sends messages to an EventBridge event bus. |
| 65 | + */ |
| 66 | +export class EventBridgeTarget implements ITarget { |
| 67 | + private eventBus: IEventBus; |
| 68 | + private eventBridgeParameters?: EventBridgeTargetParameters; |
| 69 | + public readonly targetArn: string; |
| 70 | + |
| 71 | + constructor(eventBus: IEventBus, parameters?: EventBridgeTargetParameters) { |
| 72 | + this.eventBus = eventBus; |
| 73 | + this.targetArn = eventBus.eventBusArn; |
| 74 | + if (parameters) { |
| 75 | + this.eventBridgeParameters = parameters; |
| 76 | + for (const validate of [validateDetailType, validateEndpointId, validateSource, validateTime]) { |
| 77 | + validate(parameters); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + grantPush(grantee: IRole): void { |
| 83 | + this.eventBus.grantPutEventsTo(grantee); |
| 84 | + } |
| 85 | + |
| 86 | + bind(pipe: IPipe): TargetConfig { |
| 87 | + if (!this.eventBridgeParameters) { |
| 88 | + return { |
| 89 | + targetParameters: {}, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + targetParameters: { |
| 95 | + inputTemplate: this.eventBridgeParameters.inputTransformation?.bind(pipe).inputTemplate, |
| 96 | + eventBridgeEventBusParameters: this.eventBridgeParameters, |
| 97 | + }, |
| 98 | + }; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function validateDetailType({ detailType }: EventBridgeTargetParameters) { |
| 103 | + if (detailType !== undefined && !Token.isUnresolved(detailType)) { |
| 104 | + if (detailType.length < 1 || detailType.length > 128) { |
| 105 | + throw new Error(`Detail type must be between 1 and 128 characters, received ${detailType.length}`); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +function validateEndpointId({ endpointId }: EventBridgeTargetParameters) { |
| 111 | + if (endpointId !== undefined && !Token.isUnresolved(endpointId)) { |
| 112 | + if (endpointId.length < 1 || endpointId.length > 50) { |
| 113 | + throw new Error(`Endpoint id must be between 1 and 50 characters, received ${endpointId.length}`); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function validateSource({ source }: EventBridgeTargetParameters) { |
| 119 | + if (source !== undefined && !Token.isUnresolved(source)) { |
| 120 | + if (source.length < 1 || source.length > 256) { |
| 121 | + throw new Error(`Source must be between 1 and 256 characters, received ${source.length}`); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function validateTime({ time }: EventBridgeTargetParameters) { |
| 127 | + if (time !== undefined && !Token.isUnresolved(time)) { |
| 128 | + if (time.length < 1 || time.length > 256) { |
| 129 | + throw new Error(`Time must be between 1 and 256 characters, received ${time.length}`); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments