resource "proxmox_virtual_environment_file" "this" {
  content_type = "snippets"
  datastore_id = "local"
  node_name    = var.proxmox_node_name

  source_raw {
    data      = templatefile("${path.module}/cloud-init.cfg", { })
    file_name = "backup.cloud-config.yaml"
  }
}

#resource "proxmox_virtual_environment_file" "ubuntu_cloud_image" {
#  content_type = "iso"
#  datastore_id = "local"
#  node_name    = var.proxmox_node_name
#
#  source_file {
#    path = "http://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
#  }
#}

resource "proxmox_virtual_environment_vm" "this" {
  name        = "backup"
  description = "Managed by Terraform"
  started     = true

  node_name = var.proxmox_node_name
  vm_id     = var.proxmox_vmid

  cpu {
    cores = 2
  }
  memory {
    dedicated = 1024
  }

  agent {
    enabled = true
  }

  disk {
    datastore_id = "local-lvm"
    file_format  = "qcow2"
    interface    = "scsi0"
    file_id      = "local:iso/jammy-server-cloudimg-amd64.img"
#   file_id = proxmox_virtual_environment_file.ubuntu_cloud_image.id
    size         = "20"
  }


  initialization {
    user_data_file_id = proxmox_virtual_environment_file.this.id
    datastore_id      = "local-lvm"
  }

  network_device {
    bridge = "vmbr0" # XXX VAR
  }
}