Skip to content

Commit

Permalink
Avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
WtfJoke committed Nov 29, 2023
1 parent 5654560 commit 21e3410
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IScheduleTarget, ISchedule, ScheduleTargetInput, ScheduleTargetConfig }
import { Names } from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import { IRole } from 'aws-cdk-lib/aws-iam';
import * as iam from 'aws-cdk-lib/aws-iam';
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
import { sameEnvDimension } from './util';

Expand Down Expand Up @@ -56,7 +55,7 @@ export class EventBridgePutEvents extends ScheduleTargetBase implements ISchedul
private readonly props: ScheduleTargetBaseProps,
) {
super(props, entry.eventBus.eventBusArn);
if (this.baseProps.input) {
if (this.props.input) {
throw new Error('ScheduleTargetBaseProps.input is not supported for EventBridgePutEvents. Please use entry.detail instead.');
}
}
Expand All @@ -80,26 +79,12 @@ export class EventBridgePutEvents extends ScheduleTargetBase implements ISchedul
}

protected bindBaseTargetConfig(_schedule: ISchedule): ScheduleTargetConfig {
const role: iam.IRole = this.baseProps.role ?? this.singletonScheduleRole(_schedule, this.targetArn);
this.addTargetActionToRole(_schedule, role);

if (this.baseProps.deadLetterQueue) {
this.addToDeadLetterQueueResourcePolicy(_schedule, this.baseProps.deadLetterQueue);
}

const { source, detailType, detail } = this.entry;

return {
arn: this.targetArn,
role,
deadLetterConfig: this.baseProps.deadLetterQueue ? {
arn: this.baseProps.deadLetterQueue.queueArn,
} : undefined,
retryPolicy: this.renderRetryPolicy(this.baseProps.maxEventAge, this.baseProps.retryAttempts),
input: detail,
...super.bindBaseTargetConfig(_schedule),
input: this.entry.detail,
eventBridgeParameters: {
detailType,
source,
detailType: this.entry.detailType,
source: this.entry.source,
},
};
}
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ScheduleTargetBaseProps {
export abstract class ScheduleTargetBase {

constructor(
protected readonly baseProps: ScheduleTargetBaseProps,
private readonly baseProps: ScheduleTargetBaseProps,
protected readonly targetArn: string,
) {
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export abstract class ScheduleTargetBase {
/**
* Create a return a Schedule Target Configuration for the given schedule
* @param schedule
* @returnn
* @returns a Schedule Target Configuration
*/
bind(schedule: ISchedule): ScheduleTargetConfig {
return this.bindBaseTargetConfig(schedule);
Expand All @@ -109,7 +109,7 @@ export abstract class ScheduleTargetBase {
* If a role already exists, it will be returned. This ensures that if multiple
* events have the same target, they will share a role.
*/
protected singletonScheduleRole(schedule: ISchedule, targetArn: string): iam.IRole {
private singletonScheduleRole(schedule: ISchedule, targetArn: string): iam.IRole {
const stack = Stack.of(schedule);
const arn = Token.isUnresolved(targetArn) ? stack.resolve(targetArn).toString() : targetArn;
const hash = md5hash(arn).slice(0, 6);
Expand Down Expand Up @@ -141,7 +141,7 @@ export abstract class ScheduleTargetBase {
* @param schedule schedule to add DLQ to
* @param queue the DLQ
*/
protected addToDeadLetterQueueResourcePolicy(schedule: ISchedule, queue: sqs.IQueue) {
private addToDeadLetterQueueResourcePolicy(schedule: ISchedule, queue: sqs.IQueue) {
if (!sameEnvDimension(schedule.env.region, queue.env.region)) {
throw new Error(`Cannot assign Dead Letter Queue in region ${queue.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the queue and the schedule must be in the same region.`);
}
Expand All @@ -164,7 +164,7 @@ export abstract class ScheduleTargetBase {
}
}

protected renderRetryPolicy(maximumEventAge: Duration | undefined, maximumRetryAttempts: number | undefined): CfnSchedule.RetryPolicyProperty {
private renderRetryPolicy(maximumEventAge: Duration | undefined, maximumRetryAttempts: number | undefined): CfnSchedule.RetryPolicyProperty {
const maxMaxAge = Duration.days(1).toSeconds();
const minMaxAge = Duration.minutes(15).toSeconds();
let maxAge: number = maxMaxAge;
Expand Down

0 comments on commit 21e3410

Please sign in to comment.