vmware / terraform-provider-vcd

Terraform VMware Cloud Director provider
https://www.terraform.io/docs/providers/vcd/
Mozilla Public License 2.0
148 stars 112 forks source link

bug: terraform destroy fails for `vcd_vapp_vm` when power_on=false is set #314

Open Didainius opened 5 years ago

Didainius commented 5 years ago

Terraform 0.12.6 so far, but version shouldn't matter.

Steps to reproduce

  1. terraform apply for the following config
resource "vcd_vapp_vm" "web" {
  org          = "${var.vcd_org}"
  vdc          = "${var.vcd_vdc}"

  count = 1

  vapp_name     = "${vcd_vapp.lb-test-web.name}"
  name          = "web-${count.index}"
  catalog_name  = "${var.catalog-name}"
  template_name = "${var.photon-os-name}"
  memory        = 512
  cpus          = 2
  cpu_cores     = 1

  power_on = true
}
  1. change the power_on value to false and terraform apply for this setting to take effect

    resource "vcd_vapp_vm" "web" {
    ...
    
    power_on = false
    }
  2. terraform destroy will fail:

    
    vcd_vapp_vm.web[0]: Destroying... [id=web-0]

Error: error deleting: &errors.errorString{s:"error removing VM: API Error: 400: [ 46541acf-0e02-40c8-a226-c841288150a4 ] Th e requested operation could not be executed on VM \"web-0\". Stop the VM and try again."}



The problem here looks to be `power_on = false`. If `power_on = true` is set and applied, then `terraform destroy` works fine.
lvirbalas commented 4 years ago

I hit a similar issue when destroying my deployment where VM had been powered off by something else:

Error: error Undeploying: error undeploy VM: API Error: 400: [ 5bebb75d-6c5f-45c5-ace9-8f501418bf79 ] The requested operation could not be executed since VM "demo-vm-wordpress" is not running.

I vote for destroying VM even if it is powered off.

lvirbalas commented 4 years ago

Now that we have internal disk support, a new kind of error message is seen if you try to destroy a powere off VM:

Error: [resourceVmInternalDiskDelete] failed to delete internal disk: error deleting VM demo-vm-wordpress internal disk 2000: error updating VM disks: API Error: 400: [ 1526cfef-8204-4a16-99db-5fc36a31d730 ] The hard drives of the virtual machine "demo-vm-wordpress" may not be modified while the virtual machine is suspended.

CC: @vbauzysvmware @dataclouder

vbauzys commented 4 years ago

@lvirbalas please provide HCL example and at least all terraform messages from console. Now I am not sure this message from VM resource or from internal disk resource.

lvirbalas commented 4 years ago

HCL snippet of the actual part:

# Bitnami Wordpress VM
resource "vcd_vapp_vm" "demo_vm_wordpress" {
  #org = "${vcd_org.demo_org.name}"
  #vdc = "${vcd_org_vdc.demo_vdc.name}"

  vapp_name     = "${vcd_vapp.demo_vapp.name}"
  name          = "demo-vm-wordpress"
  catalog_name  = "shared-catalog"
  template_name = "bitnami-wordpress-5.2.2-3-linux-centos-7-x86_64"
  memory        = 512
  #memory        = 868
  cpus = 1

  network {
    type               = "org"
    name               = "${vcd_network_routed.demo_routed_net_web.name}"
    ip_allocation_mode = "POOL"
  }

  metadata = {
    demo_data = "Terraform vCD Provider"
  }

  # Guest properties which can be seen via:
  # 1.) vSphere GUI -> Configure -> vApp Options -> Properties
  # 2.) vmtoolsd --cmd "info-get guestinfo.ovfenv"
  guest_properties = {
    "demo.setting" = "Guest property set by Terraform vCD Provider"
  }

  disk {
    name        = "${vcd_independent_disk.demo_independent_disk.name}"
    bus_number  = 1
    unit_number = 0
  }

  # 2.7.0
  override_template_disk {
    bus_type        = "parallel"
    size_in_mb      = "11112"
    bus_number      = 0
    unit_number     = 0
    iops            = 0
    storage_profile = "*"
  }

  accept_all_eulas = "true"
}

# 2.7.0
/*
resource "vcd_vm_internal_disk" "demo_disk_root_imported" {
  vapp_name       = vcd_vapp.demo_vapp.name
  vm_name         = vcd_vapp_vm.demo_vm_wordpress.name
  bus_type        = "parallel"
  size_in_mb      = "11113"
  bus_number      = 0
  unit_number     = 0
  storage_profile = "*"
  allow_vm_reboot = true
}
*/

output "internal_disk" {
  value = vcd_vapp_vm.demo_vm_wordpress.internal_disk

  depends_on = [vcd_vm_internal_disk.demo_disk_2nd]
}

output "disk" {
  value = vcd_vapp_vm.demo_vm_wordpress.disk
}

