Skip to content

feat(lib): add the remove function to TerraformResource #3849

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

xphir
Copy link

@xphir xphir commented Apr 13, 2025

Adds support within the TerraformResource for the removed block

Related issue

Fixes #3638

Description

Key Changes

  • Introduced a _removed private field on TerraformResource to track removed state.
  • Updated toTerraform, toHclTerraform, and toMetadata to support the removed block when applicable.
  • Added a new remove(...) method to define removed resources, including optional lifecycle and provisioner support.
  • Introduced a new RemovedBlockLocalExecProvisioner interface, as JSII does not support the Omit type against LocalExecProvisioner, to make the API clearer and reduce developer confusion.
  • Added compatibility checks to prevent calling remove() in combination with importFrom or any moveXX() methods, since these operations are mutually exclusive in Terraform’s configuration model.
  • Added versions checks for the remove functionality (1.7) and the provisioner block (1.9`)
  • Added HCL rendering support via renderRemoved

Rationale

  • Keeping a dedicated _removed field aligns with the patterns used for import and move, making the codebase more consistent and easier to reason about.
  • Using a distinct provisioner type for removed blocks improves the developer experience by avoiding misuse of the when property and surfacing clearer validation (as opposed to a value check and throw).
  • Enforcing mutual exclusivity via runtime checks ensures predictable behaviour and avoids subtle bugs where toTerraform() would silently ignore conflicting operations.

Checklist

  • I have updated the PR title to match CDKTF's style guide
  • I have run the linter on my code locally
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation if applicable
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works if applicable
  • New and existing unit tests pass locally with my changes

@xphir xphir requested a review from a team as a code owner April 13, 2025 05:07
@xphir xphir requested review from ansgarm and DanielMSchmidt and removed request for a team April 13, 2025 05:07
Copy link

hashicorp-cla-app bot commented Apr 13, 2025

CLA assistant check
All committers have signed the CLA.

@xphir xphir changed the title feat: add the remove function to TerraformResource feat(lib): add the remove function to TerraformResource Apr 13, 2025
@xphir xphir marked this pull request as draft April 13, 2025 05:41
@xphir xphir force-pushed the xphir/removed-support branch from 12b42d4 to bea7a71 Compare April 13, 2025 07:11
@xphir xphir marked this pull request as ready for review April 13, 2025 07:14
@xphir xphir marked this pull request as draft April 13, 2025 07:18
@@ -393,6 +446,12 @@ export class TerraformResource
[this.terraformResourceType]: [this.friendlyUniqueId],
}
: undefined,
// TODO: its was unclear to me if how removed should interact with `toMetadata`
Copy link
Author

Choose a reason for hiding this comment

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

Looking to get some clarification here how removed should work with toMetadata()

xphir added 2 commits April 29, 2025 10:22
Adds the `remove` function to the `TerraformResource` that enables support for the underlying terraform `removed` block (context: https://developer.hashicorp.com/terraform/language/resources/syntax#removing-resources). Includes rendering support and unit tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

removed block: orphan resources managed by cdktf/terraform
1 participant