From c5cd87f85cbd9fedf06559c852c0729dae404986 Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Sat, 3 Feb 2024 16:00:04 +0100 Subject: [PATCH] Create VM resources in Netbox --- .../modules/supernode/management-ipv6.tf | 6 ++ terraform/modules/supernode/variables.tf | 12 ++++ terraform/modules/supernode/vm.tf | 56 ++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/terraform/modules/supernode/management-ipv6.tf b/terraform/modules/supernode/management-ipv6.tf index 134d5d3..da31858 100644 --- a/terraform/modules/supernode/management-ipv6.tf +++ b/terraform/modules/supernode/management-ipv6.tf @@ -13,6 +13,12 @@ resource "netbox_ip_address" "management_ipv6" { description = "Management Address ${var.supernode_name}" + virtual_machine_interface_id = netbox_interface.eth0.id tags = toset(var.tags) } + +resource "netbox_primary_ip" "supernode" { + ip_address_id = netbox_ip_address.management_ipv6.id + virtual_machine_id = netbox_virtual_machine.supernode.id +} diff --git a/terraform/modules/supernode/variables.tf b/terraform/modules/supernode/variables.tf index 2172a4d..ab6dd90 100644 --- a/terraform/modules/supernode/variables.tf +++ b/terraform/modules/supernode/variables.tf @@ -58,3 +58,15 @@ variable "vm_ssh_keys" { type = list(string) description = "Public keys to grant access to" } + +variable "vm_cluster_name" { + type = string + description = "Cluster to assign the VM to" + default = "pve1" +} + +variable "vm_role_name" { + type = string + description = "Name of an existing Netbox device role to assign to the VM" + default = "Supernode v2" +} diff --git a/terraform/modules/supernode/vm.tf b/terraform/modules/supernode/vm.tf index f93af56..72d71c0 100644 --- a/terraform/modules/supernode/vm.tf +++ b/terraform/modules/supernode/vm.tf @@ -1,5 +1,9 @@ +locals { + vm_name = "supernode-${var.supernode_name}" +} + resource "proxmox_vm_qemu" "supernode" { - name = "supernode-${var.supernode_name}" + name = local.vm_name target_node = var.vm_target_node pool = var.vm_resource_pool desc = "Supernode v2 - ${var.supernode_name}" @@ -52,3 +56,53 @@ resource "proxmox_vm_qemu" "supernode" { ] } } + +data "netbox_cluster" "vm_cluster" { + name = var.vm_cluster_name +} + +data "netbox_device_role" "supernode" { + name = var.vm_role_name +} + +locals { + size_constants = { + G = 1024 * 1024 * 1024 + M = 1024 * 1024 + K = 1024 + } + disk_size_parts = regex("^([0-9]+)([GMK])$", proxmox_vm_qemu.supernode.disk[0].size) + disk_size_bytes = parseint(local.disk_size_parts[0], 10) * local.size_constants[local.disk_size_parts[1]] +} + +resource "netbox_virtual_machine" "supernode" { + cluster_id = data.netbox_cluster.vm_cluster.id + name = local.vm_name + status = "staged" + role_id = data.netbox_device_role.supernode.id + + vcpus = proxmox_vm_qemu.supernode.cores + memory_mb = proxmox_vm_qemu.supernode.memory + disk_size_gb = local.disk_size_bytes / local.size_constants.G + + custom_fields = { + proxmox_vm_id = proxmox_vm_qemu.supernode.vmid + } + + tags = toset(var.tags) + + lifecycle { + ignore_changes = [ + status, + ] + } +} + +resource "netbox_interface" "eth0" { + virtual_machine_id = netbox_virtual_machine.supernode.id + + name = "eth0" + mac_address = proxmox_vm_qemu.supernode.network[0].macaddr + + tags = toset(var.tags) +}