# 2.7.0
resource "vcd_vm_internal_disk" "demo_disk_2nd" {
  vapp_name = vcd_vapp.demo_vapp.name
  # Hack: introduce dependency on VM ID, so disk is recreated on the first apply if VM is recreated
  vm_name         = (vcd_vapp_vm.demo_vm_wordpress.id == "always-not-equal" ? null : vcd_vapp_vm.demo_vm_wordpress.name)
  bus_type        = "parallel"
  size_in_mb      = "447"
  bus_number      = 0
  unit_number     = 1
  storage_profile = "*"
}

Console:

Plan: 0 to add, 0 to change, 20 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-outbound: Destroying... [id=131079]
vcd_org_user.demo_org_admin: Destroying... [id=urn:vcloud:user:5c7223c3-5526-4311-8bc2-2e964cbc2bf7]
vcd_nsxv_snat.web: Destroying... [id=196613]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule: Destroying... [id=131078]
vcd_network_routed.demo_routed_net_multi_pool: Destroying... [id=urn:vcloud:network:09443ae9-e766-4925-b765-d716d6a41193]
vcd_nsxv_dnat.demo_dnat_wordpress_https: Destroying... [id=196612]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Destroying... [id=131080]
vcd_edgegateway_vpn.vpn: Destroying... [id=demo-gw]
vcd_org_vdc.demo_vdc_experiment: Destroying... [id=urn:vcloud:vdc:1e97cf66-6945-4036-b43f-744c99ecf392]
vcd_vm_internal_disk.demo_disk_root_imported: Destroying... [id=2000]
vcd_org_user.demo_org_admin: Destruction complete after 2s
vcd_vm_internal_disk.demo_disk_2nd: Destroying... [id=2001]
vcd_org_vdc.demo_vdc_experiment: Destruction complete after 6s
vcd_nsxv_snat.web: Destruction complete after 7s
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-outbound: Still destroying... [id=131079, 10s elapsed]
vcd_nsxv_dnat.demo_dnat_wordpress_https: Still destroying... [id=196612, 10s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule: Still destroying... [id=131078, 10s elapsed]
vcd_edgegateway_vpn.vpn: Still destroying... [id=demo-gw, 10s elapsed]
vcd_network_routed.demo_routed_net_multi_pool: Still destroying... [id=urn:vcloud:network:09443ae9-e766-4925-b765-d716d6a41193, 10s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Still destroying... [id=131080, 10s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-outbound: Destruction complete after 13s
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule: Destruction complete after 19s
vcd_nsxv_dnat.demo_dnat_wordpress_https: Still destroying... [id=196612, 20s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Still destroying... [id=131080, 20s elapsed]
vcd_network_routed.demo_routed_net_multi_pool: Still destroying... [id=urn:vcloud:network:09443ae9-e766-4925-b765-d716d6a41193, 20s elapsed]
vcd_edgegateway_vpn.vpn: Still destroying... [id=demo-gw, 20s elapsed]
vcd_network_routed.demo_routed_net_multi_pool: Destruction complete after 29s
vcd_nsxv_dnat.demo_dnat_wordpress_https: Still destroying... [id=196612, 30s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Still destroying... [id=131080, 30s elapsed]
vcd_edgegateway_vpn.vpn: Still destroying... [id=demo-gw, 30s elapsed]
vcd_nsxv_dnat.demo_dnat_wordpress_https: Destruction complete after 34s
vcd_edgegateway_vpn.vpn: Still destroying... [id=demo-gw, 40s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Still destroying... [id=131080, 40s elapsed]
vcd_edgegateway_vpn.vpn: Destruction complete after 45s
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Still destroying... [id=131080, 50s elapsed]
vcd_nsxv_firewall_rule.demo-nsxv-fw-rule-ssh: Destruction complete after 50s
vcd_nsxv_dnat.demo_dnat_wordpress_ssh: Destroying... [id=196614]
vcd_nsxv_dnat.demo_dnat_wordpress_ssh: Destruction complete after 5s

Error: [resourceVmInternalDiskDelete] failed to delete internal disk: error deleting VM demo-vm-wordpress internal disk 2000: error updating VM disks: API Error: 400: [ 1526cfef-8204-4a16-99db-5fc36a31d730 ] The hard drives of the virtual machine "demo-vm-wordpress" may not be modified while the virtual machine is suspended.

Error: [resourceVmInternalDiskDelete] failed to delete internal disk: error deleting VM demo-vm-wordpress internal disk 2001: error updating VM disks: API Error: 400: [ c50c141a-e122-494d-8abb-865befcae58b ] The hard drives of the virtual machine "demo-vm-wordpress" may not be modified while the virtual machine is suspended.

How to reproduce:

  1. terraform apply - create VM with internal disk resource
  2. Stop VM
  3. terraform destroy

In general, I think destroy should work regardless of whether the VM is running, is suspended or is stopped. To cure this, fixes in both internal disk and VM resources will probably be needed.