Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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: 8 additions & 5 deletions packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export class WindowsImage implements IMachineImageSource {
* Return the image to use in the given context
*/
public getImage(parent: Construct): MachineImage {
const ssmProvider = new SSMParameterProvider(parent);
const ssmProvider = new SSMParameterProvider(parent, {
parameterName: this.imageParameterName(this.version),
});

const parameterName = this.imageParameterName(this.version);
const ami = ssmProvider.getString(parameterName);
const ami = ssmProvider.parameterValue();
return new MachineImage(ami, new WindowsOS());
}

Expand Down Expand Up @@ -98,8 +99,10 @@ export class AmazonLinuxImage implements IMachineImageSource {

const parameterName = '/aws/service/ami-amazon-linux-latest/' + parts.join('-');

const ssmProvider = new SSMParameterProvider(parent);
const ami = ssmProvider.getString(parameterName);
const ssmProvider = new SSMParameterProvider(parent, {
parameterName,
});
const ami = ssmProvider.parameterValue();
return new MachineImage(ami, new LinuxOS());
}
}
Expand Down
57 changes: 57 additions & 0 deletions packages/@aws-cdk/aws-route53/lib/hosted-zone-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import cdk = require('@aws-cdk/cdk');
import { HostedZoneRefProps } from './hosted-zone-ref';

export interface HostedZoneProviderProps {
domainName: string;
privateZone?: boolean;
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);
}
/**
* Return the hosted zone meeting the filter
*/
public findHostedZone(): HostedZoneRefProps {
Copy link
Contributor

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:

  public findAndImport(parent: cdk.Construct, id: string): HostedZoneRef {
    return HostedZoneRef.import(parent, id, this.findHostedZone());
  }

Copy link
Contributor Author

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.

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;
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-route53/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './hosted-zone';
export * from './hosted-zone-provider';
export * from './hosted-zone-ref';
export * from './records';

Expand Down
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-route53/test/test.hosted-zone-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import { HostedZoneProvider } from '../lib';

export = {
'Hosted Zone Provider': {
'HostedZoneProvider will return context values if availble'(test: Test) {
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 cdkZone = {
hostedZoneId: fakeZone.Id,
zoneName: 'example.com',
};

const zone = cdk.resolve(new HostedZoneProvider(stack, filter).findHostedZone());
test.deepEqual(zone, cdkZone);
test.done();
},
}
};
Loading