From 54bb0b1e254a7e25b8c49b50ce04740323917ae8 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 20 Feb 2019 15:41:27 -0800 Subject: [PATCH] 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. --- .../input/ignore-changes-flatmap-colls.tf | 8 ++++++++ .../want/ignore-changes-flatmap-colls.tf | 8 ++++++++ .../valid/ignore-changes-flatmap-colls/want/versions.tf | 3 +++ configs/configupgrade/upgrade_expr.go | 8 ++++++++ 4 files changed, 27 insertions(+) create mode 100644 configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/input/ignore-changes-flatmap-colls.tf create mode 100644 configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/ignore-changes-flatmap-colls.tf create mode 100644 configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/versions.tf diff --git a/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/input/ignore-changes-flatmap-colls.tf b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/input/ignore-changes-flatmap-colls.tf new file mode 100644 index 000000000..1eeadef55 --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/input/ignore-changes-flatmap-colls.tf @@ -0,0 +1,8 @@ +resource "test_instance" "foo" { + lifecycle { + ignore_changes = [ + "a.%", + "b.#", + ] + } +} diff --git a/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/ignore-changes-flatmap-colls.tf b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/ignore-changes-flatmap-colls.tf new file mode 100644 index 000000000..b79d3639d --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/ignore-changes-flatmap-colls.tf @@ -0,0 +1,8 @@ +resource "test_instance" "foo" { + lifecycle { + ignore_changes = [ + a, + b, + ] + } +} diff --git a/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/versions.tf b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/versions.tf new file mode 100644 index 000000000..d9b6f790b --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/ignore-changes-flatmap-colls/want/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} diff --git a/configs/configupgrade/upgrade_expr.go b/configs/configupgrade/upgrade_expr.go index aa4d2bcbc..b1e40450c 100644 --- a/configs/configupgrade/upgrade_expr.go +++ b/configs/configupgrade/upgrade_expr.go @@ -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() {