From aac235aab349a103f92934b86dce9f0eee424c06 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Thu, 17 Sep 2020 11:23:30 -0700 Subject: [PATCH] feat: add support for the 'Version' resource attribute (#10376) As it turns out, there's one more resource attribute that we don't support currently: Version, used for custom CloudFormation resources. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/cloudformation-include/lib/cfn-include.ts | 4 ++-- .../test/invalid-templates.test.ts | 2 +- .../test-templates/custom-resource-with-attributes.json | 1 + packages/@aws-cdk/core/lib/cfn-parse.ts | 1 + packages/@aws-cdk/core/lib/cfn-resource.ts | 9 +++++++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts b/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts index c7fe4d0a09464..1a74dd14f5108 100644 --- a/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts +++ b/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts @@ -552,12 +552,12 @@ export class CfnInclude extends core.CfnElement { // fail early for resource attributes we don't support yet const knownAttributes = [ - 'Type', 'Properties', 'Condition', 'DependsOn', 'Metadata', + 'Type', 'Properties', 'Condition', 'DependsOn', 'Metadata', 'Version', 'CreationPolicy', 'UpdatePolicy', 'DeletionPolicy', 'UpdateReplacePolicy', ]; for (const attribute of Object.keys(resourceAttributes)) { if (!knownAttributes.includes(attribute)) { - throw new Error(`The ${attribute} resource attribute is not supported by cloudformation-include yet. ` + + throw new Error(`The '${attribute}' resource attribute is not supported by cloudformation-include yet. ` + 'Either remove it from the template, or use the CdkInclude class from the core package instead.'); } } diff --git a/packages/@aws-cdk/cloudformation-include/test/invalid-templates.test.ts b/packages/@aws-cdk/cloudformation-include/test/invalid-templates.test.ts index 17bf6b9a3261e..5c901dc3f1bf2 100644 --- a/packages/@aws-cdk/cloudformation-include/test/invalid-templates.test.ts +++ b/packages/@aws-cdk/cloudformation-include/test/invalid-templates.test.ts @@ -80,7 +80,7 @@ describe('CDK Include', () => { test('throws a validation exception when encountering an unrecognized resource attribute', () => { expect(() => { includeTestTemplate(stack, 'non-existent-resource-attribute.json'); - }).toThrow(/The NonExistentResourceAttribute resource attribute is not supported by cloudformation-include yet/); + }).toThrow(/The 'NonExistentResourceAttribute' resource attribute is not supported by cloudformation-include yet/); }); test("throws a validation exception when encountering a Ref-erence to a template element that doesn't exist", () => { diff --git a/packages/@aws-cdk/cloudformation-include/test/test-templates/custom-resource-with-attributes.json b/packages/@aws-cdk/cloudformation-include/test/test-templates/custom-resource-with-attributes.json index b99e4cc2c0eea..716224153dbb9 100644 --- a/packages/@aws-cdk/cloudformation-include/test/test-templates/custom-resource-with-attributes.json +++ b/packages/@aws-cdk/cloudformation-include/test/test-templates/custom-resource-with-attributes.json @@ -13,6 +13,7 @@ "CustomBucket": { "Type": "AWS::MyService::Custom", "Condition": "AlwaysFalseCond", + "Version": "1.0", "Metadata": { "Object1": "Value1", "Object2": "Value2" diff --git a/packages/@aws-cdk/core/lib/cfn-parse.ts b/packages/@aws-cdk/core/lib/cfn-parse.ts index 39d1352b52e7d..7a18378556b9d 100644 --- a/packages/@aws-cdk/core/lib/cfn-parse.ts +++ b/packages/@aws-cdk/core/lib/cfn-parse.ts @@ -284,6 +284,7 @@ export class CfnParser { cfnOptions.updatePolicy = this.parseUpdatePolicy(resourceAttributes.UpdatePolicy); cfnOptions.deletionPolicy = this.parseDeletionPolicy(resourceAttributes.DeletionPolicy); cfnOptions.updateReplacePolicy = this.parseDeletionPolicy(resourceAttributes.UpdateReplacePolicy); + cfnOptions.version = this.parseValue(resourceAttributes.Version); cfnOptions.metadata = this.parseValue(resourceAttributes.Metadata); // handle Condition diff --git a/packages/@aws-cdk/core/lib/cfn-resource.ts b/packages/@aws-cdk/core/lib/cfn-resource.ts index 9af3aa5c16a21..fcb1c95eaf5bb 100644 --- a/packages/@aws-cdk/core/lib/cfn-resource.ts +++ b/packages/@aws-cdk/core/lib/cfn-resource.ts @@ -300,6 +300,7 @@ export class CfnResource extends CfnRefElement { UpdatePolicy: capitalizePropertyNames(this, this.cfnOptions.updatePolicy), UpdateReplacePolicy: capitalizePropertyNames(this, this.cfnOptions.updateReplacePolicy), DeletionPolicy: capitalizePropertyNames(this, this.cfnOptions.deletionPolicy), + Version: this.cfnOptions.version, Metadata: ignoreEmpty(this.cfnOptions.metadata), Condition: this.cfnOptions.condition && this.cfnOptions.condition.logicalId, }, props => { @@ -429,6 +430,14 @@ export interface ICfnResourceOptions { */ updateReplacePolicy?: CfnDeletionPolicy; + /** + * The version of this resource. + * Used only for custom CloudFormation resources. + * + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html + */ + version?: string; + /** * Metadata associated with the CloudFormation resource. This is not the same as the construct metadata which can be added * using construct.addMetadata(), but would not appear in the CloudFormation template automatically.