Skip to content

Commit

Permalink
fix(api-gateway): SpecRestApi ignores disableExecuteApiEndpoint prope…
Browse files Browse the repository at this point in the history
…rty (#22133)

In this [PR](#14526) the disableExecuteApiEndpoint property was added to RestApiBaseProps, which is used by both RestApi and SpecRestApi. The property is propagated to the resulting CfnRestApi when specified for a RestApi, but is ignored when specified for a SpecRestApi.

Closes #21295.


----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
TheRealAmazonKendra committed Sep 20, 2022
1 parent 35b2806 commit a4364ce
Show file tree
Hide file tree
Showing 12 changed files with 2,431 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ export class SpecRestApi extends RestApiBase {
bodyS3Location: apiDefConfig.inlineDefinition ? undefined : apiDefConfig.s3Location,
endpointConfiguration: this._configureEndpoints(props),
parameters: props.parameters,
disableExecuteApiEndpoint: props.disableExecuteApiEndpoint,
});

props.apiDefinition.bindAfterCreate(this, this);
Expand Down
92 changes: 92 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/integ.spec-restapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { IntegTest } from '@aws-cdk/integ-tests';
import * as apigateway from '../lib';

class Test extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);

const api = new apigateway.SpecRestApi(this, 'my-api', {
apiDefinition: apigateway.ApiDefinition.fromAsset(path.join(__dirname, 'sample-definition.yaml')),
disableExecuteApiEndpoint: true,
retainDeployments: true,
cloudWatchRole: true,
deployOptions: {
cacheClusterEnabled: true,
stageName: 'beta',
description: 'beta stage',
loggingLevel: apigateway.MethodLoggingLevel.INFO,
dataTraceEnabled: true,
methodOptions: {
'/api/appliances/GET': {
cachingEnabled: true,
},
},
},
});

const handler = new lambda.Function(this, 'MyHandler', {
runtime: lambda.Runtime.NODEJS_14_X,
code: lambda.Code.fromInline(`exports.handler = ${handlerCode}`),
handler: 'index.handler',
});

const v1 = api.root.addResource('v1');

const integration = new apigateway.LambdaIntegration(handler);

const toys = v1.addResource('toys');
const getToysMethod: apigateway.Method = toys.addMethod('GET', integration, { apiKeyRequired: true });
toys.addMethod('POST');
toys.addMethod('PUT');

const appliances = v1.addResource('appliances');
appliances.addMethod('GET');

const books = v1.addResource('books');
books.addMethod('GET', integration);
books.addMethod('POST', integration);

function handlerCode(event: any, _: any, callback: any) {
return callback(undefined, {
isBase64Encoded: false,
statusCode: 200,
headers: { 'content-type': 'application/json' },
body: JSON.stringify(event),
});
}

const key = api.addApiKey('ApiKey');
const plan = api.addUsagePlan('UsagePlan', {
name: 'Basic',
apiKey: key,
description: 'Free tier monthly usage plan',
throttle: { rateLimit: 5 },
quota: {
limit: 10000,
period: apigateway.Period.MONTH,
},
});
plan.addApiStage({
stage: api.deploymentStage,
throttle: [
{
method: getToysMethod,
throttle: {
rateLimit: 10,
burstLimit: 2,
},
},
],
});
}
}

const app = new cdk.App();

const testCase = new Test(app, 'test-apigateway-spec-restapi');
new IntegTest(app, 'apigateway-spec-restapi', {
testCases: [testCase],
});
49 changes: 48 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/restapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,21 @@ describe('restapi', () => {
});
});

test('"disableExecuteApiEndpoint" can disable the default execute-api endpoint', () => {
test('disableExecuteApiEndpoint is false when set to false in RestApi', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.RestApi(stack, 'my-api', { disableExecuteApiEndpoint: false });
api.root.addMethod('GET');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
DisableExecuteApiEndpoint: false,
});
});

test('disableExecuteApiEndpoint is true when set to true in RestApi', () => {
// GIVEN
const stack = new Stack();

Expand All @@ -1164,6 +1178,39 @@ describe('restapi', () => {
});
});

test('disableExecuteApiEndpoint is false when set to false in SpecRestApi', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.SpecRestApi(stack, 'my-api', {
apiDefinition: apigw.ApiDefinition.fromInline({ foo: 'bar' }),
disableExecuteApiEndpoint: false,
});
api.root.addMethod('GET');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
DisableExecuteApiEndpoint: false,
});
});

test('disableExecuteApiEndpoint is true when set to true in SpecRestApi', () => {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigw.SpecRestApi(stack, 'my-api', {
apiDefinition: apigw.ApiDefinition.fromInline({ foo: 'bar' }),
disableExecuteApiEndpoint: true,
});
api.root.addMethod('GET');

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::RestApi', {
DisableExecuteApiEndpoint: true,
});
});

describe('Description', () => {
test('description can be set', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "21.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
"path": "apigatewayspecrestapiDefaultTestDeployAssertD16AA485.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: "3.0.2"
info:
version: 1.0.0
title: Test API for CDK
paths:
/pets:
get:
summary: Test Method
operationId: testMethod
responses:
"200":
description: A paged array of pets
content:
application/json:
schema:
$ref: "#/components/schemas/Empty"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: when_no_match
type: mock

components:
schemas:
Empty:
title: Empty Schema
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"21.0.0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "21.0.0",
"testCases": {
"apigateway-spec-restapi/DefaultTest": {
"stacks": [
"test-apigateway-spec-restapi"
],
"assertionStack": "apigateway-spec-restapi/DefaultTest/DeployAssert",
"assertionStackName": "apigatewayspecrestapiDefaultTestDeployAssertD16AA485"
}
}
}
Loading

0 comments on commit a4364ce

Please sign in to comment.