Skip to content

Commit

Permalink
Add feedback from @kaizencc
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Sep 5, 2024
1 parent 532dd3b commit d7912d1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 8 deletions.
7 changes: 4 additions & 3 deletions packages/@aws-cdk/aws-pipes-targets-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following targets are supported:
1. `targets.SqsTarget`: [Send event source to a Queue](#amazon-sqs)
2. `targets.SfnStateMachine`: [Invoke a State Machine from an event source](#aws-step-functions-state-machine)
3. `targets.LambdaFunction`: [Send event source to a Lambda Function](#aws-lambda-function)
4. `targets.EventBridgeTarget`: [Send event soruce to an EventBridge event bus](#amazon-eventbridge-event-bus)

### Amazon SQS

Expand Down Expand Up @@ -78,7 +79,7 @@ A State Machine can be used as a target for a pipe. The State Machine will be in
declare const sourceQueue: sqs.Queue;
declare const targetStateMachine: sfn.IStateMachine;

const pipeTarget = new targets.SfnStateMachine(targetStateMachine,{});
const pipeTarget = new targets.SfnStateMachine(targetStateMachine);

const pipe = new pipes.Pipe(this, 'Pipe', {
source: new SomeSource(sourceQueue),
Expand Down Expand Up @@ -132,7 +133,7 @@ A Lambda Function can be used as a target for a pipe. The Lambda Function will b
declare const sourceQueue: sqs.Queue;
declare const targetFunction: lambda.IFunction;

const pipeTarget = new targets.LambdaFunction(targetFunction,{});
const pipeTarget = new targets.LambdaFunction(targetFunction);

const pipe = new pipes.Pipe(this, 'Pipe', {
source: new SomeSource(sourceQueue),
Expand Down Expand Up @@ -180,7 +181,7 @@ An event bus can be used as a target for a pipe. The event bus will receive the
declare const sourceQueue: sqs.Queue;
declare const targetEventBus: events.EventBus;

const eventBusTarget = new targets.EventBridgeTarget(targetEventBus, {});
const eventBusTarget = new targets.EventBridgeTarget(targetEventBus);

const pipe = new pipes.Pipe(this, 'Pipe', {
source: new SomeSource(sourceQueue),
Expand Down
10 changes: 6 additions & 4 deletions packages/@aws-cdk/aws-pipes-targets-alpha/lib/event-bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IInputTransformation, IPipe, ITarget, TargetConfig } from '@aws-cdk/aws-pipes-alpha';
import { IEventBus } from 'aws-cdk-lib/aws-events';
import { IRole } from 'aws-cdk-lib/aws-iam';
import { Token } from 'aws-cdk-lib';

/**
* EventBridge target properties.
Expand Down Expand Up @@ -66,6 +67,7 @@ export class EventBridgeTarget implements ITarget {
private eventBus: IEventBus;
private eventBridgeParameters?: EventBridgeTargetParameters;
public readonly targetArn: string;

constructor(eventBus: IEventBus, parameters?: EventBridgeTargetParameters) {
this.eventBus = eventBus;
this.targetArn = eventBus.eventBusArn;
Expand Down Expand Up @@ -98,31 +100,31 @@ export class EventBridgeTarget implements ITarget {
}

function validateDetailType({ detailType }: EventBridgeTargetParameters) {
if (detailType !== undefined) {
if (detailType !== undefined && !Token.isUnresolved(detailType)) {
if (detailType.length < 1 || detailType.length > 128) {
throw new Error(`Detail type must be between 1 and 128 characters, received ${detailType.length}`);
}
}
}

function validateEndpointId({ endpointId }: EventBridgeTargetParameters) {
if (endpointId !== undefined) {
if (endpointId !== undefined && !Token.isUnresolved(endpointId)) {
if (endpointId.length < 1 || endpointId.length > 50) {
throw new Error(`Endpoint id must be between 1 and 50 characters, received ${endpointId.length}`);
}
}
}

function validateSource({ source }: EventBridgeTargetParameters) {
if (source !== undefined) {
if (source !== undefined && !Token.isUnresolved(source)) {
if (source.length < 1 || source.length > 256) {
throw new Error(`Source must be between 1 and 256 characters, received ${source.length}`);
}
}
}

function validateTime({ time }: EventBridgeTargetParameters) {
if (time !== undefined) {
if (time !== undefined && !Token.isUnresolved(time)) {
if (time.length < 1 || time.length > 256) {
throw new Error(`Time must be between 1 and 256 characters, received ${time.length}`);
}
Expand Down
118 changes: 117 additions & 1 deletion packages/@aws-cdk/aws-pipes-targets-alpha/test/event-bridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InputTransformation, Pipe } from '@aws-cdk/aws-pipes-alpha';
import { App, Stack } from 'aws-cdk-lib';
import { App, Lazy, Stack } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { EventBus } from 'aws-cdk-lib/aws-events';
import { TestSource } from './test-classes';
Expand Down Expand Up @@ -235,4 +235,120 @@ describe('EventBridge source parameters validation', () => {
});
}).toThrow('Time must be between 1 and 256 characters, received 257');
});

test('Detail type can be given for a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const bus = new EventBus(stack, 'MyEventBus', {});
const detailType = Lazy.string({ produce: () => '20' });

const target = new EventBridgeTarget(bus, {
detailType,
});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// WHEN
template.hasResourceProperties('AWS::Pipes::Pipe', {
TargetParameters: {
EventBridgeEventBusParameters: {
DetailType: '20',
},
},
});
});

test('Endpoint can be given for a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const bus = new EventBus(stack, 'MyEventBus', {});
const endpointId = Lazy.string({ produce: () => 'abcde.veo' });

const target = new EventBridgeTarget(bus, {
endpointId,
});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// WHEN
template.hasResourceProperties('AWS::Pipes::Pipe', {
TargetParameters: {
EventBridgeEventBusParameters: {
EndpointId: 'abcde.veo',
},
},
});
});

test('Source can be given for a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const bus = new EventBus(stack, 'MyEventBus', {});
const source = Lazy.string({ produce: () => 'mySource' });

const target = new EventBridgeTarget(bus, {
source,
});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// WHEN
template.hasResourceProperties('AWS::Pipes::Pipe', {
TargetParameters: {
EventBridgeEventBusParameters: {
Source: 'mySource',
},
},
});
});

test('Time can be given for a token', () => {
// GIVEN
const app = new App();
const stack = new Stack(app, 'TestStack');
const bus = new EventBus(stack, 'MyEventBus', {});
const time = Lazy.string({ produce: () => '1234' });

const target = new EventBridgeTarget(bus, {
time,
});

new Pipe(stack, 'MyPipe', {
source: new TestSource(),
target,
});

// ACT
const template = Template.fromStack(stack);

// WHEN
template.hasResourceProperties('AWS::Pipes::Pipe', {
TargetParameters: {
EventBridgeEventBusParameters: {
Time: '1234',
},
},
});
});
});

0 comments on commit d7912d1

Please sign in to comment.