From 87bafdeebe914b323f8079d84eb157b45eda3a80 Mon Sep 17 00:00:00 2001 From: peterwoodworth Date: Wed, 15 Jun 2022 13:59:45 -0700 Subject: [PATCH 1/5] fix(route53): improve fromHostedZoneId error message --- packages/@aws-cdk/aws-route53/lib/hosted-zone.ts | 2 +- .../@aws-cdk/aws-route53/test/hosted-zone.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts index 5e263736b7dc6..7b19ad8f5aeba 100644 --- a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts +++ b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts @@ -71,7 +71,7 @@ export class HostedZone extends Resource implements IHostedZone { class Import extends Resource implements IHostedZone { public readonly hostedZoneId = hostedZoneId; public get zoneName(): string { - throw new Error('HostedZone.fromHostedZoneId doesn\'t support "zoneName"'); + throw new Error('Cannot reference `zoneName` when using `HostedZone.fromHostedZoneId()`. A construct consuming this hosted zone may be trying to reference its `zoneName`. If this is the case, use `fromHostedZoneAttributes()` or `fromLookup()` instead.'); } public get hostedZoneArn(): string { return makeHostedZoneArn(this, this.hostedZoneId); diff --git a/packages/@aws-cdk/aws-route53/test/hosted-zone.test.ts b/packages/@aws-cdk/aws-route53/test/hosted-zone.test.ts index a60d277788691..f34d777220418 100644 --- a/packages/@aws-cdk/aws-route53/test/hosted-zone.test.ts +++ b/packages/@aws-cdk/aws-route53/test/hosted-zone.test.ts @@ -161,4 +161,19 @@ describe('hosted zone', () => { }); }).toThrow(/crossAccountZoneDelegationRoleName property is not supported without crossAccountZoneDelegationPrincipal/); }); + + test('fromHostedZoneId throws error when zoneName is referenced', () => { + // GIVEN + const stack = new cdk.Stack(undefined, 'TestStack', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + + // WHEN + const hz = HostedZone.fromHostedZoneId(stack, 'HostedZone', 'abcdefgh'); + + // THEN + expect(() => { + hz.zoneName; + }).toThrow('Cannot reference `zoneName` when using `HostedZone.fromHostedZoneId()`. A construct consuming this hosted zone may be trying to reference its `zoneName`. If this is the case, use `fromHostedZoneAttributes()` or `fromLookup()` instead.'); + }); }); From e4f148c16ebfcdc59b4abccda8b4c6c743cd9858 Mon Sep 17 00:00:00 2001 From: peterwoodworth Date: Wed, 15 Jun 2022 16:27:49 -0700 Subject: [PATCH 2/5] fix(route53): improve fromHostedZoneId error message --- packages/@aws-cdk/aws-route53/lib/hosted-zone.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts index 7b19ad8f5aeba..bc6934ac8a3c9 100644 --- a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts +++ b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts @@ -222,6 +222,8 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone { /** * Import a Route 53 public hosted zone defined either outside the CDK, or in a different CDK stack * + * Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query. + * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct * @param publicHostedZoneId the ID of the public hosted zone to import @@ -229,7 +231,7 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone { public static fromPublicHostedZoneId(scope: Construct, id: string, publicHostedZoneId: string): IPublicHostedZone { class Import extends Resource implements IPublicHostedZone { public readonly hostedZoneId = publicHostedZoneId; - public get zoneName(): string { throw new Error('cannot retrieve "zoneName" from an an imported hosted zone'); } + public get zoneName(): string { throw new Error('Cannot reference `zoneName` when using `PublicHostedZone.fromPublicHostedZoneId()`. A construct consuming this hosted zone may be trying to reference its `zoneName`. If this is the case, use `fromPublicHostedZoneAttributes()` instead'); } public get hostedZoneArn(): string { return makeHostedZoneArn(this, this.hostedZoneId); } @@ -368,6 +370,8 @@ export class PrivateHostedZone extends HostedZone implements IPrivateHostedZone /** * Import a Route 53 private hosted zone defined either outside the CDK, or in a different CDK stack * + * Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query. + * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct * @param privateHostedZoneId the ID of the private hosted zone to import @@ -375,7 +379,7 @@ export class PrivateHostedZone extends HostedZone implements IPrivateHostedZone public static fromPrivateHostedZoneId(scope: Construct, id: string, privateHostedZoneId: string): IPrivateHostedZone { class Import extends Resource implements IPrivateHostedZone { public readonly hostedZoneId = privateHostedZoneId; - public get zoneName(): string { throw new Error('cannot retrieve "zoneName" from an an imported hosted zone'); } + public get zoneName(): string { throw new Error('Cannot reference `zoneName` when using `PrivateHostedZone.fromPrivateHostedZoneId()`. A construct consuming this hosted zone may be trying to reference its `zoneName`'); } public get hostedZoneArn(): string { return makeHostedZoneArn(this, this.hostedZoneId); } From b224b2ebafa59bff5db14fac3e0df41aee91ef5d Mon Sep 17 00:00:00 2001 From: peterwoodworth Date: Thu, 16 Jun 2022 13:59:36 -0700 Subject: [PATCH 3/5] fix(route53): improve fromHostedZoneId docs --- packages/@aws-cdk/aws-route53/lib/hosted-zone.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts index bc6934ac8a3c9..61390494abb25 100644 --- a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts +++ b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts @@ -61,7 +61,8 @@ export class HostedZone extends Resource implements IHostedZone { /** * Import a Route 53 hosted zone defined either outside the CDK, or in a different CDK stack * - * Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query. + * Use when hosted zone ID is known. If a HostedZone is imported with this method the zoneName cannot be referenced. + * If the zoneName is needed then the HostedZone should be imported with `fromHostedZoneAttributes()` or `fromLookup(). * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct @@ -222,7 +223,8 @@ export class PublicHostedZone extends HostedZone implements IPublicHostedZone { /** * Import a Route 53 public hosted zone defined either outside the CDK, or in a different CDK stack * - * Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query. + * Use when hosted zone ID is known. If a PublicHostedZone is imported with this method the zoneName cannot be referenced. + * If the zoneName is needed then the PublicHostedZone should be imported with `fromPublicHostedZoneAttributes()`. * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct @@ -370,7 +372,8 @@ export class PrivateHostedZone extends HostedZone implements IPrivateHostedZone /** * Import a Route 53 private hosted zone defined either outside the CDK, or in a different CDK stack * - * Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query. + * Use when hosted zone ID is known. If a HostedZone is imported with this method the zoneName cannot be referenced. + * If the zoneName is needed then you cannot import a PrivateHostedZone. * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct From a2024a15079fc9a32076446687a7891eb35d5b74 Mon Sep 17 00:00:00 2001 From: peterwoodworth Date: Thu, 16 Jun 2022 14:01:02 -0700 Subject: [PATCH 4/5] fix(route53): improve fromHostedZoneId docs --- packages/@aws-cdk/aws-route53/lib/hosted-zone.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts index 61390494abb25..8f06581b78459 100644 --- a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts +++ b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts @@ -62,7 +62,7 @@ export class HostedZone extends Resource implements IHostedZone { * Import a Route 53 hosted zone defined either outside the CDK, or in a different CDK stack * * Use when hosted zone ID is known. If a HostedZone is imported with this method the zoneName cannot be referenced. - * If the zoneName is needed then the HostedZone should be imported with `fromHostedZoneAttributes()` or `fromLookup(). + * If the zoneName is needed then the HostedZone should be imported with `fromHostedZoneAttributes()` or `fromLookup()`. * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct From b4ba808ceebfab1331573dd9852c3585ffd74cff Mon Sep 17 00:00:00 2001 From: peterwoodworth Date: Thu, 16 Jun 2022 15:22:05 -0700 Subject: [PATCH 5/5] fix(route53): improve fromHostedZoneId docs --- packages/@aws-cdk/aws-route53/lib/hosted-zone.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts index 8f06581b78459..e58070474cc6d 100644 --- a/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts +++ b/packages/@aws-cdk/aws-route53/lib/hosted-zone.ts @@ -62,7 +62,7 @@ export class HostedZone extends Resource implements IHostedZone { * Import a Route 53 hosted zone defined either outside the CDK, or in a different CDK stack * * Use when hosted zone ID is known. If a HostedZone is imported with this method the zoneName cannot be referenced. - * If the zoneName is needed then the HostedZone should be imported with `fromHostedZoneAttributes()` or `fromLookup()`. + * If the zoneName is needed then the HostedZone should be imported with `fromHostedZoneAttributes()` or `fromLookup()` * * @param scope the parent Construct for this Construct * @param id the logical name of this Construct