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 <mildwonkey@users.noreply.github.com>
This commit is contained in:
parent
003317d7c8
commit
1baa1b907e
|
@ -0,0 +1,2 @@
|
||||||
|
resource "test_instance" "1_invalid_resource_name" {
|
||||||
|
}
|
|
@ -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" {
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
terraform {
|
||||||
|
required_version = ">= 0.12"
|
||||||
|
}
|
|
@ -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" {
|
module "foo" {
|
||||||
source = "foo"
|
source = "foo"
|
||||||
}
|
}
|
||||||
|
|
|
@ -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" {
|
module "foo" {
|
||||||
source = "foo"
|
source = "foo"
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -178,6 +179,9 @@ func (u *Upgrader) upgradeNativeSyntaxFile(filename string, src []byte, an *anal
|
||||||
}
|
}
|
||||||
|
|
||||||
printComments(&buf, item.LeadComment)
|
printComments(&buf, item.LeadComment)
|
||||||
|
if invalidLabel(labels[0]) {
|
||||||
|
printLabelTodo(&buf, labels[0])
|
||||||
|
}
|
||||||
printBlockOpen(&buf, blockType, labels, item.LineComment)
|
printBlockOpen(&buf, blockType, labels, item.LineComment)
|
||||||
|
|
||||||
rules := bodyContentRules{
|
rules := bodyContentRules{
|
||||||
|
@ -335,6 +339,9 @@ func (u *Upgrader) upgradeNativeSyntaxResource(filename string, buf *bytes.Buffe
|
||||||
}
|
}
|
||||||
|
|
||||||
printComments(buf, item.LeadComment)
|
printComments(buf, item.LeadComment)
|
||||||
|
if invalidLabel(labels[1]) {
|
||||||
|
printLabelTodo(buf, labels[1])
|
||||||
|
}
|
||||||
printBlockOpen(buf, blockType, labels, item.LineComment)
|
printBlockOpen(buf, blockType, labels, item.LineComment)
|
||||||
bodyDiags := upgradeBlockBody(filename, addr.String(), buf, body.List.Items, body.Rbrace, rules, adhocComments)
|
bodyDiags := upgradeBlockBody(filename, addr.String(), buf, body.List.Items, body.Rbrace, rules, adhocComments)
|
||||||
diags = diags.Append(bodyDiags)
|
diags = diags.Append(bodyDiags)
|
||||||
|
@ -767,3 +774,23 @@ func schemaHasSettableArguments(schema *configschema.Block) bool {
|
||||||
}
|
}
|
||||||
return false
|
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",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue