vatesfr / terraform-provider-xenorchestra

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

When importing an existing vm with no template #133

Closed bufanda closed 3 years ago

bufanda commented 3 years ago

I am trying to transition my stack to terraform and I am currently importing existing VMs into the terraform state. Some of the VMs using the template Other Install Media which results that the used template in the terraform state is set to null. As template is a mandatory field which unfortunately can't be set to null this would always lead to a recreation of the whole vm which I don't want to do as it would also mean to migrate all data from the existing machine.

ddelnano commented 3 years ago

@bufanda you need to use terraform's ignore_changes block. The problem that you are describing is worked around in this blog post

resource "xenorchestra_vm" "imported" {
...
...

  lifecycle {
    ignore_changes = [
      template,
    ]
  }
}

This could potentially be improved when #100 is complete.

ddelnano commented 3 years ago

I'm going to close this issue since this is a well supported way of addressing this but I'm happy to help if you run into anymore issues.

bufanda commented 3 years ago

Seems Terraform 0.14 is working different here, setting the lifecycle options now resulted in

❯ terraform plan
xenorchestra_cloud_config.default: Refreshing state... [id=247f641c-f998-4374-881c-68c021fc27a2]
xenorchestra_vm.idun: Refreshing state... [id=491493eb-8f2e-50df-43bf-c245ec8974a7]
xenorchestra_vm.mani: Refreshing state... [id=db98cf97-d610-55e6-d09c-62ee0c7ff2e2]
xenorchestra_vm.sunna: Refreshing state... [id=e707a6aa-c26b-766c-a2da-051ebdfec000]
xenorchestra_vm.ran: Refreshing state... [id=f91d9782-cfc0-eb38-f45a-961637e89552]

Error: "template": required field is not set

  on instance-ran.tf line 1, in resource "xenorchestra_vm" "ran":
   1: resource "xenorchestra_vm" "ran" {

I have set a template id of an existing template, but I only could work around this error by setting a template id in the terraform.tfstate itself.

ddelnano commented 3 years ago

@bufanda can you please share your entire terraform files so I can see what you did?

You should not need to edit the terrraform state file to resolve this issue and according to the terraform docs the lifecycle meta-argument has not changed recently

bufanda commented 3 years ago

I'm using Version 0.16.0 of the provider and this is the instance terraform

resource "xenorchestra_vm" "ran" {
    affinity_host    = data.xenorchestra_host.asgard.id
    auto_poweron     = true
    core_os          = false
    cpus             = 1
    cpu_cap          = 0
    cpu_weight       = 0
    memory_max       = 1073741824
    name_description = "TimeMachine Server in VLAN315"
    name_label       = "VLAN315-TM-Ubuntu 20.04"
    tags             = [
        "ubuntu",
        "OnNFSStorage",
    ]
    template         = data.xenorchestra_template.ubuntu_2004_cloud.id
    wait_for_ip      = false
    disk { 
        name_description = "Created by XO"
        name_label       = "Ubuntu 20.04 Cloud Ready_apuba"
        size             = 10737418240
        sr_id            = "7cf89653-2f0a-863f-fecd-b24903fc2ea2"
    }
    network {
        network_id     = data.xenorchestra_network.vlan42_bond.id
        mac_address    = "be:2e:f4:fe:d0:44"
    }
    network {
        network_id     = data.xenorchestra_network.vlan315_bond.id
        mac_address    = "5e:e2:64:72:15:4d"
    }
    network {
        network_id     = data.xenorchestra_network.vlan128_bond.id
        mac_address    = "06:6a:cc:fd:6e:54"
    }

    timeouts {}

    lifecycle {
        ignore_changes = [
            template,
    ]
  }

} 
bufanda commented 3 years ago

I just upgrade terraform from 0.14.0 to 0.14.7 and did import another VM following your blog post just as I did with another vm before the upgrade. Before I had the same issue following your blog post. After upgrading to 0.14.7 it now works. seems 0.14.0 maybe an issue here. Not sure what happened.