Telmate / proxmox-api-go

Consume the proxmox API in golang
MIT License
370 stars 234 forks source link

feat: add support for unlinking disks from VM #279

Closed FredrickB closed 9 months ago

FredrickB commented 10 months ago

Added support for unlinking a disk from a VM with optional force-removal. This is the first step to fix an issue in the Proxmox Terraform provider where the disks are not removed correctly if removed from the HCL as part of a cloud-init based VM.

Testing

I have tested the changes on a local Proxmox install, version 8.0.4 with the following modified example JSON for creating the Qemu VM:

qemu1.json (supply name of iso-file uploaded to Proxmox node):

{
  "name": "golang1.test.com",
  "desc": "Test proxmox-api-go",
  "memory": 512,
  "os": "l26",
  "cores": 2,
  "sockets": 1,
  "iso": {
    "file": "iso/<some-iso>.iso",
    "storage": "local"
  },
  "disk": {
    "0": {
      "type": "virtio",
      "storage": "local",
      "storage_type": "dir",
      "size": "5G",
      "backup": true
    },
    "1": {
      "type": "virtio",
      "storage": "local",
      "storage_type": "dir",
      "size": "1G",
      "backup": true
    },
    "2": {
      "type": "virtio",
      "storage": "local",
      "storage_type": "dir",
      "size": "1G",
      "backup": true
    }
  },
  "network": {
    "0": {
      "model": "virtio",
      "bridge": "nat"
    }
  }
}
./proxmox-api-go --insecure createQemu 100 <proxmox-node-name> < qemu1.json

Scenarios

Unlink disk, but don't delete it

Should unlink the disk virtio1 and leave it as Unused Disk 0 in tab Hardware of VM

./proxmox-api-go unlink 100 <proxmox-node-name> virtio1

Unlink disk, and delete it

Should unlink the disk virtio2 and remove it from tab Hardware of VM

./proxmox-api-go unlink 100 <proxmox-node-name> virtio2 true

Unlink and delete multiple disks

Should unlink the disks virtio1 and virtio2 and remove both from tab Hardware of VM

./proxmox-api-go unlink 100 <proxmox-node-name> "virtio1,virtio2" true