forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add HostedZone context provider (aws#823)
Adds a context provider that can be used to discover and reference existing Hosted Zones. This change modifies the way context values are negotiated between the cdk app and the toolkit, and the way the values are stored inside `cdk.json`. BREAKING CHANGE: you must use the matching `aws-cdk` toolkit when upgrading to this version, or context providers will cease to work. All existing cached context values in `cdk.json` will be invalidated and refreshed.
- Loading branch information
Showing
12 changed files
with
412 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { HostedZoneRef, HostedZoneRefProps } from './hosted-zone-ref'; | ||
|
||
/** | ||
* Zone properties for looking up the Hosted Zone | ||
*/ | ||
export interface HostedZoneProviderProps { | ||
/** | ||
* The zone domain e.g. example.com | ||
*/ | ||
domainName: string; | ||
|
||
/** | ||
* Is this a private zone | ||
*/ | ||
privateZone?: boolean; | ||
|
||
/** | ||
* If this is a private zone which VPC is assocaitated | ||
*/ | ||
vpcId?: string; | ||
} | ||
|
||
const HOSTED_ZONE_PROVIDER = 'hosted-zone'; | ||
|
||
const DEFAULT_HOSTED_ZONE: HostedZoneRefProps = { | ||
hostedZoneId: '/hostedzone/DUMMY', | ||
zoneName: 'example.com', | ||
}; | ||
|
||
interface AwsHostedZone { | ||
Id: string; | ||
Name: string; | ||
} | ||
|
||
/** | ||
* Context provider that will lookup the Hosted Zone ID for the given arguments | ||
*/ | ||
export class HostedZoneProvider { | ||
private provider: cdk.ContextProvider; | ||
constructor(context: cdk.Construct, props: HostedZoneProviderProps) { | ||
this.provider = new cdk.ContextProvider(context, HOSTED_ZONE_PROVIDER, props); | ||
} | ||
|
||
/** | ||
* This method calls `findHostedZone` and returns the imported `HostedZoneRef` | ||
*/ | ||
public findAndImport(parent: cdk.Construct, id: string): HostedZoneRef { | ||
return HostedZoneRef.import(parent, id, this.findHostedZone()); | ||
} | ||
/** | ||
* Return the hosted zone meeting the filter | ||
*/ | ||
public findHostedZone(): HostedZoneRefProps { | ||
const zone = this.provider.getValue(DEFAULT_HOSTED_ZONE); | ||
if (zone === DEFAULT_HOSTED_ZONE) { | ||
return zone; | ||
} | ||
if (!this.isAwsHostedZone(zone)) { | ||
throw new Error(`Expected an AWS Hosted Zone received ${JSON.stringify(zone)}`); | ||
} else { | ||
const actualZone = zone as AwsHostedZone; | ||
// CDK handles the '.' at the end, so remove it here | ||
if (actualZone.Name.endsWith('.')) { | ||
actualZone.Name = actualZone.Name.substring(0, actualZone.Name.length - 1); | ||
} | ||
return { | ||
hostedZoneId: actualZone.Id, | ||
zoneName: actualZone.Name, | ||
}; | ||
} | ||
} | ||
|
||
private isAwsHostedZone(zone: AwsHostedZone | any): zone is AwsHostedZone { | ||
const candidateZone = zone as AwsHostedZone; | ||
return candidateZone.Name !== undefined && candidateZone.Id !== undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/@aws-cdk/aws-route53/test/test.hosted-zone-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { Test } from 'nodeunit'; | ||
import { HostedZoneProvider, HostedZoneRef, HostedZoneRefProps } from '../lib'; | ||
|
||
export = { | ||
'Hosted Zone Provider': { | ||
'HostedZoneProvider will return context values if availble'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(undefined, 'TestStack', { env: { account: '12345', region: 'us-east-1' } }); | ||
const filter = {domainName: 'test.com'}; | ||
new HostedZoneProvider(stack, filter).findHostedZone(); | ||
const key = Object.keys(stack.missingContext)[0]; | ||
|
||
const fakeZone = { | ||
Id: "/hostedzone/11111111111111", | ||
Name: "example.com.", | ||
CallerReference: "TestLates-PublicZo-OESZPDFV7G6A", | ||
Config: { | ||
Comment: "CDK created", | ||
PrivateZone: false | ||
}, | ||
ResourceRecordSetCount: 3 | ||
}; | ||
|
||
stack.setContext(key, fakeZone); | ||
|
||
const cdkZoneProps: HostedZoneRefProps = { | ||
hostedZoneId: fakeZone.Id, | ||
zoneName: 'example.com', | ||
}; | ||
|
||
const cdkZone = HostedZoneRef.import(stack, 'MyZone', cdkZoneProps); | ||
|
||
// WHEN | ||
const provider = new HostedZoneProvider(stack, filter); | ||
const zoneProps = cdk.resolve(provider.findHostedZone()); | ||
const zoneRef = provider.findAndImport(stack, 'MyZoneProvider'); | ||
|
||
// THEN | ||
test.deepEqual(zoneProps, cdkZoneProps); | ||
test.deepEqual(zoneRef.hostedZoneId, cdkZone.hostedZoneId); | ||
test.done(); | ||
}, | ||
} | ||
}; |
Oops, something went wrong.