From d7dda4e4360861354dbd61fc8e19117ef4a412db Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Tue, 30 Apr 2019 14:34:01 -0400 Subject: [PATCH] states/statefile: upgrade legacy dependency syntax (#21159) We used to allow "foo.1" etc as a reference, but now it's "foo[1]". --- .../testdata/roundtrip/v3-simple.in.tfstate | 5 +++-- .../testdata/roundtrip/v3-simple.out.tfstate | 5 +++-- states/statefile/version3_upgrade.go | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/states/statefile/testdata/roundtrip/v3-simple.in.tfstate b/states/statefile/testdata/roundtrip/v3-simple.in.tfstate index b9d9889ef..ec8ceeb3a 100644 --- a/states/statefile/testdata/roundtrip/v3-simple.in.tfstate +++ b/states/statefile/testdata/roundtrip/v3-simple.in.tfstate @@ -20,7 +20,8 @@ "type": "null_resource", "depends_on": [ "null_resource.foo.*", - "null_resource.foobar" + "null_resource.foobar", + "null_resource.foobar.1" ], "primary": { "id": "5388490630832483079", @@ -87,4 +88,4 @@ "depends_on": [] } ] -} +} \ No newline at end of file diff --git a/states/statefile/testdata/roundtrip/v3-simple.out.tfstate b/states/statefile/testdata/roundtrip/v3-simple.out.tfstate index 38f325d66..ba77ddc7f 100644 --- a/states/statefile/testdata/roundtrip/v3-simple.out.tfstate +++ b/states/statefile/testdata/roundtrip/v3-simple.out.tfstate @@ -25,7 +25,8 @@ }, "depends_on": [ "null_resource.foo", - "null_resource.foobar" + "null_resource.foobar", + "null_resource.foobar[1]" ] } ] @@ -74,4 +75,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/states/statefile/version3_upgrade.go b/states/statefile/version3_upgrade.go index 7ec1c9465..2cbe8a53c 100644 --- a/states/statefile/version3_upgrade.go +++ b/states/statefile/version3_upgrade.go @@ -301,7 +301,7 @@ func upgradeInstanceObjectV3ToV4(rsOld *resourceStateV2, isOld *instanceStateV2, dependencies := make([]string, len(rsOld.Dependencies)) for i, v := range rsOld.Dependencies { - dependencies[i] = strings.TrimSuffix(v, ".*") + dependencies[i] = parseLegacyDependency(v) } return &instanceObjectStateV4{ @@ -413,3 +413,19 @@ func simplifyImpliedValueType(ty cty.Type) cty.Type { return ty } } + +func parseLegacyDependency(s string) string { + parts := strings.Split(s, ".") + ret := parts[0] + for _, part := range parts[1:] { + if part == "*" { + break + } + if i, err := strconv.Atoi(part); err == nil { + ret = ret + fmt.Sprintf("[%d]", i) + break + } + ret = ret + "." + part + } + return ret +}