Skip to content

Commit

Permalink
fix(apigatewayv2): unable to retrieve domain url for default stage (#…
Browse files Browse the repository at this point in the history
…16854)

The `defaultStage` prop in `HttpApi` returns `IHttpStage`.

The `domainUrl` getter was previously added only to `HttpStage`.
Elevate this to the `IHttpStage` level so it's available from the
`HttpApi`.

closes #16638


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar committed Oct 14, 2021
1 parent d0e15cc commit c6db91e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/@aws-cdk/aws-apigatewayv2/lib/http/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface IHttpStage extends IStage {
*/
readonly api: IHttpApi;

/**
* The custom domain URL to this stage
*/
readonly domainUrl: string;

/**
* Metric for the number of client-side errors captured in a given period.
*
Expand Down Expand Up @@ -96,6 +101,7 @@ export interface HttpStageAttributes extends StageAttributes {
}

abstract class HttpStageBase extends StageBase implements IHttpStage {
public abstract readonly domainUrl: string;
public abstract readonly api: IHttpApi;

public metricClientError(props?: MetricOptions): Metric {
Expand Down Expand Up @@ -140,6 +146,10 @@ export class HttpStage extends HttpStageBase {
get url(): string {
throw new Error('url is not available for imported stages.');
}

get domainUrl(): string {
throw new Error('domainUrl is not available for imported stages.');
}
}
return new Import(scope, id);
}
Expand Down Expand Up @@ -177,9 +187,6 @@ export class HttpStage extends HttpStageBase {
return `https://${this.api.apiId}.execute-api.${s.region}.${s.urlSuffix}/${urlPath}`;
}

/**
* The custom domain URL to this stage
*/
public get domainUrl(): string {
if (!this._apiMapping) {
throw new Error('domainUrl is not available when no API mapping is associated with the Stage');
Expand Down
24 changes: 23 additions & 1 deletion packages/@aws-cdk/aws-apigatewayv2/test/http/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Match, Template } from '@aws-cdk/assertions';
import { Certificate } from '@aws-cdk/aws-certificatemanager';
import { Metric } from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import { Duration, Stack } from '@aws-cdk/core';
import {
CorsHttpMethod,
CorsHttpMethod, DomainName,
HttpApi, HttpAuthorizer, HttpIntegrationType, HttpMethod, HttpRouteAuthorizerBindOptions, HttpRouteAuthorizerConfig,
HttpRouteIntegrationBindOptions, HttpRouteIntegrationConfig, IHttpRouteAuthorizer, IHttpRouteIntegration, HttpNoneAuthorizer, PayloadFormatVersion,
} from '../../lib';
Expand Down Expand Up @@ -374,6 +375,27 @@ describe('HttpApi', () => {
expect(() => api.apiEndpoint).toThrow(/apiEndpoint is not configured/);
});

test('domainUrl can be retrieved for default stage', () => {
const stack = new Stack();
const dn = new DomainName(stack, 'DN', {
domainName: 'example.com',
certificate: Certificate.fromCertificateArn(stack, 'cert', 'arn:aws:acm:us-east-1:111111111111:certificate'),
});

const api = new HttpApi(stack, 'Api', {
createDefaultStage: true,
defaultDomainMapping: {
domainName: dn,
},
});

expect(stack.resolve(api.defaultStage?.domainUrl)).toEqual({
'Fn::Join': ['', [
'https://', { Ref: 'DNFDC76583' }, '/',
]],
});
});


describe('default authorization settings', () => {
test('can add default authorizer', () => {
Expand Down

0 comments on commit c6db91e

Please sign in to comment.