From 1baa1b907ef51ae752b46f8baa16c93c71b0d701 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Thu, 28 Mar 2019 13:48:35 -0400 Subject: [PATCH] configs/configupgrade: detect invalid resource names and print a TODO (#20856) * configs/configupgrade: detect invalid resource names and print a TODO message In terraform 0.11 and prior it was possible to start a resource name with a number. This is no longer valid, as the resource name would would be ambiguous with number values in HCL expressions. Fixes #19919 * Update configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/resource.tf Co-Authored-By: mildwonkey --- .../invalid-resource-name/input/resource.tf | 2 ++ .../invalid-resource-name/want/resource.tf | 8 ++++++ .../invalid-resource-name/want/versions.tf | 3 +++ .../valid/module-source/input/main.tf | 4 +++ .../valid/module-source/want/main.tf | 4 +++ configs/configupgrade/upgrade_native.go | 27 +++++++++++++++++++ 6 files changed, 48 insertions(+) create mode 100644 configs/configupgrade/test-fixtures/valid/invalid-resource-name/input/resource.tf create mode 100644 configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/resource.tf create mode 100644 configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/versions.tf diff --git a/configs/configupgrade/test-fixtures/valid/invalid-resource-name/input/resource.tf b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/input/resource.tf new file mode 100644 index 000000000..406f86974 --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/input/resource.tf @@ -0,0 +1,2 @@ +resource "test_instance" "1_invalid_resource_name" { +} diff --git a/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/resource.tf b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/resource.tf new file mode 100644 index 000000000..cb1ccd74f --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/resource.tf @@ -0,0 +1,8 @@ +# TF-UPGRADE-TODO: In Terraform v0.11 and earlier, it was possible to begin a +# resource name with a number, but it is no longer possible in Terraform v0.12. +# +# Rename the resource and run `terraform state mv` to apply the rename in the +# state. Detailed information on the `state mv` command can be found in the +# documentation online: https://www.terraform.io/docs/commands/state/mv.html +resource "test_instance" "1_invalid_resource_name" { +} diff --git a/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/versions.tf b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/versions.tf new file mode 100644 index 000000000..d9b6f790b --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/invalid-resource-name/want/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} diff --git a/configs/configupgrade/test-fixtures/valid/module-source/input/main.tf b/configs/configupgrade/test-fixtures/valid/module-source/input/main.tf index d111c977c..363245d7a 100644 --- a/configs/configupgrade/test-fixtures/valid/module-source/input/main.tf +++ b/configs/configupgrade/test-fixtures/valid/module-source/input/main.tf @@ -1,3 +1,7 @@ +// Test note: the configupgrade tool will ignore this possibly-relative module +// source because it does not find a local directory "foo". The example where +// the configupgrade tool makes a recommendation about relative module sources +// is is in relative-module-source. module "foo" { source = "foo" } diff --git a/configs/configupgrade/test-fixtures/valid/module-source/want/main.tf b/configs/configupgrade/test-fixtures/valid/module-source/want/main.tf index d111c977c..363245d7a 100644 --- a/configs/configupgrade/test-fixtures/valid/module-source/want/main.tf +++ b/configs/configupgrade/test-fixtures/valid/module-source/want/main.tf @@ -1,3 +1,7 @@ +// Test note: the configupgrade tool will ignore this possibly-relative module +// source because it does not find a local directory "foo". The example where +// the configupgrade tool makes a recommendation about relative module sources +// is is in relative-module-source. module "foo" { source = "foo" } diff --git a/configs/configupgrade/upgrade_native.go b/configs/configupgrade/upgrade_native.go index 89bda8524..1c1be2e0d 100644 --- a/configs/configupgrade/upgrade_native.go +++ b/configs/configupgrade/upgrade_native.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log" + "regexp" "sort" "strings" @@ -178,6 +179,9 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal } printComments(&buf, item.LeadComment) + if invalidLabel(labels[0]) { + printLabelTodo(&buf, labels[0]) + } printBlockOpen(&buf, blockType, labels, item.LineComment) rules := bodyContentRules{ @@ -335,6 +339,9 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe } printComments(buf, item.LeadComment) + if invalidLabel(labels[1]) { + printLabelTodo(buf, labels[1]) + } printBlockOpen(buf, blockType, labels, item.LineComment) bodyDiags := upgradeBlockBody(filename, addr.String(), buf, body.List.Items, body.Rbrace, rules, adhocComments) diags = diags.Append(bodyDiags) @@ -767,3 +774,23 @@ func schemaHasSettableArguments(schema *configschema.Block) bool { } return false } + +func invalidLabel(name string) bool { + matched, err := regexp.Match(`[0-9]`, []byte{name[0]}) + if err == nil { + return matched + } + // This isn't likely, but if there's an error here we'll just ignore it and + // move on. + return false +} + +func printLabelTodo(buf *bytes.Buffer, label string) { + buf.WriteString("# TF-UPGRADE-TODO: In Terraform v0.11 and earlier, it was possible to begin a\n" + + "# resource name with a number, but it is no longer possible in Terraform v0.12.\n" + + "#\n" + + "# Rename the resource and run `terraform state mv` to apply the rename in the\n" + + "# state. Detailed information on the `state move` command can be found in the\n" + + "# documentation online: https://www.terraform.io/docs/commands/state/mv.html\n", + ) +}