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

hclwrite: Add RenameAttribute on Body #506

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

Conversation

raymyers
Copy link

@raymyers raymyers commented Jan 17, 2022

Adding the ability to change the name of an attribute already set on a block body. This can be used to automate renaming of terraform locals.

Without this, the only obvious approach is to remove the current attribute and add a new one, but this loses the order and comments which isn't desirable.

Usage

attr := block.Body().GetAttribute(fromName)
if attr != nil {
  attr.SetName(toName)
}

UPDATE

wasChanged := block.Body().RenameAttribute(fromName, toName)

@hashicorp-cla
Copy link

hashicorp-cla commented Jan 17, 2022

CLA assistant check
All committers have signed the CLA.

@apparentlymart
Copy link
Member

Hi @raymyers! Thanks for working on this.

I agree that this seems like a valuable thing for hclwrite to be able to represent! My lone concern about it is that so far we intentionally put all of the attribute-manipulation methods up on Body rather than on individual Attribute so that we could maintain the required invariant that attribute names always be unique within a particular body. For example, SetAttribute will either update the value of an existing attribute in-place or append a new one, depending on whether the given name is already in use.

A hypothetical block.Body.RenameAttribute(fromName, toName) could in principle return a bool to indicate whether it succeeded or not and return false if the toName is already in use. That's a slightly-less-intuitive API design, but I think it would be a drop-in replacement for your current approach if you already know that toName isn't conflicted. I assume you'd already be checking that the given name isn't conflicted within Terraform's local value namespace, which requires that the names be unique across the entire module and therefore implies uniqueness within a particular locals block too.

What do you think? 🤔

@raymyers
Copy link
Author

@apparentlymart

A hypothetical block.Body.RenameAttribute(fromName, toName) could in principle return a bool to indicate whether it succeeded or not and return false if the toName is already in use.

This makes sense, I've renamed SetName to private setName and added a RenameAttribute() on body that preserves the invariant and returns a bool as you suggested. I considered making it an error, but that seemed less consistent with the rest of the API - open to either.

Thanks for reviewing!

@raymyers
Copy link
Author

raymyers commented Jan 19, 2022

Looks like CI has some linux-only failures in TestTokenGenerateConsistency now which didn't show up locally on my OSX and seem unrelated to the change. Is this a known issue?

@raymyers raymyers changed the title hclwrite: Add SetName on Attribute hclwrite: Add RenameAttribute on Body Jan 19, 2022
@raymyers
Copy link
Author

@apparentlymart Would you be able to approve the workflow to run the build?

@minamijoyo
Copy link
Contributor

Hi @raymyers @apparentlymart, Thank you for working on this!

Is there anything I can help move this forward? I have another use case. Upgrading Terraform AWS provider v3 to v4 requires renaming some attributes. I’m currently removing an old attribute and adding a new one, which causes a loss of comments.
minamijoyo/tfedit#49

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.

None yet

4 participants