configs/configupgrade: Silently ignore and trim .% .# in ignore_changes

Prior to Terraform 0.12, ignore_changes was implemented in a
flatmap-oriented fashion and so users found that they could (and in fact,
were often forced to) use the internal .% and .# suffixes flatmap uses to
ignore changes to the number of elements in a list or map.

Terraform 0.12 no longer uses that representation, so we'll interpret
ignoring changes to the length as ignoring changes to the entire
collection. While this is not a totally-equivalent change, in practice
this pattern was most often used in conjunction with specific keys from a
map in order to _effectively_ ignore the entire map, even though Terraform
didn't really support that.
This commit is contained in:
Martin Atkins 2019-02-20 15:41:27 -08:00
parent 0c94e20a83
commit 54bb0b1e25
4 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,8 @@
resource "test_instance" "foo" {
lifecycle {
ignore_changes = [
"a.%",
"b.#",
]
}
}

View File

@ -0,0 +1,8 @@
resource "test_instance" "foo" {
lifecycle {
ignore_changes = [
a,
b,
]
}
}

View File

@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}

View File

@ -560,6 +560,14 @@ func upgradeHeredocBody(buf *bytes.Buffer, val *hilast.Output, filename string,
func upgradeTraversalExpr(val interface{}, filename string, an *analysis) ([]byte, tfdiags.Diagnostics) {
if lit, ok := val.(*hcl1ast.LiteralType); ok && lit.Token.Type == hcl1token.STRING {
trStr := lit.Token.Value().(string)
if strings.HasSuffix(trStr, ".%") || strings.HasSuffix(trStr, ".#") {
// Terraform 0.11 would often not validate traversals given in
// strings and so users would get away with this sort of
// flatmap-implementation-detail reference, particularly inside
// ignore_changes. We'll just trim these off to tolerate it,
// rather than failing below in ParseTraversalAbs.
trStr = trStr[:len(trStr)-2]
}
trSrc := []byte(trStr)
_, trDiags := hcl2syntax.ParseTraversalAbs(trSrc, "", hcl2.Pos{})
if !trDiags.HasErrors() {