Skip to content

Commit

Permalink
feat(apigateway): publish api endpoint through an export name aws#3662 (
Browse files Browse the repository at this point in the history
aws#4849)

* feat(apigateway): allow customizing the name of the CfnOutput

* chore(apigateway): refactor endpointExportName resolution logic

* chore(apigateway): fix tests for restapi

* simplify logic a little
  • Loading branch information
whimzyLive authored and mergify[bot] committed Nov 8, 2019
1 parent 887389a commit 652a8f5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ export interface RestApiProps extends ResourceOptions {
* @default - no domain name is defined, use `addDomainName` or directly define a `DomainName`.
*/
readonly domainName?: DomainNameOptions;

/**
* Export name for the CfnOutput containing the API endpoint
*
* @default - when no export name is given, output will be created without export
*/
readonly endpointExportName?: string;
}

/**
Expand Down Expand Up @@ -392,7 +399,7 @@ export class RestApi extends Resource implements IRestApi {
...props.deployOptions
});

new CfnOutput(this, 'Endpoint', { value: this.urlForPath() });
new CfnOutput(this, 'Endpoint', { exportName: props.endpointExportName, value: this.urlForPath() });
} else {
if (props.deployOptions) {
throw new Error(`Cannot set 'deployOptions' if 'deploy' is disabled`);
Expand Down
68 changes: 67 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.restapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike, ResourcePart, SynthUtils } from '@aws-cdk/assert';
import { App, CfnElement, CfnResource, Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import apigateway = require('../lib');
Expand Down Expand Up @@ -672,6 +672,72 @@ export = {
ValidateRequestParameters: false
}));

test.done();
},
'creates output with given "exportName"'(test: Test) {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigateway.RestApi(stack, 'myapi', { endpointExportName: 'my-given-export-name' });
api.root.addMethod('GET');

// THEN
test.deepEqual(SynthUtils.toCloudFormation(stack).Outputs, {
myapiEndpoint8EB17201: {
Value: {
'Fn::Join': [
'',
[
'https://',
{Ref: 'myapi162F20B8'},
'.execute-api.',
{Ref: 'AWS::Region'},
'.',
{Ref: 'AWS::URLSuffix'},
'/',
{Ref: 'myapiDeploymentStageprod329F21FF'},
'/'
]
]
},
Export: {Name: 'my-given-export-name'}
}
});

test.done();
},

'creates output when "exportName" is not specified'(test: Test) {
// GIVEN
const stack = new Stack();

// WHEN
const api = new apigateway.RestApi(stack, 'myapi');
api.root.addMethod('GET');

// THEN
test.deepEqual(SynthUtils.toCloudFormation(stack).Outputs, {
myapiEndpoint8EB17201: {
Value: {
'Fn::Join': [
'',
[
'https://',
{Ref: 'myapi162F20B8'},
'.execute-api.',
{Ref: 'AWS::Region'},
'.',
{Ref: 'AWS::URLSuffix'},
'/',
{Ref: 'myapiDeploymentStageprod329F21FF'},
'/'
]
]
}
}
});

test.done();
}
};

0 comments on commit 652a8f5

Please sign in to comment.