Skip to content

Commit

Permalink
[Issue:#2] Fix for disk resize issue
Browse files Browse the repository at this point in the history
This commit fixes the issue where a resize of disk would result
in deletion and creation of the disk. With this change if the
size of an existing disk is increased, terraform reconfigures
the vm's disk with the new size and does not delete create the
disk.
  • Loading branch information
santoshananda committed May 29, 2017
1 parent 821902e commit 8642d54
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -554,6 +555,67 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
addedDisks := newDiskSet.Difference(oldDiskSet)
removedDisks := oldDiskSet.Difference(newDiskSet)

log.Printf("[DEBUG] addedDisks : %#v\n", addedDisks)
log.Printf("[DEBUG] removedDisks : %#v\n", removedDisks)

modifiedDisks := make([]map[string]interface{}, 0)

for _, addedDiskRaw := range addedDisks.List() {
addedDisk, _ := addedDiskRaw.(map[string]interface{})
newSize := addedDisk["size"].(int)
ad := make(map[string]interface{})
for k, v := range addedDisk {
ad[k] = v
}
for _, removedDiskRaw := range removedDisks.List() {
removedDisk, _ := removedDiskRaw.(map[string]interface{})
rd := make(map[string]interface{})
for k, v := range removedDisk {
rd[k] = v
}
ad["uuid"], rd["uuid"] = "", ""
ad["key"], rd["key"] = 0, 0
ad["size"], rd["size"] = 0, 0
ok := reflect.DeepEqual(ad, rd)
if ok {
if removedDisk["size"].(int) < newSize {
log.Printf("[DEBUG] Mofifying the size to %d", newSize)
addedDisks.Remove(addedDisk)
removedDisks.Remove(removedDisk)
removedDisk["size"] = newSize
modifiedDisks = append(modifiedDisks, removedDisk)
}
break
}
}
}

log.Printf("[DEBUG] addedDisks after resize: %#v\n", addedDisks)
log.Printf("[DEBUG] removedDisks after resize: %#v\n", removedDisks)
log.Printf("[DEBUG] modifiedDisks after resize: %#v\n", modifiedDisks)

// Just Resized disks
for _, disk := range modifiedDisks {
log.Printf("[DEBUG] Modifying disk : %#v\n", disk)
devices, err := vm.Device(context.TODO())
if err != nil {
return fmt.Errorf("[ERROR] Update resize Disk - Could not get virtual device list: %v", err)
}
v := devices.FindByKey(int32(disk["key"].(int)))
virtualDisk, _ := v.(*types.VirtualDisk)

newSize := disk["size"].(int)
virtualDisk.CapacityInKB = int64(newSize * 1024 * 1024)

config := &types.VirtualDeviceConfigSpec{
Device: virtualDisk,
Operation: types.VirtualDeviceConfigSpecOperationEdit,
}
config.FileOperation = ""
configSpec.DeviceChange = append(configSpec.DeviceChange, config)

}

// Removed disks
for _, diskRaw := range removedDisks.List() {
if disk, ok := diskRaw.(map[string]interface{}); ok {
Expand Down

0 comments on commit 8642d54

Please sign in to comment.