Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with empty initialize_params block #664

Merged
merged 2 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func resourceComputeInstance() *schema.Resource {
Update: resourceComputeInstanceUpdate,
Delete: resourceComputeInstanceDelete,

SchemaVersion: 5,
SchemaVersion: 6,
MigrateState: resourceComputeInstanceMigrateState,

Schema: map[string]*schema.Schema{
Expand Down
28 changes: 26 additions & 2 deletions google/resource_compute_instance_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ func resourceComputeInstanceMigrateState(
if err != nil {
return is, err
}
// when adding case 5, make sure to turn this into a fallthrough
fallthrough
case 5:
log.Println("[INFO] Found Compute Instance State v5; migrating to v6")
is, err = migrateStateV5toV6(is)
if err != nil {
return is, err
}
// when adding case 6, make sure to turn this into a fallthrough
return is, err
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
Expand Down Expand Up @@ -238,7 +245,7 @@ func migrateStateV3toV4(is *terraform.InstanceState, meta interface{}) (*terrafo
is.Attributes["boot_disk.0.disk_encryption_key_raw"] = is.Attributes["disk.0.disk_encryption_key_raw"]
is.Attributes["boot_disk.0.disk_encryption_key_sha256"] = is.Attributes["disk.0.disk_encryption_key_sha256"]

if is.Attributes["disk.0.size"] != "" {
if is.Attributes["disk.0.size"] != "" && is.Attributes["disk.0.size"] != "0" {
is.Attributes["boot_disk.0.initialize_params.#"] = "1"
is.Attributes["boot_disk.0.initialize_params.0.size"] = is.Attributes["disk.0.size"]
}
Expand Down Expand Up @@ -489,3 +496,20 @@ func getDiskFromAutoDeleteAndImage(config *Config, instance *compute.Instance, a

return nil, fmt.Errorf("could not find attached disk with image %q", image)
}

func migrateStateV5toV6(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
if is.Attributes["boot_disk.0.initialize_params.#"] == "1" {
if (is.Attributes["boot_disk.0.initialize_params.0.size"] == "0" ||
is.Attributes["boot_disk.0.initialize_params.0.size"] == "") &&
is.Attributes["boot_disk.0.initialize_params.0.type"] == "" &&
is.Attributes["boot_disk.0.initialize_params.0.image"] == "" {
is.Attributes["boot_disk.0.initialize_params.#"] = "0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth calling delete(is.Attributes, "boot_disk.0.initialize_params.INSERT_EVERY_ATTRIBUTE_HERE") to get a properly clean state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can't hurt. Done.

delete(is.Attributes, "boot_disk.0.initialize_params.0.size")
delete(is.Attributes, "boot_disk.0.initialize_params.0.type")
delete(is.Attributes, "boot_disk.0.initialize_params.0.image")
}
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
10 changes: 10 additions & 0 deletions google/resource_compute_instance_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func TestComputeInstanceMigrateState(t *testing.T) {
"create_timeout": "4",
},
},
"remove empty initialize_params": {
StateVersion: 5,
Attributes: map[string]string{
"boot_disk.0.initialize_params.#": "1",
"boot_disk.0.initialize_params.0.size": "0",
},
Expected: map[string]string{
"boot_disk.0.initialize_params.#": "0",
},
},
}

for tn, tc := range cases {
Expand Down