Skip to content

Commit

Permalink
feat(cdk): Modify TagManager to support ASG Tags, update VPC
Browse files Browse the repository at this point in the history
BREAKING CHANGE: cdk.TagManager constructor now accepts
`TagManagerProps` as a new argument
  • Loading branch information
moofish32 committed Sep 24, 2018
1 parent 9c3ce7f commit 75a5719
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class VpcNetwork extends VpcNetworkRef implements cdk.ITaggable {
throw new Error('To use DNS Hostnames, DNS Support must be enabled, however, it was explicitly disabled.');
}

this.tags = new cdk.TagManager(this, props.tags);
this.tags = new cdk.TagManager(this, { initialTags: props.tags});
this.tags.setTag(NAME_TAG, this.path, { overwrite: false });

const cidrBlock = ifUndefined(props.cidr, VpcNetwork.DEFAULT_CIDR_RANGE);
Expand Down
33 changes: 23 additions & 10 deletions packages/@aws-cdk/cdk/lib/core/tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export interface RemoveProps {
blockPropagate?: boolean;
}

export interface TagManagerProps {
initialTags?: Tags;
autoScalingGroup?: boolean;
}

/**
* TagManager facilitates a common implementation of tagging for Constructs.
*
Expand Down Expand Up @@ -119,9 +124,14 @@ export class TagManager extends Token {
* Tags that will be removed during `tags` method
*/
private readonly blockedTags: string[] = [];
private readonly autoScalingGroup: boolean = false;

constructor(private readonly parent: Construct, initialTags: Tags = {}) {
constructor(private readonly parent: Construct, props: TagManagerProps = {}) {
super();

if (props.autoScalingGroup) { this.autoScalingGroup = props.autoScalingGroup; }

const initialTags = props.initialTags || {};
for (const key of Object.keys(initialTags)) {
const tag = {
value: initialTags[key],
Expand Down Expand Up @@ -172,10 +182,18 @@ export class TagManager extends Token {
const nonOverwrite = filterTags(this._tags, {sticky: true});
const ancestors = this.parent.ancestors();
ancestors.push(this.parent);
const tags = {...propOverwrite, ...propagatedTags(ancestors), ...nonOverwrite};
const ancestorTags = propagatedTags(ancestors);
const propTags = filterTags(this._tags, {propagate: true});
const tags = {...propOverwrite, ...ancestorTags, ...nonOverwrite};
for (const key of this.blockedTags) { delete tags[key]; }

return Object.keys(tags).map( key => ({key, value: tags[key]}));
if (this.autoScalingGroup) {
return Object.keys(tags).map( (key) => {
const propagateAtLaunch = !!propTags[key] || !!ancestorTags[key];
return {key, value: tags[key], propagateAtLaunch};
});
} else {
return Object.keys(tags).map( key => ({key, value: tags[key]}));
}
}

/**
Expand All @@ -202,17 +220,12 @@ export class TagManager extends Token {
* Removes the specified tag from the array if it exists
*
* @param key The key of the tag to remove
* @param props The `RemoveProps` for the tag
*/
public removeTag(key: string, props: RemoveProps = {blockPropagate: true}): void {
if (props.blockPropagate) {
this.blockedTags.push(key);
}
delete this._tags[key];
}

/**
* Retrieve all propagated tags from all ancestors
*
* This retrieves tags from parents but not local tags
*/
}
48 changes: 47 additions & 1 deletion packages/@aws-cdk/cdk/test/core/test.tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ class ChildTagger extends Construct implements ITaggable {
this.tags = new TagManager(parent);
}
}
class AsgTagger extends Construct implements ITaggable {
public readonly tags: TagManager;
constructor(parent: Construct, name: string) {
super(parent, name);
this.tags = new TagManager(parent, {autoScalingGroup: true});
}
}
class Child extends Construct {
constructor(parent: Construct, name: string) {
super(parent, name);
}
}

export = {

'TagManger handles tags for a Contruct Tree': {
'setTag by default propagates to children'(test: Test) {
const root = new Root();
Expand Down Expand Up @@ -177,5 +183,45 @@ export = {
}
test.done();
},
'ASG TagManager propagates ancestor tags'(test: Test) {
const root = new Root();
const ctagger = new ChildTagger(root, 'one');
const asgTagger = new AsgTagger(ctagger, 'asg');
ctagger.tags.setTag('bar', 'foo', {propagate: true});
ctagger.tags.setTag('zab', 'foo', {propagate: false});
asgTagger.tags.setTag('baz', 'foo', {propagate: false});
asgTagger.tags.setTag('foo', 'bar');
const tags = asgTagger.tags.resolve();
const propKeys = ['bar', 'foo'];
for (const tag of tags) {
// tslint:disable-next-line:no-console
console.log(`${tag.key}:${tag.propagateAtLaunch}`);
// tslint:disable-next-line:no-console
console.log(`${tag.key}::${propKeys.indexOf(tag.key) > -1}`);
test.deepEqual(tag.propagateAtLaunch, propKeys.indexOf(tag.key) > -1);
}
test.deepEqual(tags.length, 3);
test.done();
},
'ASG TagManager returns propagateAtLaunch'(test: Test) {
const root = new Root();
const ctagger = new ChildTagger(root, 'one');
const asgTagger = new AsgTagger(ctagger, 'asg');
ctagger.tags.setTag('baz', 'foo', {propagate: false});
asgTagger.tags.setTag('foo', 'bar');
const asgTm = new TagManager(root, { autoScalingGroup: true });
asgTm.setTag('prop', 'true');
asgTm.setTag('noprop', 'false', {propagate: false });
const tags = asgTm.resolve();
for (const tag of tags) {
if (tag.key === 'prop') {
test.deepEqual(tag.propagateAtLaunch, true);
}
if (tag.key === 'noprop') {
test.deepEqual(tag.propagateAtLaunch, false);
}
}
test.done();
},
},
};

0 comments on commit 75a5719

Please sign in to comment.