Skip to content

Commit

Permalink
Adding 'detach_unknown_disks_on_delete' flag for VM resource
Browse files Browse the repository at this point in the history
Optional, defaults to false.  If true, will detach disks not managed by
Terraform VM resource prior to VM deletion.

Issue: hashicorp#8945
  • Loading branch information
dagnello committed Sep 20, 2016
1 parent d129675 commit 16c9d11
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
41 changes: 39 additions & 2 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ func resourceVSphereVirtualMachine() *schema.Resource {
},
},

"detach_unknown_disks_on_delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"cdrom": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1149,10 +1155,11 @@ func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{
}

// Safely eject any disks the user marked as keep_on_remove
var diskSetList []interface{}
if vL, ok := d.GetOk("disk"); ok {
if diskSet, ok := vL.(*schema.Set); ok {

for _, value := range diskSet.List() {
diskSetList = diskSet.List()
for _, value := range diskSetList {
disk := value.(map[string]interface{})

if v, ok := disk["keep_on_remove"].(bool); ok && v == true {
Expand All @@ -1168,6 +1175,36 @@ func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{
}
}

// Safely eject any disks that are not managed by this resource
if v, ok := d.GetOk("detach_unknown_disks_on_delete"); ok && v.(bool) {
var disksToRemove object.VirtualDeviceList
for _, device := range devices {
if devices.TypeName(device) != "VirtualDisk" {
continue
}
vd := device.GetVirtualDevice()
var skip bool
for _, value := range diskSetList {
disk := value.(map[string]interface{})
if int32(disk["key"].(int)) == vd.Key {
skip = true
break
}
}
if skip {
continue
}
disksToRemove = append(disksToRemove, device)
}
if len(disksToRemove) != 0 {
err = vm.RemoveDevice(context.TODO(), true, disksToRemove...)
if err != nil {
log.Printf("[ERROR] Update Remove Disk - Error removing disk: %v", err)
return err
}
}
}

task, err := vm.Destroy(context.TODO())
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The following arguments are supported:
* `dns_servers` - (Optional) List of DNS servers for the virtual network adapter; defaults to 8.8.8.8, 8.8.4.4
* `network_interface` - (Required) Configures virtual network interfaces; see [Network Interfaces](#network-interfaces) below for details.
* `disk` - (Required) Configures virtual disks; see [Disks](#disks) below for details
* `detach_unknown_disks_on_delete` - (Optional) will detach disks not managed by this resource on delete (avoids deletion of disks attached after resource creation outside of Terraform scope).
* `cdrom` - (Optional) Configures a CDROM device and mounts an image as its media; see [CDROM](#cdrom) below for more details.
* `windows_opt_config` - (Optional) Extra options for clones of Windows machines.
* `linked_clone` - (Optional) Specifies if the new machine is a [linked clone](https://www.vmware.com/support/ws5/doc/ws_clone_overview.html#wp1036396) of another machine or not.
Expand Down

0 comments on commit 16c9d11

Please sign in to comment.