From f8d17342b79d2f72c00f7ef90793fe415c89df83 Mon Sep 17 00:00:00 2001 From: watany <76135106+watany-dev@users.noreply.github.com> Date: Sat, 21 Dec 2024 11:26:37 +0000 Subject: [PATCH] enum like class --- packages/aws-cdk-lib/core/README.md | 9 +++-- .../aws-cdk-lib/core/lib/removal-policies.ts | 35 +++++-------------- .../core/test/removal-policies.test.ts | 18 +++++++--- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/packages/aws-cdk-lib/core/README.md b/packages/aws-cdk-lib/core/README.md index 6dc16ad6379d5..a9292494c3496 100644 --- a/packages/aws-cdk-lib/core/README.md +++ b/packages/aws-cdk-lib/core/README.md @@ -1663,10 +1663,15 @@ import { RemovalPolicys } from 'aws-cdk-lib'; RemovalPolicys.of(scope).destroy(); // Apply RETAIN policy only to specific resource types -RemovalPolicys.of(scope).retain({ - applyToResourceTypes: ['AWS::S3::Bucket', 'AWS::DynamoDB::Table'], +RemovalPolicies.of(parent).retain({ + applyToResourceTypes: [ + 'AWS::DynamoDB::Table', + bucket.cfnResourceType, // 'AWS::S3::Bucket' + ], }); + + // Apply SNAPSHOT policy excluding specific resource types RemovalPolicys.of(scope).snapshot({ excludeResourceTypes: ['AWS::Test::Resource'], diff --git a/packages/aws-cdk-lib/core/lib/removal-policies.ts b/packages/aws-cdk-lib/core/lib/removal-policies.ts index 5665e3dd1690c..09e002e9c9602 100644 --- a/packages/aws-cdk-lib/core/lib/removal-policies.ts +++ b/packages/aws-cdk-lib/core/lib/removal-policies.ts @@ -9,19 +9,17 @@ import { RemovalPolicy } from './removal-policy'; export interface RemovalPolicyProps { /** * Apply the removal policy only to specific resource types. - * Can be a CloudFormation resource type string (e.g., 'AWS::S3::Bucket') - * or a class that extends CfnResource (e.g., TestBucketResource). + * Can be a CloudFormation resource type string (e.g., 'AWS::S3::Bucket'). * @default - apply to all resources */ - readonly applyToResourceTypes?: (string | typeof CfnResource)[]; + readonly applyToResourceTypes?: string[]; /** * Exclude specific resource types from the removal policy. - * Can be a CloudFormation resource type string (e.g., 'AWS::S3::Bucket') - * or a class that extends CfnResource. + * Can be a CloudFormation resource type string (e.g., 'AWS::S3::Bucket'). * @default - no exclusions */ - readonly excludeResourceTypes?: (string | typeof CfnResource)[]; + readonly excludeResourceTypes?: string[]; /** * If true, overwrite any user-specified removal policy that has been previously set. @@ -41,31 +39,15 @@ class RemovalPolicyAspect implements IAspect { private readonly props: RemovalPolicyProps = {}, ) {} - /** - * Converts pattern (string or class) to resource type string - */ - private convertPatternToResourceType(pattern: string | typeof CfnResource): string { - if (typeof pattern === 'string') { - return pattern; - } else { - const cfnType = (pattern as any).CFN_RESOURCE_TYPE_NAME; - if (typeof cfnType !== 'string') { - throw new Error(`Class ${pattern.name} must have a static CFN_RESOURCE_TYPE_NAME property.`); - } - return cfnType; - } - } - /** * Checks if the given resource type matches any of the patterns */ - private resourceTypeMatchesPatterns(resourceType: string, patterns?: (string | typeof CfnResource)[]): boolean { + private resourceTypeMatchesPatterns(resourceType: string, patterns?: string[]): boolean { if (!patterns || patterns.length === 0) { return false; } - const resourceTypeStrings = patterns.map(pattern => this.convertPatternToResourceType(pattern)); - return resourceTypeStrings.includes(resourceType); + return patterns.includes(resourceType); } public visit(node: IConstruct): void { @@ -76,8 +58,9 @@ class RemovalPolicyAspect implements IAspect { const cfnResource = node as CfnResource; const resourceType = cfnResource.cfnResourceType; - const userAlreadySetPolicy = cfnResource.cfnOptions.deletionPolicy !== undefined || - cfnResource.cfnOptions.updateReplacePolicy !== undefined; + const userAlreadySetPolicy = + cfnResource.cfnOptions.deletionPolicy !== undefined || + cfnResource.cfnOptions.updateReplacePolicy !== undefined; if (!this.props.overwrite && userAlreadySetPolicy) { return; diff --git a/packages/aws-cdk-lib/core/test/removal-policies.test.ts b/packages/aws-cdk-lib/core/test/removal-policies.test.ts index 38a95fe05015e..eb096c99119da 100644 --- a/packages/aws-cdk-lib/core/test/removal-policies.test.ts +++ b/packages/aws-cdk-lib/core/test/removal-policies.test.ts @@ -60,7 +60,10 @@ describe('removal-policys', () => { // WHEN RemovalPolicies.of(parent).retain({ - applyToResourceTypes: ['AWS::S3::Bucket', 'AWS::DynamoDB::Table'], + applyToResourceTypes: [ + bucket.cfnResourceType, // 'AWS::S3::Bucket' + TestTableResource.CFN_RESOURCE_TYPE_NAME, // 'AWS::DynamoDB::Table' + ], }); // THEN @@ -80,7 +83,10 @@ describe('removal-policys', () => { // WHEN RemovalPolicies.of(parent).retain({ - applyToResourceTypes: [TestBucketResource, TestTableResource], + applyToResourceTypes: [ + TestBucketResource.CFN_RESOURCE_TYPE_NAME, // 'AWS::S3::Bucket' + table.cfnResourceType, // 'AWS::DynamoDB::Table' + ], }); // THEN @@ -100,7 +106,9 @@ describe('removal-policys', () => { // WHEN RemovalPolicies.of(parent).snapshot({ - excludeResourceTypes: ['AWS::Test::Resource'], + excludeResourceTypes: [ + TestResource.CFN_RESOURCE_TYPE_NAME, // 'AWS::Test::Resource' + ], }); // THEN @@ -120,7 +128,9 @@ describe('removal-policys', () => { // WHEN RemovalPolicies.of(parent).snapshot({ - excludeResourceTypes: [TestResource], + excludeResourceTypes: [ + resource.cfnResourceType, + ], }); // THEN