Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigateway): minCompressionSize on SpecRestApi #24067

Merged
merged 12 commits into from
Mar 1, 2023
34 changes: 32 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import { IVpcEndpoint } from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import { ArnFormat, CfnOutput, IResource as IResourceBase, Resource, Stack, Token, FeatureFlags, RemovalPolicy } from '@aws-cdk/core';
import { ArnFormat, CfnOutput, IResource as IResourceBase, Resource, Stack, Token, FeatureFlags, RemovalPolicy, Size } from '@aws-cdk/core';
import { APIGATEWAY_DISABLE_CLOUDWATCH_ROLE } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { ApiDefinition } from './api-definition';
Expand Down Expand Up @@ -225,9 +225,22 @@ export interface RestApiProps extends RestApiOptions {
* payload size.
*
* @default - Compression is disabled.
* @deprecated - superseded by `minCompressionSize`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳👌

*/
readonly minimumCompressionSize?: number;

/**
* A Size(in bytes, kibibytes, mebibytes etc) that is used to enable compression (with non-negative
* between 0 and 10485760 (10M) bytes, inclusive) or disable compression
* (when undefined) on an API. When compression is enabled, compression or
* decompression is not applied on the payload if the payload size is
* smaller than this value. Setting it to zero allows compression for any
* payload size.
*
* @default - Compression is disabled.
*/
readonly minCompressionSize?: Size;

/**
* The ID of the API Gateway RestApi resource that you want to clone.
*
Expand Down Expand Up @@ -261,6 +274,18 @@ export interface SpecRestApiProps extends RestApiBaseProps {
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-import-api.html
*/
readonly apiDefinition: ApiDefinition;

/**
* A Size(in bytes, kibibytes, mebibytes etc) that is used to enable compression (with non-negative
* between 0 and 10485760 (10M) bytes, inclusive) or disable compression
* (when undefined) on an API. When compression is enabled, compression or
* decompression is not applied on the payload if the payload size is
* smaller than this value. Setting it to zero allows compression for any
* payload size.
*
* @default - Compression is disabled.
*/
readonly minCompressionSize?: Size;
}

/**
Expand Down Expand Up @@ -648,6 +673,7 @@ export class SpecRestApi extends RestApiBase {
name: this.restApiName,
policy: props.policy,
failOnWarnings: props.failOnWarnings,
minimumCompressionSize: props.minCompressionSize?.toBytes(),
body: apiDefConfig.inlineDefinition ?? undefined,
bodyS3Location: apiDefConfig.inlineDefinition ? undefined : apiDefConfig.s3Location,
endpointConfiguration: this._configureEndpoints(props),
Expand Down Expand Up @@ -758,12 +784,16 @@ export class RestApi extends RestApiBase {
constructor(scope: Construct, id: string, props: RestApiProps = { }) {
super(scope, id, props);

if (props.minCompressionSize?.toBytes()! >= 0 && props.minimumCompressionSize! >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (props.minCompressionSize?.toBytes()! >= 0 && props.minimumCompressionSize! >= 0) {
if (props.minCompressionSize !== undefined && props.minimumCompressionSize !== undefined) {

throw new Error('both properties minCompressionSize and minimumCompressionSize cannot be set at once.');
}

const resource = new CfnRestApi(this, 'Resource', {
name: this.physicalName,
description: props.description,
policy: props.policy,
failOnWarnings: props.failOnWarnings,
minimumCompressionSize: props.minimumCompressionSize,
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
minimumCompressionSize: props.minCompressionSize?.toBytes() ?? props.minimumCompressionSize,
binaryMediaTypes: props.binaryMediaTypes,
endpointConfiguration: this._configureEndpoints(props),
apiKeySourceType: props.apiKeySourceType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "21.0.0",
"version": "30.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"21.0.0"}
{"version":"30.0.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "21.0.0",
"version": "30.0.0",
"testCases": {
"apigateway-restapi/DefaultTest": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"version": "21.0.0",
"version": "30.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"test-apigateway-restapi.assets": {
"type": "cdk:asset-manifest",
"properties": {
Expand All @@ -23,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -60,7 +54,7 @@
"/test-apigateway-restapi/my-api/Deployment/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
"data": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
}
],
"/test-apigateway-restapi/my-api/DeploymentStage.beta/Resource": [
Expand Down Expand Up @@ -204,7 +198,7 @@
"/test-apigateway-restapi/TestDeployment/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
"data": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
}
],
"/test-apigateway-restapi/TestStage/Resource": [
Expand All @@ -231,28 +225,19 @@
"data": "CheckBootstrapVersion"
}
],
"myapiDeployment92F2CB4963d40685c54c6f8da21d80a83f16d3d5": [
"myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca": [
{
"type": "aws:cdk:logicalId",
"data": "myapiDeployment92F2CB4963d40685c54c6f8da21d80a83f16d3d5",
"data": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
}
],
"myapiv1appliances507FEFF4": [
"TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d": [
{
"type": "aws:cdk:logicalId",
"data": "myapiv1appliances507FEFF4",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
}
],
"myapiv1appliancesGET8FE872EC": [
{
"type": "aws:cdk:logicalId",
"data": "myapiv1appliancesGET8FE872EC",
"data": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
Expand Down Expand Up @@ -307,6 +292,12 @@
]
},
"displayName": "apigateway-restapi/DefaultTest/DeployAssert"
},
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "21.0.0",
"version": "30.0.0",
"files": {
"527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049": {
"94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b": {
"source": {
"path": "test-apigateway-restapi.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049.json",
"objectKey": "94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"myapi4C7BF186": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"MinimumCompressionSize": 1024,
"Name": "my-api"
}
},
Expand Down Expand Up @@ -55,7 +56,7 @@
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca": {
"myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand Down Expand Up @@ -87,7 +88,7 @@
"CacheClusterEnabled": true,
"CacheClusterSize": "0.5",
"DeploymentId": {
"Ref": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
"Ref": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
},
"Description": "beta stage",
"MethodSettings": [
Expand Down Expand Up @@ -672,7 +673,7 @@
"MyHandlerServiceRoleFFA06653"
]
},
"TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d": {
"TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand All @@ -695,7 +696,7 @@
"Ref": "myapi4C7BF186"
},
"DeploymentId": {
"Ref": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
"Ref": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
},
"StageName": "prod"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
"id": "App",
"path": "",
"children": {
"Tree": {
"id": "Tree",
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.129"
}
},
"test-apigateway-restapi": {
"id": "test-apigateway-restapi",
"path": "test-apigateway-restapi",
Expand All @@ -26,6 +18,7 @@
"attributes": {
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
"aws:cdk:cloudformation:props": {
"minimumCompressionSize": 1024,
"name": "my-api"
}
},
Expand All @@ -38,6 +31,14 @@
"id": "CloudWatchRole",
"path": "test-apigateway-restapi/my-api/CloudWatchRole",
"children": {
"ImportCloudWatchRole": {
"id": "ImportCloudWatchRole",
"path": "test-apigateway-restapi/my-api/CloudWatchRole/ImportCloudWatchRole",
"constructInfo": {
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"Resource": {
"id": "Resource",
"path": "test-apigateway-restapi/my-api/CloudWatchRole/Resource",
Expand Down Expand Up @@ -145,7 +146,7 @@
"cacheClusterEnabled": true,
"cacheClusterSize": "0.5",
"deploymentId": {
"Ref": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
"Ref": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
},
"description": "beta stage",
"methodSettings": [
Expand Down Expand Up @@ -992,6 +993,14 @@
"id": "ServiceRole",
"path": "test-apigateway-restapi/MyHandler/ServiceRole",
"children": {
"ImportServiceRole": {
"id": "ImportServiceRole",
"path": "test-apigateway-restapi/MyHandler/ServiceRole/ImportServiceRole",
"constructInfo": {
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"Resource": {
"id": "Resource",
"path": "test-apigateway-restapi/MyHandler/ServiceRole/Resource",
Expand Down Expand Up @@ -1107,7 +1116,7 @@
"Ref": "myapi4C7BF186"
},
"deploymentId": {
"Ref": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
"Ref": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
},
"stageName": "prod"
}
Expand Down Expand Up @@ -1156,6 +1165,22 @@
"fqn": "@aws-cdk/aws-apigateway.Stage",
"version": "0.0.0"
}
},
"BootstrapVersion": {
"id": "BootstrapVersion",
"path": "test-apigateway-restapi/BootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnParameter",
"version": "0.0.0"
}
},
"CheckBootstrapVersion": {
"id": "CheckBootstrapVersion",
"path": "test-apigateway-restapi/CheckBootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnRule",
"version": "0.0.0"
}
}
},
"constructInfo": {
Expand All @@ -1176,12 +1201,30 @@
"path": "apigateway-restapi/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.129"
"version": "10.1.249"
}
},
"DeployAssert": {
"id": "DeployAssert",
"path": "apigateway-restapi/DefaultTest/DeployAssert",
"children": {
"BootstrapVersion": {
"id": "BootstrapVersion",
"path": "apigateway-restapi/DefaultTest/DeployAssert/BootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnParameter",
"version": "0.0.0"
}
},
"CheckBootstrapVersion": {
"id": "CheckBootstrapVersion",
"path": "apigateway-restapi/DefaultTest/DeployAssert/CheckBootstrapVersion",
"constructInfo": {
"fqn": "@aws-cdk/core.CfnRule",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/core.Stack",
"version": "0.0.0"
Expand All @@ -1198,6 +1241,14 @@
"fqn": "@aws-cdk/integ-tests.IntegTest",
"version": "0.0.0"
}
},
"Tree": {
"id": "Tree",
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.1.249"
}
}
},
"constructInfo": {
Expand Down
Loading