mirror of https://github.com/hashicorp/terraform
It doesn't need to be a List of Maps, it can just be a Map. We're also safe to remove a previous workaround I stuck in there. The config parsing is equivalent between a list of maps and a plain map, so we just need a state migration to make this backwards compatible.pull/1517/head
parent
0bd7856942
commit
01e75e0fc3
@ -0,0 +1,72 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func resourceComputeInstanceMigrateState(
|
||||
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
|
||||
if is.Empty() {
|
||||
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
|
||||
return is, nil
|
||||
}
|
||||
|
||||
switch v {
|
||||
case 0:
|
||||
log.Println("[INFO] Found Compute Instance State v0; migrating to v1")
|
||||
return migrateStateV0toV1(is)
|
||||
default:
|
||||
return is, fmt.Errorf("Unexpected schema version: %d", v)
|
||||
}
|
||||
}
|
||||
|
||||
func migrateStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
|
||||
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
|
||||
|
||||
// Delete old count
|
||||
delete(is.Attributes, "metadata.#")
|
||||
|
||||
newMetadata := make(map[string]string)
|
||||
|
||||
for k, v := range is.Attributes {
|
||||
if !strings.HasPrefix(k, "metadata.") {
|
||||
continue
|
||||
}
|
||||
|
||||
// We have a key that looks like "metadata.*" and we know it's not
|
||||
// metadata.# because we deleted it above, so it must be metadata.<N>.<key>
|
||||
// from the List of Maps. Just need to convert it to a single Map by
|
||||
// ditching the '<N>' field.
|
||||
kParts := strings.SplitN(k, ".", 3)
|
||||
|
||||
// Sanity check: all three parts should be there and <N> should be a number
|
||||
badFormat := false
|
||||
if len(kParts) != 3 {
|
||||
badFormat = true
|
||||
} else if _, err := strconv.Atoi(kParts[1]); err != nil {
|
||||
badFormat = true
|
||||
}
|
||||
|
||||
if badFormat {
|
||||
return is, fmt.Errorf(
|
||||
"migration error: found metadata key in unexpected format: %s", k)
|
||||
}
|
||||
|
||||
// Rejoin as "metadata.<key>"
|
||||
newK := strings.Join([]string{kParts[0], kParts[2]}, ".")
|
||||
newMetadata[newK] = v
|
||||
delete(is.Attributes, k)
|
||||
}
|
||||
|
||||
for k, v := range newMetadata {
|
||||
is.Attributes[k] = v
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
|
||||
return is, nil
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
func TestComputeInstanceMigrateState(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
StateVersion int
|
||||
Attributes map[string]string
|
||||
Expected map[string]string
|
||||
Meta interface{}
|
||||
}{
|
||||
"v0.4.2 and earlier": {
|
||||
StateVersion: 0,
|
||||
Attributes: map[string]string{
|
||||
"metadata.#": "2",
|
||||
"metadata.0.foo": "bar",
|
||||
"metadata.1.baz": "qux",
|
||||
"metadata.2.with.dots": "should.work",
|
||||
},
|
||||
Expected: map[string]string{
|
||||
"metadata.foo": "bar",
|
||||
"metadata.baz": "qux",
|
||||
"metadata.with.dots": "should.work",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range cases {
|
||||
is := &terraform.InstanceState{
|
||||
ID: "i-abc123",
|
||||
Attributes: tc.Attributes,
|
||||
}
|
||||
is, err := resourceComputeInstanceMigrateState(
|
||||
tc.StateVersion, is, tc.Meta)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("bad: %s, err: %#v", tn, err)
|
||||
}
|
||||
|
||||
for k, v := range tc.Expected {
|
||||
if is.Attributes[k] != v {
|
||||
t.Fatalf(
|
||||
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
|
||||
tn, k, v, k, is.Attributes[k], is.Attributes)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeInstanceMigrateState_empty(t *testing.T) {
|
||||
var is *terraform.InstanceState
|
||||
var meta interface{}
|
||||
|
||||
// should handle nil
|
||||
is, err := resourceComputeInstanceMigrateState(0, is, meta)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("err: %#v", err)
|
||||
}
|
||||
if is != nil {
|
||||
t.Fatalf("expected nil instancestate, got: %#v", is)
|
||||
}
|
||||
|
||||
// should handle non-nil but empty
|
||||
is = &terraform.InstanceState{}
|
||||
is, err = resourceComputeInstanceMigrateState(0, is, meta)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("err: %#v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue