Skip to content

Commit

Permalink
fix(events-targets): ApiGateway events target should accept IRestApi
Browse files Browse the repository at this point in the history
issue: #16423
  • Loading branch information
Neel Krishna committed Mar 7, 2024
1 parent 9c82bca commit f8f83f4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/aws-cdk-lib/aws-events-targets/lib/api-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export interface ApiGatewayProps extends TargetBaseProps {
*/
export class ApiGateway implements events.IRuleTarget {

constructor(public readonly restApi: api.RestApi, private readonly props?: ApiGatewayProps) {
/**
* @param restApi - IRestApi implementation to use as event target
* @param props - Properties to configure the APIGatway target
*/
constructor(public readonly restApi: api.IRestApi, private readonly props?: ApiGatewayProps) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,62 @@ import * as sqs from '../../../aws-sqs';
import * as cdk from '../../../core';
import * as targets from '../../lib';

test('use a SpecRestApi APIGateway event rule target', () => {
// GIVEN
const stack = new cdk.Stack();
const specRestApi = newTestSpecRestApi(stack);
const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule.addTarget(new targets.ApiGateway(specRestApi));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
Targets: [
{
Arn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':execute-api:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':',
{
Ref: 'MySpecRestApiFB7DB2AB',
},
'/',
{
Ref: 'MySpecRestApiDeploymentStageprod8522A503',
},
'/*/',
],
],
},
HttpParameters: {},
Id: 'Target0',
RoleArn: {
'Fn::GetAtt': [
'MySpecRestApiEventsRole25C1D10F',
'Arn',
],
},
},
],
});
});

test('use api gateway rest api as an event rule target', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -261,3 +317,12 @@ function newTestRestApi(scope: constructs.Construct, suffix = '') {
handler: lambdaFunctin,
} );
}

function newTestSpecRestApi(scope: constructs.Construct, suffix = '') {
return new api.SpecRestApi( scope, `MySpecRestApi${suffix}`, {
apiDefinition: api.ApiDefinition.fromInline({
openapi: '3.0.2',
paths: {},
}),
} );
}

0 comments on commit f8f83f4

Please sign in to comment.