terraform {
  required_providers {
    proxmox = {
      source = "Telmate/proxmox"
      version = "3.0.2-rc01"
    }
  }
}

resource "proxmox_vm_qemu" "nixos-vm" {
  vmid        = var.vmid
  name        = var.name
  target_node = var.target_node
  agent       = var.agent
  cpu {
    cores     = var.cpu_cores
  }
  memory      = var.memory
  boot        = var.boot # " " #"order=scsi0" # has to be the same as the OS disk of the template
  clone_id    = var.clone_id
  full_clone  = var.full_clone
  scsihw      = var.scsihw
  vm_state    = var.vm_state
  automatic_reboot = var.automatic_reboot

  # Cloud-Init configuration
  # cicustom   = "vendor=local:snippets/qemu-guest-agent.yml" # /var/lib/vz/snippets/qemu-guest-agent.yml
  ciupgrade  = var.ciupgrade
  ipconfig0  = var.ipconfig0
  skip_ipv6  = var.skip_ipv6
  ciuser     = var.ciuser
  cipassword = var.cipassword # "Enter123!"
  sshkeys    = var.sshkeys # "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICUP7m8jZJiclZGfSje8CeBYFhX10SrdtjYziuChmj1X plasmagoat@macbook-air"

  # Most cloud-init images require a serial device for their display
  serial {
    id = 0
  }

  disks {
    virtio {
      virtio0 {
        # We have to specify the disk from our template, else Terraform will think it's not supposed to be there
        disk {
          storage = var.disk_storage
          # The size of the disk should be at least as big as the disk in the template. If it's smaller, the disk will be recreated
          size    = var.disk_size
        }
      }
    }
    ide {
      # Some images require a cloud-init disk on the IDE controller, others on the SCSI or SATA controller
      ide1 {
        cloudinit {
          storage = var.cloudinit_storage
        }
      }
    }
  }

  network {
    id = 0
    bridge = var.network_bridge
    model  = var.network_model
  }
}
