Skip to content

Commit

Permalink
fix(cdk): fix TagManager to evaluate to undefined if no tags are incl…
Browse files Browse the repository at this point in the history
…uded (#882)

It is unnecessary to add "Tags" field to CloudFormation template unless
tags are specified. Therefore, this patch fixes TagManager to evaluate to
undefined if there are no tags included.
  • Loading branch information
jungseoklee authored and Elad Ben-Israel committed Oct 10, 2018
1 parent 56f0b4e commit 96767d7
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
}
],
"GlobalSecondaryIndexes": [],
"LocalSecondaryIndexes": [],
"Tags": []
"LocalSecondaryIndexes": []
}
},
"TableWithGlobalAndLocalSecondaryIndexBC540710": {
Expand Down Expand Up @@ -313,8 +312,7 @@
}
}
],
"LocalSecondaryIndexes": [],
"Tags": []
"LocalSecondaryIndexes": []
}
},
"TableWithLocalSecondaryIndex4DA3D08F": {
Expand Down Expand Up @@ -366,8 +364,7 @@
"ProjectionType": "ALL"
}
}
],
"Tags": []
]
}
}
}
Expand Down
74 changes: 24 additions & 50 deletions packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ export = {
KeySchema: [{ AttributeName: 'hashKey', KeyType: 'HASH' }],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -119,8 +118,7 @@ export = {
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -151,8 +149,7 @@ export = {
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -183,8 +180,7 @@ export = {
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -215,8 +211,7 @@ export = {
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -247,8 +242,7 @@ export = {
],
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -286,8 +280,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
TableName: 'MyTable',
Tags: []
TableName: 'MyTable'
}
}
}
Expand Down Expand Up @@ -325,8 +318,7 @@ export = {
{ AttributeName: 'sortKey', AttributeType: 'N' }
],
StreamSpecification: { StreamViewType: 'NEW_IMAGE' },
TableName: 'MyTable',
Tags: []
TableName: 'MyTable'
}
}
}
Expand Down Expand Up @@ -364,8 +356,7 @@ export = {
{ AttributeName: 'sortKey', AttributeType: 'N' }
],
StreamSpecification: { StreamViewType: 'OLD_IMAGE' },
TableName: 'MyTable',
Tags: []
TableName: 'MyTable'
}
}
}
Expand Down Expand Up @@ -462,8 +453,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 }
}
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -514,8 +504,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 }
}
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -564,8 +553,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -618,8 +606,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 }
}
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -789,8 +776,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
},
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -851,8 +837,7 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}
],
LocalSecondaryIndexes: [],
Tags: []
LocalSecondaryIndexes: []
}
}
}
Expand Down Expand Up @@ -897,8 +882,7 @@ export = {
],
Projection: { ProjectionType: 'ALL' },
}
],
Tags: []
]
}
}
}
Expand Down Expand Up @@ -944,8 +928,7 @@ export = {
],
Projection: { ProjectionType: 'KEYS_ONLY' },
}
],
Tags: []
]
}
}
}
Expand Down Expand Up @@ -994,8 +977,7 @@ export = {
],
Projection: { NonKeyAttributes: ['lsiNonKey0', 'lsiNonKey1'], ProjectionType: 'INCLUDE' },
}
],
Tags: []
]
}
}
}
Expand Down Expand Up @@ -1100,8 +1082,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableReadAutoScalingRoleFEE68E49:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1183,8 +1164,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableReadAutoScalingRoleFEE68E49:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1294,8 +1274,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableReadAutoScalingRoleFEE68E49:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1373,7 +1352,6 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: [],
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ] } },
Expand Down Expand Up @@ -1581,8 +1559,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableWriteAutoScalingRoleDF7775DE:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1664,8 +1641,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableWriteAutoScalingRoleDF7775DE:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1775,8 +1751,7 @@ export = {
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ],
TableName: 'MyTable',
Tags: [] } },
TableName: 'MyTable' } },
MyTableWriteAutoScalingRoleDF7775DE:
{ Type: 'AWS::IAM::Role',
Properties:
Expand Down Expand Up @@ -1854,7 +1829,6 @@ export = {
ProvisionedThroughput: { ReadCapacityUnits: 42, WriteCapacityUnits: 1337 },
GlobalSecondaryIndexes: [],
LocalSecondaryIndexes: [],
Tags: [],
AttributeDefinitions:
[ { AttributeName: 'hashKey', AttributeType: 'S' },
{ AttributeName: 'sortKey', AttributeType: 'N' } ] } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@
"ToPort": 80
}
],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@
}
],
"SecurityGroupIngress": [],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-rds/test/integ.cluster.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@
"GroupDescription": "RDS security group",
"SecurityGroupEgress": [],
"SecurityGroupIngress": [],
"Tags": [],
"VpcId": {
"Ref": "VPCB9E5F0B4"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/cdk/lib/core/tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ export class TagManager extends Token {
protected tagFormatResolve(tagGroups: TagGroups): any {
const tags = {...tagGroups.nonStickyTags, ...tagGroups.ancestorTags, ...tagGroups.stickyTags};
for (const key of this.blockedTags) { delete tags[key]; }
if (Object.keys(tags).length === 0) {
return undefined;
}
return Object.keys(tags).map( key => ({key, value: tags[key]}));
}
}
10 changes: 5 additions & 5 deletions packages/@aws-cdk/cdk/test/core/test.tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export = {
test.deepEqual(construct.tags.resolve(), tagArray);
}

test.deepEqual(ctagger2.tags.resolve().length, 0);
test.deepEqual(ctagger2.tags.resolve(), undefined);
test.done();
},
'setTag with propagate false tags do not propagate'(test: Test) {
Expand All @@ -51,7 +51,7 @@ export = {
ctagger.tags.setTag(tag.key, tag.value, {propagate: false});

for (const construct of [ctagger1, ctagger2]) {
test.deepEqual(construct.tags.resolve().length, 0);
test.deepEqual(construct.tags.resolve(), undefined);
}
test.deepEqual(ctagger.tags.resolve()[0].key, 'Name');
test.deepEqual(ctagger.tags.resolve()[0].value, 'TheCakeIsALie');
Expand Down Expand Up @@ -96,7 +96,7 @@ export = {
ctagger.tags.setTag(tag.key, tag.value, {propagate: true});
test.deepEqual(ctagger.tags.resolve(), [tag]);
ctagger.tags.removeTag(tag.key);
test.deepEqual(ctagger.tags.resolve(), []);
test.deepEqual(ctagger.tags.resolve(), undefined);
ctagger.tags.setTag(tag.key, tag.value, {propagate: true});
test.deepEqual(ctagger.tags.resolve(), [tag]);
test.done();
Expand All @@ -115,7 +115,7 @@ export = {
ctagger.tags.removeTag('Name');

for (const construct of [ctagger, ctagger1, ctagger2]) {
test.deepEqual(construct.tags.resolve().length, 0);
test.deepEqual(construct.tags.resolve(), undefined);
}
test.done();
},
Expand All @@ -127,7 +127,7 @@ export = {
ctagger1.tags.removeTag('Env', {blockPropagate: true});
const result = ctagger.tags.resolve();
test.deepEqual(result, [{key: 'Env', value: 'Dev'}]);
test.deepEqual(ctagger1.tags.resolve(), []);
test.deepEqual(ctagger1.tags.resolve(), undefined);
test.done();
},
'children can override parent propagated tags'(test: Test) {
Expand Down

0 comments on commit 96767d7

Please sign in to comment.