Skip to content

Commit

Permalink
fix(asg/ec2): fix value of defaultChild
Browse files Browse the repository at this point in the history
`AutoScalingGroup` and `Subnet` had default children with incorrect
names, so that `defaultChild` would not pick them up. Add and use an
override mechanism so that people can use the escape hatch more
conveniently with those constructs.

Fixes #3478.
  • Loading branch information
rix0rrr committed Aug 7, 2019
1 parent 4e11d86 commit 226a659
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
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
16 changes: 16 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');
const defaultChild = this.tryFindChild('Default');
if (resourceChild && defaultChild) {
Expand All @@ -211,6 +216,17 @@ 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.
*/
public set defaultChild(value: IConstruct | undefined) {
this._defaultChild = value;
}

/**
* All direct children of this construct.
*/
Expand Down

0 comments on commit 226a659

Please sign in to comment.