From 21e3410bf6a2cf55782e6b0f1359b65754228e16 Mon Sep 17 00:00:00 2001 From: Manuel Date: Sat, 28 Oct 2023 14:13:03 +0200 Subject: [PATCH] Avoid duplication --- .../lib/event-bridge-put-events.ts | 25 ++++--------------- .../aws-scheduler-targets-alpha/lib/target.ts | 10 ++++---- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/event-bridge-put-events.ts b/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/event-bridge-put-events.ts index c6db58774e66e..9f7697a968f7b 100644 --- a/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/event-bridge-put-events.ts +++ b/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/event-bridge-put-events.ts @@ -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'; @@ -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.'); } } @@ -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, }, }; } diff --git a/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts b/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts index 574c44814d586..936742d54a819 100644 --- a/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts +++ b/packages/@aws-cdk/aws-scheduler-targets-alpha/lib/target.ts @@ -68,7 +68,7 @@ export interface ScheduleTargetBaseProps { export abstract class ScheduleTargetBase { constructor( - protected readonly baseProps: ScheduleTargetBaseProps, + private readonly baseProps: ScheduleTargetBaseProps, protected readonly targetArn: string, ) { } @@ -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); @@ -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); @@ -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.`); } @@ -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;