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

feat(core): RemovalPolicies.of(scope) #32283

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

watany-dev
Copy link
Contributor

@watany-dev watany-dev commented Nov 26, 2024

Issue # (if applicable)

N/A - New feature proposal

Reason for this change

Currently, applying removal policies to multiple resources requires setting them individually or using Tags as a workaround. This change introduces a new RemovalPolicies module that provides a more intuitive and type-safe way to manage removal policies across multiple resources, similar to the existing Tags API.

Description of changes

Added a new RemovalPolicies module that provides:

  • A similar interface to Tags.of() for managing removal policies

  • Type-safe resource type specifications using CloudFormation resource type strings

  • Ability to include or exclude specific resource types

  • Convenient methods for common removal policies (destroy, retain, snapshot, retainOnUpdateOrDelete)

Example usage:

// Using CloudFormation resource type strings
RemovalPolicies.of(scope).retain({
  applyToResourceTypes: ['AWS::S3::Bucket', 'AWS::DynamoDB::Table']
});

const bucket = new s3.Bucket(scope, 'bucket')

// Using CDK resource classes (type-safe)
RemovalPolicies.of(scope).retain({
  applyToResourceTypes: [
       bucket.cfnResourceType,
       CfnTable.CFN_RESOURCE_TYPE_NAME,
  ]
});

// Mixed usage
RemovalPolicies.of(scope).retain({
  applyToResourceTypes: [bucket.cfnResourceType, 'AWS::DynamoDB::Table']
});

Description of how you validated changes

TBD

Checklist

[x] My code adheres to the CONTRIBUTING GUIDE and DESIGN GUIDELINES


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@watany-dev watany-dev requested a review from a team as a code owner November 26, 2024 04:05
@aws-cdk-automation aws-cdk-automation requested a review from a team November 26, 2024 04:05
@github-actions github-actions bot added p2 distinguished-contributor [Pilot] contributed 50+ PRs to the CDK labels Nov 26, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

Copy link

codecov bot commented Nov 26, 2024

Codecov Report

Attention: Patch coverage is 97.36842% with 1 line in your changes missing coverage. Please review.

Project coverage is 66.98%. Comparing base (86d2853) to head (9436df3).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32283      +/-   ##
==========================================
+ Coverage   66.91%   66.98%   +0.06%     
==========================================
  Files         329      330       +1     
  Lines       18684    18722      +38     
  Branches     3260     3267       +7     
==========================================
+ Hits        12503    12540      +37     
- Misses       5854     5855       +1     
  Partials      327      327              
Flag Coverage Δ
suite.unit 66.98% <97.36%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 80.71% <ø> (ø)
packages/aws-cdk-lib/core 82.18% <97.36%> (+0.08%) ⬆️

@watany-dev watany-dev changed the title feat(core): RemovalPolicys.of(scope) feat(core): RemovalPolicys.of(scope) Nov 26, 2024
@aws-cdk-automation
Copy link
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@aws-cdk-automation aws-cdk-automation dismissed their stale review December 19, 2024 11:52

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 20, 2024
@watany-dev watany-dev requested a review from go-to-k December 21, 2024 13:56
packages/aws-cdk-lib/core/lib/removal-policies.ts Outdated Show resolved Hide resolved
Comment on lines +6 to +9
/**
* Properties for applying a removal policy
*/
export interface RemovalPolicyProps {
Copy link
Contributor

@go-to-k go-to-k Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add a property priority to RemovalPolicyProps? (ref: https://github.com/aws/aws-cdk/blob/v2.172.0/packages/aws-cdk-lib/core/lib/aspect.ts#L45)

(* The priority was introduced in v2.172.0: https://dev.to/aws-heroes/aws-cdk-aspects-specifications-have-changed-3i75.)

And the apply method will be changed like:

  public apply(policy: RemovalPolicy, props: RemovalPolicyProps = {}) {
    Aspects.of(this.scope).add(new RemovalPolicyAspect(policy, props), {
      priority: props.priority ?? AspectPriority.MUTATING,
    });
  }

Since the RemovalPolicy change is a kind of resource update, I think it is good that the default value should be MUTATING. (It is the same with the add and remove method of the Tags class.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks.
dec21e2

Copy link
Contributor

@go-to-k go-to-k Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change.

If multiple aspects apply conflicting settings, the one with the higher priority wins.

I found the words "the higher priority" and "wins" a little confusing.

The higher priority (lower numeric value) is applied first, and the lower priority (higher numeric value) is applied later. (Complicated explanation...)

In other words, the removal policy will be the value by the former if overwrite is false, and the value by the latter if true, right?

Can we explain that well?

Copy link
Contributor

@go-to-k go-to-k Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying it out, but what do you think?

#32283 (comment)

packages/aws-cdk-lib/core/README.md Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/removal-policys.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only seen halfway through, but I'll leave you with my comments so far.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 21, 2024
@watany-dev
Copy link
Contributor Author

npx jest core/test/removal-policies.test.ts --coverage --collectCoverageFrom='core/lib/removal-policies.ts'
 PASS  core/test/removal-policies.test.ts

=============================== Coverage summary ===============================
Statements   : 100% ( 28/28 )
Branches     : 90.47% ( 19/21 )
Functions    : 100% ( 10/10 )
Lines        : 100% ( 28/28 )
================================================================================

Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name of the tests in my own way.

packages/aws-cdk-lib/core/test/removal-policies.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/test/removal-policies.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/test/removal-policies.test.ts Outdated Show resolved Hide resolved
@go-to-k
Copy link
Contributor

go-to-k commented Dec 29, 2024

@watany-dev

I made many comments on the tests and documentations. But I suggested my proposed amendments of what I think is good for all the comments. Simply adopting all of them would complete the modifications. If there is something that doesn't fit your idea, feel free to let me know.

watany-dev and others added 18 commits December 31, 2024 12:02
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
….removal-policies.ts

Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
Co-authored-by: Kenta Goto (k.goto) <24818752+go-to-k@users.noreply.github.com>
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 9436df3
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@go-to-k go-to-k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the changes. Can't wait to use this feature!

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants