bpg / terraform-provider-proxmox

Terraform Provider for Proxmox
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
768 stars 125 forks source link

Unable to Import Virtual Machine into state #655

Closed mattd-krystal closed 10 months ago

mattd-krystal commented 10 months ago

Describe the bug When importing a running virtula machine I get the following error: Reason: json: cannot unmarshal string into Go struct field GetStatusResponseData.data.pid of type int

Proxmox version is 6.3.6

To Reproduce Steps to reproduce the behavior:

  1. I made a test resource "proxmox_virtual_environment_vm" "test"
  2. Run terraform import import proxmox_virtual_environment_vm.test awning/146
  3. See error

Please also provide a minimal Terraform configuration that reproduces the issue.

resource "proxmox_virtual_environment_vm" "test" {
  name        = "test01"
  description = "Test Import"
  node_name   = "awning"
  vm_id       = 143

  agent {
    enabled = false
  }

  cpu {
    cores = 1
  }

  memory {
    dedicated = 4096
  }

  disk {
    datastore_id = "local-zfs"
    interface    = "virtio0"
    file_format  = "raw"
    ssd          = true
    size = 100
  }

  network_device {
    bridge = "vmbr1"
    mtu    = 1
    model  = "virtio"
  }

  operating_system {
    type = "win10"
  }
}

Expected behavior The resource would be imported into terraform state

Additional context Add any other context about the problem here.

2023-10-26T10:35:56.239+0100 [DEBUG] provider.terraform-provider-proxmox_v0.36.0: Sending HTTP Request: tf_resource_type=proxmox_virtual_environment_vm Cookie="PVEAuthCookie=PVE:root@pam:653A32FB::F4ID5R74mx4th54HC45HWynr8Zsn1aZj0bdOYBiBUY7aX8hsfFwOfphznUB/hGa93bu8JSdEmZmGX6WT1JeUFziX0iUO1eXdb0K4ksXB+QhwE2/ufdAyqRm8uRshxQ++kV7Ypkg4ZMNcYfiqsdfFO1SsE763vc1l3in/MGTUKbqJ9g8CVFaEu9he1KOeIlM8+UqBW+pjcPN83HjAFqK8CD2id7KUzDJ0Zy8Z480zsZFgZia3wkjY08UqDPJ9kep4VuXxeGPbaLP5HCMZWIf+3EctIplYwX++Q1qnIIhLH4hNW0Je1HhmWtZ1i2Xn+eb+frinY8RQD1gDyxkyWSIwXQ==" tf_http_req_method=GET tf_provider_addr=registry.terraform.io/bpg/proxmox tf_http_req_version=HTTP/1.1 tf_http_trans_id=74f9a8a2-6fa5-80c2-0f5a-a8c43240d589 tf_mux_provider=tf5to6server.v5tov6Server tf_req_id=c3cf43e4-2875-575d-202d-fc31a2401250 Accept=application/json tf_http_req_body="" User-Agent=Go-http-client/1.1 tf_rpc=ReadResource Accept-Encoding=gzip tf_http_op_type=request tf_http_req_uri=/api2/json/nodes/awning/qemu/146/status/current @caller=github.com/hashicorp/terraform-plugin-sdk/v2@v2.29.0/helper/logging/logging_http_transport.go:162 @module=proxmox Host=10.6.19.1:8006 timestamp="2023-10-26T10:35:56.239+0100"
2023-10-26T10:35:56.283+0100 [DEBUG] provider.terraform-provider-proxmox_v0.36.0: Received HTTP Response: tf_mux_provider=tf5to6server.v5tov6Server tf_provider_addr=registry.terraform.io/bpg/proxmox Connection=[Keep-Alive, Keep-Alive] tf_http_op_type=response tf_http_res_version=HTTP/1.1 tf_http_trans_id=74f9a8a2-6fa5-80c2-0f5a-a8c43240d589 Expires="Thu, 26 Oct 2023 09:35:55 GMT" tf_http_res_body="{\"data\":{\"cpu\":0.132178188661128,\"maxmem\":17179869184,\"netin\":36439445868772,\"uptime\":65481022,\"running-machine\":\"pc-i440fx-5.2+pve0\",\"template\":\"\",\"disk\":0,\"vmid\":\"146\",\"blockstat\":{\"virtio0\":{\"unmap_operations\":0,\"wr_total_time_ns\":20575975107230,\"invalid_wr_operations\":0,\"flush_operations\":24613261,\"timed_stats\":[],\"invalid_unmap_operations\":0,\"unmap_merged\":0,\"account_invalid\":true,\"unmap_bytes\":0,\"failed_wr_operations\":0,\"rd_total_time_ns\":26747993383592,\"invalid_flush_operations\":0,\"failed_unmap_operations\":0,\"account_failed\":true,\"wr_highest_offset\":213914746880,\"invalid_rd_operations\":0,\"rd_merged\":2131077,\"idle_time_ns\":1665014776,\"wr_operations\":168970494,\"failed_rd_operations\":0,\"rd_bytes\":2780566379008,\"unmap_total_time_ns\":0,\"failed_flush_operations\":0,\"wr_bytes\":2234110811136,\"flush_total_time_ns\":2361832071792,\"rd_operations\":85591355,\"wr_merged\":1587908}},\"pid\":\"46423\",\"ha\":{\"managed\":0},\"proxmox-support\":{\"pbs-dirty-bitmap\":true,\"query-bitmap-info\":true,\"pbs-library-version\":\"1.0.3 (8de935110ed4cab743f6c9437357057f9f9f08ea)\",\"pbs-dirty-bitmap-migration\":true,\"pbs-dirty-bitmap-savevm\":true,\"pbs-masterkey\":true},\"mem\":14969253354,\"running-qemu\":\"5.2.0\",\"name\":\"web02.qhotels.kloud.co.uk\",\"cpus\":8,\"qmpstatus\":\"running\",\"status\":\"running\",\"diskwrite\":2234110811136,\"netout\":33618584613194,\"diskread\":2780566379008,\"maxdisk\":0,\"nics\":{\"tap146i0\":{\"netout\":\"33618584613194\",\"netin\":\"36439445868772\"}}}}" tf_http_res_status_code=200 @caller=github.com/hashicorp/terraform-plugin-sdk/v2@v2.29.0/helper/logging/logging_http_transport.go:162 Cache-Control=max-age=0 Date="Thu, 26 Oct 2023 09:35:55 GMT" tf_resource_type=proxmox_virtual_environment_vm Pragma=no-cache tf_http_res_status_reason="200 OK" tf_req_id=c3cf43e4-2875-575d-202d-fc31a2401250 tf_rpc=ReadResource @module=proxmox Content-Type=application/json;charset=UTF-8 Server=pve-api-daemon/3.0 timestamp="2023-10-26T10:35:56.283+0100"

Have a feeling this maybe potentially related to the issue that was reported earlier for lxc containers here: https://github.com/bpg/terraform-provider-proxmox/issues/552

ratiborusx commented 10 months ago

@mattd-krystal It works for me on 0.36.0 and PVE 8.0.3. Maybe you should try to update your PVE instance? I'm not sure we even know the oldest/lowest PVE version that's supported by provider (maybe @bpg does). If I'm not mistaken unmarshal error in Go appears when there's a variable type mismatch or type conversion failure. I created VM then removed it from state file and imported back:

ratiborus@HOMEWORLD:~/WORKSPACE/terraform_yandex/proxmox$ terraform state rm module.proxmox.proxmox_virtual_environment_vm.base
Removed module.proxmox.proxmox_virtual_environment_vm.base
Successfully removed 1 resource instance(s).
ratiborus@HOMEWORLD:~/WORKSPACE/terraform_yandex/proxmox$ terraform import module.proxmox.proxmox_virtual_environment_vm.base prox-srv1/104
module.proxmox.data.proxmox_virtual_environment_vms.template_vms: Reading...
module.proxmox.proxmox_virtual_environment_vm.base: Importing from ID "prox-srv1/104"...
module.proxmox.proxmox_virtual_environment_vm.base: Import prepared!
  Prepared proxmox_virtual_environment_vm for import
module.proxmox.proxmox_virtual_environment_vm.base: Refreshing state... [id=104]
module.proxmox.data.proxmox_virtual_environment_vms.template_vms: Read complete after 0s [id=b496787d-1806-4d45-8253-2194665568be]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

@bpg There may also be another problem with import though - after importing VM as above the 'disk {}' and 'clone {}' arguments are gone and it triggers VM reprovisioning. There's also a bunch of other arguments that are missing but at least they don't trigger resource destruction. I also see we've lost 'vm_id' argument which is usually (always?) the same as 'id' one which is present. Maybe that's why there's reprovisioning triggered - we're kinda losing ownership of disk (at least from terraform's POV)?

ratiborus@HOMEWORLD:~/WORKSPACE/terraform_yandex/proxmox$ terraform state show module.proxmox.proxmox_virtual_environment_vm.base
# module.proxmox.proxmox_virtual_environment_vm.base:
resource "proxmox_virtual_environment_vm" "base" {
    acpi                    = true
    bios                    = "seabios"
    description             = "Managed by Terraform"
    id                      = "104"
    ipv4_addresses          = [
        [
            "127.0.0.1",
        ],
        [
            "10.177.144.56",
        ],
    ]
    ipv6_addresses          = [
        [
            "::1",
        ],
        [
            "fe80::bc29:a9ff:fe96:291",
        ],
    ]
    keyboard_layout         = "en-us"
    mac_addresses           = [
        "00:00:00:00:00:00",
        "BE:29:A9:96:02:91",
    ]
    name                    = "astra-test-vm"
    network_interface_names = [
        "lo",
        "eth0",
    ]
    node_name               = "prox-srv1"
    scsi_hardware           = "virtio-scsi-single"
    started                 = true
    tablet_device           = true
    tags                    = [
        "astra-1.7.4-base",
        "terraform",
    ]
    template                = false

    agent {
        enabled = true
        timeout = "15m"
        trim    = true
        type    = "virtio"
    }

    cpu {
        architecture = "x86_64"
        cores        = 2
        flags        = []
        hotplugged   = 0
        numa         = true
        sockets      = 2
        type         = "host"
        units        = 1024
    }

    initialization {
        datastore_id = "vzdata"
        interface    = "ide2"

        ip_config {
            ipv4 {
                address = "dhcp"
            }
        }
    }

    memory {
        dedicated = 4096
        floating  = 0
        shared    = 0
    }

    network_device {
        bridge      = "vmbr0"
        enabled     = true
        firewall    = false
        mac_address = "BE:29:A9:96:02:91"
        model       = "virtio"
        mtu         = 0
        queues      = 0
        rate_limit  = 0
        vlan_id     = 0
    }

    operating_system {
        type = "l26"
    }

    serial_device {
        device = "socket"
    }

    vga {
        enabled = true
        memory  = 32
        type    = "serial0"
    }
}

ratiborus@HOMEWORLD:~/WORKSPACE/terraform_yandex/proxmox$ terraform apply
module.proxmox.data.proxmox_virtual_environment_vms.template_vms: Reading...
module.proxmox.proxmox_virtual_environment_file.cloud_image_astra_174_base: Refreshing state... [id=local:iso/alse-vanilla-1.7.4-cloud-base.img]
module.proxmox.proxmox_virtual_environment_file.cloud_config_userdata_raw_file["prox-srv1"]: Refreshing state... [id=local:snippets/userdata-proxmox-raw-file.yml]
module.proxmox.proxmox_virtual_environment_file.cloud_image_debian_12: Refreshing state... [id=local:iso/debian-12-generic-amd64.img]
module.proxmox.proxmox_virtual_environment_file.cloud_image_ubuntu_2204: Refreshing state... [id=local:iso/ubuntu-22.04-server-cloudimg-amd64.img]
module.proxmox.proxmox_virtual_environment_pool.templates: Refreshing state... [id=templates]
module.proxmox.proxmox_virtual_environment_file.cloud_config_userdata_raw_file["prox-srv2"]: Refreshing state... [id=local:snippets/userdata-proxmox-raw-file.yml]
module.proxmox.proxmox_virtual_environment_file.cloud_config_userdata_raw_file["prox-srv3"]: Refreshing state... [id=local:snippets/userdata-proxmox-raw-file.yml]
module.proxmox.data.proxmox_virtual_environment_vms.template_vms: Read complete after 0s [id=bf551716-a176-4d74-b9bb-e4227bdcde66]
module.proxmox.proxmox_virtual_environment_vm.template_astra_174_base: Refreshing state... [id=1001]
module.proxmox.proxmox_virtual_environment_vm.base: Refreshing state... [id=104]
module.proxmox.proxmox_virtual_environment_vm.template_ubuntu_2204: Refreshing state... [id=1011]
module.proxmox.proxmox_virtual_environment_vm.base2: Refreshing state... [id=105]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # module.proxmox.proxmox_virtual_environment_vm.base must be replaced
-/+ resource "proxmox_virtual_environment_vm" "base" {
      ~ id                      = "104" -> (known after apply)
      ~ ipv4_addresses          = [
          - [
              - "127.0.0.1",
            ],
          - [
              - "10.177.144.56",
            ],
        ] -> (known after apply)
      ~ ipv6_addresses          = [
          - [
              - "::1",
            ],
          - [
              - "fe80::bc29:a9ff:fe96:291",
            ],
        ] -> (known after apply)
      ~ mac_addresses           = [
          - "00:00:00:00:00:00",
          - "BE:29:A9:96:02:91",
        ] -> (known after apply)
      + migrate                 = true
        name                    = "astra-test-vm"
      ~ network_interface_names = [
          - "lo",
          - "eth0",
        ] -> (known after apply)
      + on_boot                 = true
      + reboot                  = true
        tags                    = [
            "astra-1.7.4-base",
            "terraform",
        ]
      + timeout_clone           = 1800
      + timeout_create          = 1800
      + timeout_migrate         = 1800
      + timeout_move_disk       = 1800
      + timeout_reboot          = 1800
      + timeout_shutdown_vm     = 1800
      + timeout_start_vm        = 1800
      + timeout_stop_vm         = 300
      + vm_id                   = (known after apply)
        # (9 unchanged attributes hidden)

      + clone {
          + full      = true # forces replacement
          + node_name = "prox-srv1" # forces replacement
          + retries   = 3 # forces replacement
          + vm_id     = 1001 # forces replacement
        }

      ~ cpu {
          - flags        = [] -> null
            # (7 unchanged attributes hidden)
        }

      + disk { # forces replacement
          + cache             = "none"
          + datastore_id      = "vzdata"
          + discard           = "on"
          + file_format       = "qcow2" # forces replacement
          + interface         = "scsi0"
          + iothread          = true
          + path_in_datastore = (known after apply)
          + size              = 50
          + ssd               = true
        }

      ~ network_device {
          ~ mac_address = "BE:29:A9:96:02:91" -> (known after apply)
            # (8 unchanged attributes hidden)
        }

      - vga {
          - enabled = true -> null
          - memory  = 32 -> null
          - type    = "serial0" -> null
        }

        # (5 unchanged blocks hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.
mattd-krystal commented 10 months ago

@ratiborusx Believe me I would love to update my PVE instance to the latest version, unfortunately at least for the forseeable future I have to keep it at this version. I was originally trying to build a module with this but run into this issue.

@bpg If you are able to, would you mind confirming what versions of PVE are supported with this provider? If my version is a bit too far out of date, happy to close this issue.

ratiborusx commented 10 months ago

@mattd-krystal Just recently with the latest PR's docs were updated to include more info in requirements. I don't have good news for you, but maybe there's some workarounds... https://github.com/bpg/terraform-provider-proxmox#requirements image

bpg commented 10 months ago

Hi @mattd-krystal,

Per README.md

This provider is compatible with the latest version of Proxmox VE (currently 8.0). While it may work with older 7.x versions, it is not guaranteed to do so.

Practically, most of the 7.x APIs should work with the current provider version, however acceptance tests are run only against 8.x. To my knowledge, the difference between 6.x and 8.x is more severe, though I never used 6.x in my lab. But funny enough, one of the first changes that I've made to this provider was to fix GetStatusResponseData.data.pid field that was causing issue on my 7.x PVE back then.

@ratiborusx Resources import is a quite recent (and probably the least tested) feature of the provider. There is no much acceptance testing around it, and personally I'm not using imports at all, so if some arguments got missed / scrambled at import it will be harder to spot and fix. Imports definitely need some more love and polishing.

mattd-krystal commented 10 months ago

@bpg @ratiborusx Right okay, thank you both for your comments and assistance, since my version of PVE is too out of date for this, think I will close this issue for now, it's out of scope for the provider and the actual version is now EOL as well.

Might be worth raising a separate issue for the import functionality that you were discussing in previous comments?