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

Add vsphere virtual machine migration (Fixes #7008) #7023

Merged
merged 1 commit into from
Jun 7, 2016
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
3 changes: 3 additions & 0 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Update: resourceVSphereVirtualMachineUpdate,
Delete: resourceVSphereVirtualMachineDelete,

SchemaVersion: 1,
MigrateState: resourceVSphereVirtualMachineMigrateState,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package vsphere

import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/terraform"
)

func resourceVSphereVirtualMachineMigrateState(
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")
is, err := migrateVSphereVirtualMachineStateV0toV1(is)
if err != nil {
return is, err
}
return is, nil
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateVSphereVirtualMachineStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
log.Println("[DEBUG] Empty VSphere Virtual Machine State; nothing to migrate.")
return is, nil
}

log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

if is.Attributes["skip_customization"] == "" {
is.Attributes["skip_customization"] = "false"
}

for k, _ := range is.Attributes {
if strings.HasPrefix(k, "disk.") && strings.HasSuffix(k, ".size") {
diskParts := strings.Split(k, ".")
if len(diskParts) != 3 {
continue
}
s := strings.Join([]string{diskParts[0], diskParts[1], "controller_type"}, ".")
if _, ok := is.Attributes[s]; !ok {
is.Attributes[s] = "scsi"
}
}
}

log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package vsphere

import (
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestVSphereVirtualMachineMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
Attributes map[string]string
Expected map[string]string
Meta interface{}
}{
"skip_customization before 0.6.16": {
StateVersion: 0,
Attributes: map[string]string{},
Expected: map[string]string{
"skip_customization": "false",
},
},
"disk controller_type": {
StateVersion: 0,
Attributes: map[string]string{
"disk.1234.size": "0",
"disk.5678.size": "0",
"disk.9999.size": "0",
"disk.9999.controller_type": "ide",
},
Expected: map[string]string{
"disk.1234.size": "0",
"disk.1234.controller_type": "scsi",
"disk.5678.size": "0",
"disk.5678.controller_type": "scsi",
"disk.9999.size": "0",
"disk.9999.controller_type": "ide",
},
},
}

for tn, tc := range cases {
is := &terraform.InstanceState{
ID: "i-abc123",
Attributes: tc.Attributes,
}
is, err := resourceVSphereVirtualMachineMigrateState(
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 := resourceVSphereVirtualMachineMigrateState(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 = resourceVSphereVirtualMachineMigrateState(0, is, meta)

if err != nil {
t.Fatalf("err: %#v", err)
}
}