bpg / terraform-provider-proxmox

Terraform / OpenTofu Provider for Proxmox VE
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
892 stars 140 forks source link

Creation of standalone VM disks #1465

Open vehagn opened 3 months ago

vehagn commented 3 months ago

I would like a new resource for managing "standalone" VM disks, e.g. virtual_environment_disk.

Creating a new VM disk is possible by sending a POST call to the /api2/json/nodes/{node}/storage/{storage}/content endpoint ref https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/storage/{storage}/content

I'm using the Proxmox CSI Plugin to provision Proxmox storage backed PersistentVolumes for a Kubernetes cluster. The Proxmox CSI Plugin has the possibility to automatically create VM disks in Proxmox and mount them to the correct VM, though the volumes it creates are all named vm-9999-pvc-<UUID> which makes it difficult to distinguish which Proxmox VM disk corresponds to which Kubernetes PV.

I've been able to circumvent this issue by managing VM disks using the Mastercard/restapi provider as shown below which works adequately, though I would prefer to manage it using this provider.

terraform {
  required_providers {
    restapi = {
      source  = "Mastercard/restapi"
      version = ">= 1.19.1"
    }
  }
}

variable "proxmox_api" {
  type = object({
    endpoint  = string
    insecure  = bool
    api_token = string
  })
  sensitive = true
}

variable "volume" {
  type = object({
    name = string
    node = string
    size = string
    storage = optional(string, "local-zfs")
    vmid = optional(number, 9999)
    format = optional(string, "raw")
  })
}

locals {
  filename = "vm-${var.volume.vmid}-${var.volume.name}"
}

resource "restapi_object" "proxmox-volume" {
  path = "/api2/json/nodes/${var.volume.node}/storage/${var.volume.storage}/content/"

  id_attribute = "data"

  force_new = [var.volume.size]

  data = jsonencode({
    vmid     = var.volume.vmid
    filename = local.filename
    size     = var.volume.size
    format   = var.volume.format
  })
}

An alternative solution would be changes to Proxmox CSI Plugin to allow for named VM disks, possibly related to this issue.