From a55d730154b5df92149edd354f38a66977d29107 Mon Sep 17 00:00:00 2001 From: Nick Lynch Date: Thu, 20 Aug 2020 12:34:00 +0100 Subject: [PATCH] fix(cfn-include): short form for Condition Add YAML support for the short form of Condition `!Condition`. fixes #9785 --- .../cloudformation-include/lib/file-utils.ts | 1 + .../yaml/short-form-conditions.yaml | 18 ++++++++ .../test/yaml-templates.test.ts | 45 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 packages/@aws-cdk/cloudformation-include/test/test-templates/yaml/short-form-conditions.yaml diff --git a/packages/@aws-cdk/cloudformation-include/lib/file-utils.ts b/packages/@aws-cdk/cloudformation-include/lib/file-utils.ts index 65a05101bcb90..eca3a7a0110ba 100644 --- a/packages/@aws-cdk/cloudformation-include/lib/file-utils.ts +++ b/packages/@aws-cdk/cloudformation-include/lib/file-utils.ts @@ -35,6 +35,7 @@ const shortForms: yaml_types.Schema.CustomTag[] = [ 'Select', 'Split', 'Transform', 'And', 'Equals', 'If', 'Not', 'Or', ].map(name => makeTagForCfnIntrinsic(name)).concat( makeTagForCfnIntrinsic('Ref', false), + makeTagForCfnIntrinsic('Condition', false), makeTagForCfnIntrinsic('GetAtt', true, (_doc: yaml.Document, cstNode: yaml_cst.CST.Node): any => { // The position of the leftmost period and opening bracket tell us what syntax is being used // If no brackets are found, then the dot notation is being used; the leftmost dot separates the diff --git a/packages/@aws-cdk/cloudformation-include/test/test-templates/yaml/short-form-conditions.yaml b/packages/@aws-cdk/cloudformation-include/test/test-templates/yaml/short-form-conditions.yaml new file mode 100644 index 0000000000000..376f77921ab5f --- /dev/null +++ b/packages/@aws-cdk/cloudformation-include/test/test-templates/yaml/short-form-conditions.yaml @@ -0,0 +1,18 @@ +Conditions: + AlwaysTrueCond: + !Not [ !Equals [ !Ref "AWS::Region", completely-made-up-region1 ] ] + AnotherAlwaysTrueCond: + !Not [ !Equals [ !Ref "AWS::Region", completely-made-up-region2 ] ] + ThirdAlwaysTrueCond: + !Not [ !Equals [ !Ref "AWS::Region", completely-made-up-region3 ] ] + CombinedCond: + !Or [!Condition AlwaysTrueCond, !Condition AnotherAlwaysTrueCond, !Condition ThirdAlwaysTrueCond] +Resources: + Bucket: + Type: AWS::S3::Bucket + Properties: + BucketName: + !If + - CombinedCond + - MyBucketName + - !Ref AWS::NoValue diff --git a/packages/@aws-cdk/cloudformation-include/test/yaml-templates.test.ts b/packages/@aws-cdk/cloudformation-include/test/yaml-templates.test.ts index 11180842bdb34..99339a064a9e1 100644 --- a/packages/@aws-cdk/cloudformation-include/test/yaml-templates.test.ts +++ b/packages/@aws-cdk/cloudformation-include/test/yaml-templates.test.ts @@ -316,6 +316,51 @@ describe('CDK Include', () => { }); }); + test('can ingest a template with the short form Conditions', () => { + includeTestTemplate(stack, 'short-form-conditions.yaml'); + + expect(stack).toMatchTemplate({ + "Conditions": { + "AlwaysTrueCond": { + "Fn::Not": [ + { "Fn::Equals": [{ "Ref": "AWS::Region" }, "completely-made-up-region1"] }, + ], + }, + "AnotherAlwaysTrueCond": { + "Fn::Not": [ + { "Fn::Equals": [{ "Ref": "AWS::Region" }, "completely-made-up-region2"] }, + ], + }, + "ThirdAlwaysTrueCond": { + "Fn::Not": [ + { "Fn::Equals": [{ "Ref": "AWS::Region" }, "completely-made-up-region3"] }, + ], + }, + "CombinedCond": { + "Fn::Or": [ + { "Condition": "AlwaysTrueCond" }, + { "Condition": "AnotherAlwaysTrueCond" }, + { "Condition": "ThirdAlwaysTrueCond" }, + ], + }, + }, + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": { + "Fn::If": [ + "CombinedCond", + "MyBucketName", + { "Ref": "AWS::NoValue" }, + ], + }, + }, + }, + }, + }); + }); + test('can ingest a yaml with long-form functions and output it unchanged', () => { includeTestTemplate(stack, 'long-form-subnet.yaml');