vatesfr / terraform-provider-xenorchestra

Xen Orchestra provider for Terraform
MIT License
151 stars 32 forks source link

Stopped VM does not trigger dirty terraform plan #197

Open ddelnano opened 2 years ago

ddelnano commented 2 years ago

When investigating #196, I noticed that if a VM created with terrraform is stopped a subsequent terraform plan will not show that the VM will be started. This means the power_state property is not being refreshed properly.

4censord commented 2 years ago

This means the power_state property is not being refreshed properly.

I have done some testing on this: When changing the power_state property from Computed: true, to

Optional: true,
Default:  "Running",

Terraform detects that it has changed:

  # xenorchestra_vm.vm will be updated in-place
  ~ resource "xenorchestra_vm" "vm" {
        id                 = "1daf4d69-58e3-1976-b7c4-7e6edb0ee595"
      ~ power_state        = "Halted" -> "Running"
        tags               = [
            "dev",
        ]
        # (16 unchanged attributes hidden)

      ~ disk {
          ~ attached   = false -> true
            # (6 unchanged attributes hidden)
        }

      ~ network {
          ~ attached       = false -> true
            # (5 unchanged attributes hidden)
        }
        # (1 unchanged block hidden)
    }

applying this, terraform fails with:

╷
│ Error: jsonrpc2: code 13 message: VM state is halted but should be running: {"objectId":"1daf4d69-58e3-1976-b7c4-7e6edb0ee595","expected":"running","actual":"halted"}
│ 
│   with xenorchestra_vm.vm,
│   on main.tf line 1, in resource "xenorchestra_vm" "vm":
│    1: resource "xenorchestra_vm" "vm" {
│ 
╵
Terraform config Using this config: ```hcl resource "xenorchestra_vm" "vm" { name_label = "${local.projekt}" template = data.xenorchestra_template.template.id cpus = 1 memory_max = 1 * local.gib wait_for_ip = true cdrom { id = data.xenorchestra_vdi.iso.id } network { network_id = data.xenorchestra_network.network.id } disk { sr_id = data.xenorchestra_sr.storage.id name_label = "Data disk for ${local.projekt}" size = 1 * local.gib } tags = [ "dev", ] } ``` * Running apply * wait till completion * stop vm via xoa (normal stop) * run plan, see changed powerstate * run apply see terraform crash. Running terraform with debug logging seems to list all my vms, settings and the like, so i'd rather not post that publicly