-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(context-providers) Enable arguments to be passed as property object #823
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f6c1ec2
feat(context-providers) Refactor to enable context providers properties
moofish32 2e77cac
feat(context-providers) Refactor to enable context providers properties
moofish32 b3b7226
hosted zone refactor
moofish32 b764b97
Merge branch 'master' into context-provider-fix
moofish32 ca25785
fixing commit merge
moofish32 0e13281
initial working filters and errors
moofish32 318afcc
moving test into route53 module
moofish32 ffa83c3
removing commented code
moofish32 c0d863d
adding findAndImport to HostedZoneProvider
moofish32 42758db
refactor context provider props from interface to type
moofish32 47816e0
resolving conflict
moofish32 0cde788
resolving conflicts in package-lock
moofish32 9dd737a
cleaning up code
moofish32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you have this, might even make sense to add a convenience
import
function on this class as well:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea 👍 I will add.