Skip to content

Commit

Permalink
enum like class
Browse files Browse the repository at this point in the history
  • Loading branch information
watany-dev committed Dec 21, 2024
1 parent 2d90ae7 commit f8d1734
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
9 changes: 7 additions & 2 deletions packages/aws-cdk-lib/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
35 changes: 9 additions & 26 deletions packages/aws-cdk-lib/core/lib/removal-policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions packages/aws-cdk-lib/core/test/removal-policies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -120,7 +128,9 @@ describe('removal-policys', () => {

// WHEN
RemovalPolicies.of(parent).snapshot({
excludeResourceTypes: [TestResource],
excludeResourceTypes: [
resource.cfnResourceType,
],
});

// THEN
Expand Down

0 comments on commit f8d1734

Please sign in to comment.