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

Merged
merged 60 commits into from
Mar 7, 2025
Merged

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 98.27586% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.37%. Comparing base (3ed7c4d) to head (1743e0a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32283      +/-   ##
==========================================
+ Coverage   82.24%   82.37%   +0.13%     
==========================================
  Files         119      120       +1     
  Lines        6875     6933      +58     
  Branches     1161     1169       +8     
==========================================
+ Hits         5654     5711      +57     
- Misses       1118     1119       +1     
  Partials      103      103              
Flag Coverage Δ
suite.unit 82.37% <98.27%> (+0.13%) ⬆️

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

Components Coverage Δ
packages/aws-cdk ∅ <ø> (∅)
packages/aws-cdk-lib/core 82.37% <98.27%> (+0.13%) ⬆️
🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@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
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 watany-dev requested review from kaizencc and mrgrain March 5, 2025 14:59
Copy link
Contributor

@kaizencc kaizencc left a comment

Choose a reason for hiding this comment

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

Hi @watany-dev thanks for continuing to iterate with me on this PR. sorry for the additional comments -- i want to make sure that this one is right before we send it out. my main thing is that removalPolicyAspect shouldn't be a property anymore, now that you've added MissingRemovalPolicies class. Please let me know if you have any questions, i'll continue to monitor this PR. Thanks!

Comment on lines 39 to 47
/**
* Whether to respect the removal policy that was previously applied to the resource.
*
* If set to `true`, the removal policy will only be applied if the resource
* doesn't already have a removal policy set.
*
* @default false
*/
readonly respectPreviousPolicy?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

i would expect this property to be deleted, now that the classes we expose determine this behavior. what would it mean to have a MissingRemovalPolicy.apply(..., { respectPreviousPolicy: false })?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for my carelessness. I hope you are cured.

*/
class RemovalPolicyAspect extends BaseRemovalPolicyAspect {
protected shouldApplyPolicy(cfnResource: CfnResource): boolean {
// If respectPreviousPolicy is true, only apply if no policy exists
Copy link
Contributor

Choose a reason for hiding this comment

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

i was hoping respectPreviousPolicy was no longer a property set by the user. RemovalPolicyAspect should always apply the policy (i.e. it's as if respectPreviousPolicy was false). MissingRemovalPolicyAspect behaves as if respectPreviousPolicy was true.

that way, the user doesn't have to set respectPreviousPolicy and rahter just decides either to use RemovalPolicy or MissingRemovalPolicy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry! Unnecessary properties have disappeared! I may need to consult on this, but if priority is used in the removalPolicies to be overwritten, I'll issue a warning, right?

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Mar 5, 2025
@mergify mergify bot dismissed kaizencc’s stale review March 6, 2025 15:03

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Mar 6, 2025
@watany-dev watany-dev requested a review from kaizencc March 6, 2025 15:55
Copy link
Contributor

@kaizencc kaizencc left a comment

Choose a reason for hiding this comment

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

okay, approving @watany-dev! hopefully i didn't break the build with my changes but if i did i'll circle back and fix

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Mar 7, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 34c547c into aws:main Mar 7, 2025
21 checks passed
Copy link

github-actions bot commented Mar 7, 2025

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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 skip-abstractions-board signal to automated workflow to skip adding to project board
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants