From b23ee1be9381f0f927e43c4214e91aafcdf03899 Mon Sep 17 00:00:00 2001 From: Hari Prakash <73071214+hariprakash-j@users.noreply.github.com> Date: Tue, 5 Dec 2023 03:20:52 +0000 Subject: [PATCH] fix(apigateway): move url property to RestApiBase (#27742) Moved the url property from RestApi class to the RestApiBase to have the url property on all derived class. This will fix the problem mentioned in the issue where SpecRestApi does not work with the RestApiOrigin construct. PS. I could not perform the integration tests as the free tier on my personal account expired. Closes #27501. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../cdk.out | 1 + ...-rest-api-origin-custom-origin.assets.json | 19 + ...est-api-origin-custom-origin.template.json | 202 +++++++++++ .../integ.json | 12 + .../manifest.json | 137 +++++++ ...efaultTestDeployAssertCD227A2A.assets.json | 19 + ...aultTestDeployAssertCD227A2A.template.json | 36 ++ .../tree.json | 334 ++++++++++++++++++ ...nteg.spec-rest-api-origin-custom-origin.ts | 69 ++++ .../aws-cdk-lib/aws-apigateway/lib/restapi.ts | 14 +- .../aws-apigateway/test/restapi.test.ts | 141 +++++--- .../lib/rest-api-origin.ts | 2 +- 12 files changed, 936 insertions(+), 50 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/cdk.out new file mode 100644 index 0000000000000..2313ab5436501 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"34.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.assets.json new file mode 100644 index 0000000000000..c8570b0f86f57 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.assets.json @@ -0,0 +1,19 @@ +{ + "version": "34.0.0", + "files": { + "aaeb60e3ccc35e5d7d500c55948cc2eb600ffd3a692d7e77f0be3e7cb2939ea9": { + "source": { + "path": "integ-spec-rest-api-origin-custom-origin.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "aaeb60e3ccc35e5d7d500c55948cc2eb600ffd3a692d7e77f0be3e7cb2939ea9.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.template.json new file mode 100644 index 0000000000000..e5a1df9cd9b06 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ-spec-rest-api-origin-custom-origin.template.json @@ -0,0 +1,202 @@ +{ + "Resources": { + "RestApi0C43BF4B": { + "Type": "AWS::ApiGateway::RestApi", + "Properties": { + "Body": { + "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" + } + } + } + }, + "Name": "RestApi" + } + }, + "RestApiDeployment180EC503edd77c5c8fffabb2e523387cd087d12f": { + "Type": "AWS::ApiGateway::Deployment", + "Properties": { + "Description": "Automatically created by the RestApi construct", + "RestApiId": { + "Ref": "RestApi0C43BF4B" + } + } + }, + "RestApiDeploymentStageprod3855DE66": { + "Type": "AWS::ApiGateway::Stage", + "Properties": { + "DeploymentId": { + "Ref": "RestApiDeployment180EC503edd77c5c8fffabb2e523387cd087d12f" + }, + "RestApiId": { + "Ref": "RestApi0C43BF4B" + }, + "StageName": "prod" + } + }, + "Distribution830FAC52": { + "Type": "AWS::CloudFront::Distribution", + "Properties": { + "DistributionConfig": { + "DefaultCacheBehavior": { + "CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "Compress": true, + "TargetOriginId": "integspecrestapiorigincustomoriginDistributionOrigin1C1368952", + "ViewerProtocolPolicy": "allow-all" + }, + "Enabled": true, + "HttpVersion": "http2", + "IPV6Enabled": true, + "Origins": [ + { + "CustomOriginConfig": { + "OriginProtocolPolicy": "https-only", + "OriginSSLProtocols": [ + "TLSv1.2" + ] + }, + "DomainName": { + "Fn::Select": [ + 2, + { + "Fn::Split": [ + "/", + { + "Fn::Join": [ + "", + [ + "https://", + { + "Ref": "RestApi0C43BF4B" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".", + { + "Ref": "AWS::URLSuffix" + }, + "/", + { + "Ref": "RestApiDeploymentStageprod3855DE66" + }, + "/" + ] + ] + } + ] + } + ] + }, + "Id": "integspecrestapiorigincustomoriginDistributionOrigin1C1368952", + "OriginPath": "" + } + ] + } + } + } + }, + "Outputs": { + "RestApiEndpoint0551178A": { + "Value": { + "Fn::Join": [ + "", + [ + "https://", + { + "Ref": "RestApi0C43BF4B" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".", + { + "Ref": "AWS::URLSuffix" + }, + "/", + { + "Ref": "RestApiDeploymentStageprod3855DE66" + }, + "/" + ] + ] + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ.json new file mode 100644 index 0000000000000..b82885d02dd42 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "34.0.0", + "testCases": { + "rest-api-origin-custom-origin-path/DefaultTest": { + "stacks": [ + "integ-spec-rest-api-origin-custom-origin" + ], + "assertionStack": "rest-api-origin-custom-origin-path/DefaultTest/DeployAssert", + "assertionStackName": "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/manifest.json new file mode 100644 index 0000000000000..eabd9189aa029 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/manifest.json @@ -0,0 +1,137 @@ +{ + "version": "34.0.0", + "artifacts": { + "integ-spec-rest-api-origin-custom-origin.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-spec-rest-api-origin-custom-origin.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-spec-rest-api-origin-custom-origin": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "integ-spec-rest-api-origin-custom-origin.template.json", + "terminationProtection": false, + "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}/aaeb60e3ccc35e5d7d500c55948cc2eb600ffd3a692d7e77f0be3e7cb2939ea9.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-spec-rest-api-origin-custom-origin.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "integ-spec-rest-api-origin-custom-origin.assets" + ], + "metadata": { + "/integ-spec-rest-api-origin-custom-origin/RestApi/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApi0C43BF4B" + } + ], + "/integ-spec-rest-api-origin-custom-origin/RestApi/Deployment/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiDeployment180EC503edd77c5c8fffabb2e523387cd087d12f" + } + ], + "/integ-spec-rest-api-origin-custom-origin/RestApi/DeploymentStage.prod/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiDeploymentStageprod3855DE66" + } + ], + "/integ-spec-rest-api-origin-custom-origin/RestApi/Endpoint": [ + { + "type": "aws:cdk:logicalId", + "data": "RestApiEndpoint0551178A" + } + ], + "/integ-spec-rest-api-origin-custom-origin/Distribution/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "Distribution830FAC52" + } + ], + "/integ-spec-rest-api-origin-custom-origin/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-spec-rest-api-origin-custom-origin/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "integ-spec-rest-api-origin-custom-origin" + }, + "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.template.json", + "terminationProtection": false, + "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}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets" + ], + "metadata": { + "/rest-api-origin-custom-origin-path/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/rest-api-origin-custom-origin-path/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "rest-api-origin-custom-origin-path/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets.json new file mode 100644 index 0000000000000..0ab325387f4df --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.assets.json @@ -0,0 +1,19 @@ +{ + "version": "34.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.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": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/restapiorigincustomoriginpathDefaultTestDeployAssertCD227A2A.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "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." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/tree.json new file mode 100644 index 0000000000000..858307cd90b68 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.js.snapshot/tree.json @@ -0,0 +1,334 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "integ-spec-rest-api-origin-custom-origin": { + "id": "integ-spec-rest-api-origin-custom-origin", + "path": "integ-spec-rest-api-origin-custom-origin", + "children": { + "RestApi": { + "id": "RestApi", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi", + "aws:cdk:cloudformation:props": { + "body": { + "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" + } + } + } + }, + "name": "RestApi" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.CfnRestApi", + "version": "0.0.0" + } + }, + "Default": { + "id": "Default", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/Default", + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.ResourceBase", + "version": "0.0.0" + } + }, + "Deployment": { + "id": "Deployment", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/Deployment", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/Deployment/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Deployment", + "aws:cdk:cloudformation:props": { + "description": "Automatically created by the RestApi construct", + "restApiId": { + "Ref": "RestApi0C43BF4B" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.CfnDeployment", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.Deployment", + "version": "0.0.0" + } + }, + "DeploymentStage.prod": { + "id": "DeploymentStage.prod", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/DeploymentStage.prod", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/DeploymentStage.prod/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ApiGateway::Stage", + "aws:cdk:cloudformation:props": { + "deploymentId": { + "Ref": "RestApiDeployment180EC503edd77c5c8fffabb2e523387cd087d12f" + }, + "restApiId": { + "Ref": "RestApi0C43BF4B" + }, + "stageName": "prod" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.CfnStage", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.Stage", + "version": "0.0.0" + } + }, + "Endpoint": { + "id": "Endpoint", + "path": "integ-spec-rest-api-origin-custom-origin/RestApi/Endpoint", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnOutput", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_apigateway.SpecRestApi", + "version": "0.0.0" + } + }, + "Distribution": { + "id": "Distribution", + "path": "integ-spec-rest-api-origin-custom-origin/Distribution", + "children": { + "Origin1": { + "id": "Origin1", + "path": "integ-spec-rest-api-origin-custom-origin/Distribution/Origin1", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-spec-rest-api-origin-custom-origin/Distribution/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Distribution", + "aws:cdk:cloudformation:props": { + "distributionConfig": { + "enabled": true, + "origins": [ + { + "domainName": { + "Fn::Select": [ + 2, + { + "Fn::Split": [ + "/", + { + "Fn::Join": [ + "", + [ + "https://", + { + "Ref": "RestApi0C43BF4B" + }, + ".execute-api.", + { + "Ref": "AWS::Region" + }, + ".", + { + "Ref": "AWS::URLSuffix" + }, + "/", + { + "Ref": "RestApiDeploymentStageprod3855DE66" + }, + "/" + ] + ] + } + ] + } + ] + }, + "id": "integspecrestapiorigincustomoriginDistributionOrigin1C1368952", + "originPath": "", + "customOriginConfig": { + "originSslProtocols": [ + "TLSv1.2" + ], + "originProtocolPolicy": "https-only" + } + } + ], + "defaultCacheBehavior": { + "pathPattern": "*", + "targetOriginId": "integspecrestapiorigincustomoriginDistributionOrigin1C1368952", + "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "compress": true, + "viewerProtocolPolicy": "allow-all" + }, + "httpVersion": "http2", + "ipv6Enabled": true + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnDistribution", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.Distribution", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "integ-spec-rest-api-origin-custom-origin/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "integ-spec-rest-api-origin-custom-origin/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "rest-api-origin-custom-origin-path": { + "id": "rest-api-origin-custom-origin-path", + "path": "rest-api-origin-custom-origin-path", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "rest-api-origin-custom-origin-path/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "rest-api-origin-custom-origin-path/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "rest-api-origin-custom-origin-path/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "rest-api-origin-custom-origin-path/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "rest-api-origin-custom-origin-path/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.ts new file mode 100644 index 0000000000000..d0fc107219642 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront-origins/test/integ.spec-rest-api-origin-custom-origin.ts @@ -0,0 +1,69 @@ +import * as apigateway from 'aws-cdk-lib/aws-apigateway'; +import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; +import * as cdk from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; +import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'; + +const app = new cdk.App(); + +const stack = new cdk.Stack(app, 'integ-spec-rest-api-origin-custom-origin'); + +const restApiSwaggerDefinition = { + 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', + }, + }, + }, +}; + +const api = new apigateway.SpecRestApi(stack, 'RestApi', { + apiDefinition: apigateway.ApiDefinition.fromInline(restApiSwaggerDefinition), +}); + +new cloudfront.Distribution(stack, 'Distribution', { + defaultBehavior: { origin: new origins.RestApiOrigin(api, { originPath: '/' }) }, +}); + +new IntegTest(app, 'rest-api-origin-custom-origin-path', { + testCases: [stack], +}); \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-apigateway/lib/restapi.ts b/packages/aws-cdk-lib/aws-apigateway/lib/restapi.ts index 015ac2d500beb..fcdd5b911897c 100644 --- a/packages/aws-cdk-lib/aws-apigateway/lib/restapi.ts +++ b/packages/aws-cdk-lib/aws-apigateway/lib/restapi.ts @@ -326,6 +326,13 @@ export abstract class RestApiBase extends Resource implements IRestApi { return this._domainName; } + /** + * The deployed root URL of this REST API. + */ + public get url() { + return this.urlForPath(); + } + /** * The ID of this API Gateway RestApi. */ @@ -837,13 +844,6 @@ export class RestApi extends RestApiBase { this.node.addValidation({ validate: () => this.validateRestApi() }); } - /** - * The deployed root URL of this REST API. - */ - public get url() { - return this.urlForPath(); - } - /** * Adds a new model. */ diff --git a/packages/aws-cdk-lib/aws-apigateway/test/restapi.test.ts b/packages/aws-cdk-lib/aws-apigateway/test/restapi.test.ts index 24ca1f48e56d2..7faf3624573a3 100644 --- a/packages/aws-cdk-lib/aws-apigateway/test/restapi.test.ts +++ b/packages/aws-cdk-lib/aws-apigateway/test/restapi.test.ts @@ -379,29 +379,29 @@ describe('restapi', () => { // THEN expect(stack.resolve(api.url)).toEqual({ 'Fn::Join': - ['', - ['https://', - { Ref: 'apiC8550315' }, - '.execute-api.', - { Ref: 'AWS::Region' }, - '.', - { Ref: 'AWS::URLSuffix' }, - '/', - { Ref: 'apiDeploymentStageprod896C8101' }, - '/']], + ['', + ['https://', + { Ref: 'apiC8550315' }, + '.execute-api.', + { Ref: 'AWS::Region' }, + '.', + { Ref: 'AWS::URLSuffix' }, + '/', + { Ref: 'apiDeploymentStageprod896C8101' }, + '/']], }); expect(stack.resolve(api.urlForPath('/foo/bar'))).toEqual({ 'Fn::Join': - ['', - ['https://', - { Ref: 'apiC8550315' }, - '.execute-api.', - { Ref: 'AWS::Region' }, - '.', - { Ref: 'AWS::URLSuffix' }, - '/', - { Ref: 'apiDeploymentStageprod896C8101' }, - '/foo/bar']], + ['', + ['https://', + { Ref: 'apiC8550315' }, + '.execute-api.', + { Ref: 'AWS::Region' }, + '.', + { Ref: 'AWS::URLSuffix' }, + '/', + { Ref: 'apiDeploymentStageprod896C8101' }, + '/foo/bar']], }); }); @@ -438,16 +438,16 @@ describe('restapi', () => { // THEN expect(stack.resolve(arn)).toEqual({ 'Fn::Join': - ['', - ['arn:', - { Ref: 'AWS::Partition' }, - ':execute-api:', - { Ref: 'AWS::Region' }, - ':', - { Ref: 'AWS::AccountId' }, - ':', - { Ref: 'apiC8550315' }, - '/stage/method/path']], + ['', + ['arn:', + { Ref: 'AWS::Partition' }, + ':execute-api:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':', + { Ref: 'apiC8550315' }, + '/stage/method/path']], }); }); @@ -481,18 +481,18 @@ describe('restapi', () => { // THEN expect(stack.resolve(method.methodArn)).toEqual({ 'Fn::Join': - ['', - ['arn:', - { Ref: 'AWS::Partition' }, - ':execute-api:', - { Ref: 'AWS::Region' }, - ':', - { Ref: 'AWS::AccountId' }, - ':', - { Ref: 'apiC8550315' }, - '/', - { Ref: 'apiDeploymentStageprod896C8101' }, - '/*/']], + ['', + ['arn:', + { Ref: 'AWS::Partition' }, + ':execute-api:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':', + { Ref: 'apiC8550315' }, + '/', + { Ref: 'apiDeploymentStageprod896C8101' }, + '/*/']], }); }); @@ -1386,6 +1386,63 @@ describe('SpecRestApi', () => { }); }); + test('check if url property exists for a SpecRestApi', () => { + // GIVEN + const stack = new Stack(); + const restApiSwaggerDefinition = { + 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', + }, + }, + }, + }; + const api = new apigw.SpecRestApi(stack, 'my-api', { + apiDefinition: apigw.ApiDefinition.fromInline(restApiSwaggerDefinition), + }); + // THEN + expect(api.url).toBeTruthy(); + }); + test('can override "apiKeyRequired" set in "defaultMethodOptions" at the resource level', () => { // GIVEN const stack = new Stack(); diff --git a/packages/aws-cdk-lib/aws-cloudfront-origins/lib/rest-api-origin.ts b/packages/aws-cdk-lib/aws-cloudfront-origins/lib/rest-api-origin.ts index b737d0dd9d79a..d730c9381941a 100644 --- a/packages/aws-cdk-lib/aws-cloudfront-origins/lib/rest-api-origin.ts +++ b/packages/aws-cdk-lib/aws-cloudfront-origins/lib/rest-api-origin.ts @@ -35,7 +35,7 @@ export interface RestApiOriginProps extends cloudfront.OriginProps { */ export class RestApiOrigin extends cloudfront.OriginBase { - constructor(restApi: apigateway.RestApi, private readonly props: RestApiOriginProps = {}) { + constructor(restApi: apigateway.RestApiBase, private readonly props: RestApiOriginProps = {}) { // urlForPath() is of the form 'https://.execute-api..amazonaws.com/' // Splitting on '/' gives: ['https', '', '.execute-api..amazonaws.com', ''] // The element at index 2 is the domain name, the element at index 3 is the stage name