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(route53): improve fromHostedZoneId error message #20755

Merged
merged 7 commits into from
Jun 16, 2022
15 changes: 11 additions & 4 deletions packages/@aws-cdk/aws-route53/lib/hosted-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,7 +72,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);
Expand Down Expand Up @@ -222,14 +223,17 @@ 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. 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
* @param publicHostedZoneId the ID of the public hosted zone to import
*/
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);
}
Expand Down Expand Up @@ -368,14 +372,17 @@ 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. 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
* @param privateHostedZoneId the ID of the private hosted zone to import
*/
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);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-route53/test/hosted-zone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
});