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(cdk): CfnMapping.findInMap with tokens #2531

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/@aws-cdk/cdk/lib/cfn-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class CfnMapping extends CfnRefElement {
throw new Error(`Mapping doesn't contain top-level key '${key1}'`);
}

// opportunistically check that the key exists (if the key does not contain tokens)
if (!Token.isToken(key2) && !(key2 in this.mapping[key1])) {
// opportunistically check that the second key exists (if the key does not contain tokens)
if (!Token.isToken(key1) && !Token.isToken(key2) && !(key2 in this.mapping[key1])) {
throw new Error(`Mapping doesn't contain second-level key '${key2}'`);
}

Expand Down
43 changes: 42 additions & 1 deletion packages/@aws-cdk/cdk/test/test.mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,46 @@ export = {
test.deepEqual(stack.node.resolve(v1), expected);
test.deepEqual(stack.node.resolve(v2), expected);
test.done();
}
},

'no validation if first key is token and second is a static string'(test: Test) {
// GIVEN
const stack = new Stack();
const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
'us-east-1': {
size: 12
}
}
});

// WHEN
const v = mapping.findInMap(Aws.region, 'size');

// THEN
test.deepEqual(stack.node.resolve(v), {
"Fn::FindInMap": [ 'mapping', { Ref: "AWS::Region" }, "size" ]
});
test.done();
},

'validate first key if it is a string and second is a token'(test: Test) {
// GIVEN
const stack = new Stack();
const mapping = new CfnMapping(stack, 'mapping', {
mapping: {
size: {
'us-east-1': 12
}
}
});

// WHEN
const v = mapping.findInMap('size', Aws.region);

// THEN
test.throws(() => mapping.findInMap('not-found', Aws.region), /Mapping doesn't contain top-level key 'not-found'/);
test.deepEqual(stack.node.resolve(v), { 'Fn::FindInMap': [ 'mapping', 'size', { Ref: 'AWS::Region' } ] });
test.done();
},
};