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

fix(apigatewayv2): unable to retrieve domain url for default stage #16854

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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