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(asg/ec2): fix value of defaultChild #3572

Merged
merged 3 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
resource: 'autoScalingGroup:*:autoScalingGroupName',
resourceName: this.autoScalingGroupName
});
this.node.defaultChild = this.autoScalingGroup;

this.applyUpdatePolicies(props);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,23 @@ export = {
"Roles": ["HelloDude"]
}));
test.done();
}
},

'defaultChild is available on an ASG'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);
const asg = new autoscaling.AutoScalingGroup(stack, 'MyStack', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
});

// THEN
test.notEqual(asg.node.defaultChild, undefined);

test.done();
},
};

function mockVpc(stack: cdk.Stack) {
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ export class Subnet extends Resource implements ISubnet {
this.subnetAvailabilityZone = subnet.attrAvailabilityZone;
this.subnetIpv6CidrBlocks = subnet.attrIpv6CidrBlocks;
this.subnetNetworkAclAssociationId = subnet.attrNetworkAclAssociationId;
this.node.defaultChild = subnet;

const table = new CfnRouteTable(this, 'RouteTable', {
vpcId: props.vpcId,
Expand Down
12 changes: 11 additions & 1 deletion packages/@aws-cdk/aws-ec2/test/test.vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,18 @@ export = {
}), /`vpnGatewayAsn`.+`vpnGateway`.+false/);

test.done();
}
},

'Subnets have a defaultChild'(test: Test) {
// GIVEN
const stack = new Stack();

const vpc = new Vpc(stack, 'VpcNetwork');

test.notEqual(vpc.publicSubnets[0].node.defaultChild, undefined);

test.done();
},
},

"When creating a VPC with a custom CIDR range": {
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/core/lib/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class ConstructNode {
private readonly _references = new Set<Reference>();
private readonly _dependencies = new Set<IDependable>();
private readonly invokedAspects: IAspect[] = [];
private _defaultChild: IConstruct | undefined;

constructor(private readonly host: Construct, scope: IConstruct, id: string) {
id = id || ''; // if undefined, convert to empty string
Expand Down Expand Up @@ -202,6 +203,10 @@ export class ConstructNode {
* @returns a construct or undefined if there is no default child
*/
public get defaultChild(): IConstruct | undefined {
if (this._defaultChild !== undefined) {
return this._defaultChild;
}

const resourceChild = this.tryFindChild('Resource');
eladb marked this conversation as resolved.
Show resolved Hide resolved
const defaultChild = this.tryFindChild('Default');
if (resourceChild && defaultChild) {
Expand All @@ -211,6 +216,20 @@ export class ConstructNode {
return defaultChild || resourceChild;
}

/**
* Override the defaultChild property.
*
* This should only be used in the cases where the correct
* default child is not named 'Resource' or 'Default' as it
* should be.
*
* If you set this to undefined, the default behavior of finding
* the child named 'Resource' or 'Default' will be used.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document what happens when undefined is set

public set defaultChild(value: IConstruct | undefined) {
this._defaultChild = value;
}

/**
* All direct children of this construct.
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/core/test/test.construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@ export = {
test.same(root.node.defaultChild, defaultChild);
test.done();
},
'can override defaultChild'(test: Test) {
const root = new Root();
new Construct(root, 'Resource');
const defaultChild = new Construct(root, 'OtherResource');
root.node.defaultChild = defaultChild;

test.same(root.node.defaultChild, defaultChild);
test.done();
},
'returns "undefined" if there is no default'(test: Test) {
const root = new Root();
new Construct(root, 'child1');
Expand Down