From 126f6273491d5e64eb928ceb0671e871323ae916 Mon Sep 17 00:00:00 2001 From: Matt Berry Date: Fri, 17 Sep 2021 18:01:01 +0000 Subject: [PATCH] fix(aws-events-targets): Consume IRestApi as target (#16423) Fixes: aws#16423 When creating an Event Bridge target for an API Gateway, the superinterface IRestApi should be consumed instead of the concrete class RestApi. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-events-targets/lib/api-gateway.ts | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/@aws-cdk/aws-events-targets/lib/api-gateway.ts b/packages/@aws-cdk/aws-events-targets/lib/api-gateway.ts index 168c5a65a386d..91abe5253d43d 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/api-gateway.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/api-gateway.ts @@ -70,11 +70,14 @@ export interface ApiGatewayProps extends TargetBaseProps { } /** - * Use an API Gateway REST APIs as a target for Amazon EventBridge rules. + * Use an API Gateway REST API as a target for Amazon EventBridge rules. */ -export class ApiGateway implements events.IRuleTarget { +export class ApiGatewayTarget implements events.IRuleTarget { - constructor(public readonly restApi: api.RestApi, private readonly props?: ApiGatewayProps) { + /** + * @param iRestApi - An implementation of a Rest API to send events to + */ + constructor(public readonly iRestApi: api.IRestApi, protected readonly props?: ApiGatewayProps) { } /** @@ -93,15 +96,15 @@ export class ApiGateway implements events.IRuleTarget { throw new Error('The number of wildcards in the path does not match the number of path pathParameterValues.'); } - const restApiArn = this.restApi.arnForExecuteApi( + const restApiArn = this.iRestApi.arnForExecuteApi( this.props?.method, this.props?.path || '/', - this.props?.stage || this.restApi.deploymentStage.stageName, + this.props?.stage || this.iRestApi.deploymentStage.stageName, ); return { ...(this.props ? bindBaseTargetConfig(this.props) : {}), arn: restApiArn, - role: this.props?.eventRole || singletonEventRole(this.restApi, [new iam.PolicyStatement({ + role: this.props?.eventRole || singletonEventRole(this.iRestApi, [new iam.PolicyStatement({ resources: [restApiArn], actions: [ 'execute-api:Invoke', @@ -110,7 +113,7 @@ export class ApiGateway implements events.IRuleTarget { })]), deadLetterConfig: this.props?.deadLetterQueue && { arn: this.props.deadLetterQueue?.queueArn }, input: this.props?.postBody, - targetResource: this.restApi, + targetResource: this.iRestApi, httpParameters: { headerParameters: this.props?.headerParameters, queryStringParameters: this.props?.queryStringParameters, @@ -121,3 +124,19 @@ export class ApiGateway implements events.IRuleTarget { } +/** + * Use an API Gateway REST APIs as a target for Amazon EventBridge rules. + * + * @deprecated - Use ApiGatewayTarget + */ +export class ApiGateway extends ApiGatewayTarget { + + /** + * @param restApi - A Rest API to send events to + */ + constructor(public readonly restApi: api.RestApi, protected readonly props?: ApiGatewayProps) { + super(restApi, props); + } + +} +