tflint-ruleset-terraform icon indicating copy to clipboard operation
tflint-ruleset-terraform copied to clipboard

Support naming convention auto-fix

Open Fraser-Isbester opened this issue 1 year ago • 4 comments

The introduction of moved blocks means that we can now statically rename resources.

Here is an example test case of what I'm proposing.

Name: "mixed_snake_case: kebab-case to snake_case",
Content: `
resource "aws_instance" "my-instance" {}`,

Expected: `
resource "aws_instance" "my_instance" {}
# Auto-generated by tflint. Safe to remove after apply.
# https://developer.hashicorp.com/terraform/language/moved
moved {
  from = aws_instance.my-instance
  to = aws_instance.my_instance
}`,

WIP PR

Fraser-Isbester avatar Dec 10 '24 07:12 Fraser-Isbester

Clever!

If we are going to rename resources I'd say updating all expressions that reference them is essential. Generating moved blocks is more of a bonus.

Also, I'm thinking moved block generation should be off by default. Ideally your linter runs before any state is modified. But it certainly makes sense to offer it as a configurable option when migrating a project with existing state.

bendrucker avatar Dec 10 '24 16:12 bendrucker

Yeah, agreed to both! I will look into updating all references, is there a tflint native mechanism for this? I only saw mechanisms for updating code at the specific lint location.

Fraser-Isbester avatar Dec 10 '24 19:12 Fraser-Isbester

is there a tflint native mechanism for this?

Yeah I believe auto-fix has just been focused on the range of the issue. Updating references might require a bit of re-thinking.

bendrucker avatar Dec 10 '24 20:12 bendrucker

I would like this feature too. It seems there are similar tools like: https://github.com/suzuki-shunsuke/tfmv

shmokmt avatar Apr 14 '25 09:04 shmokmt