Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create VM resources in Netbox #13

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions terraform/modules/supernode/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ terraform {
# https://github.com/Telmate/terraform-provider-proxmox/issues/784
version = "<= 2.9.11"
}
iphelpers = {
source = "ffddorf/iphelpers"
version = "~> 1.0.0"
}
}
}
19 changes: 15 additions & 4 deletions terraform/modules/supernode/management-ipv6.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ data "netbox_prefix" "management_net" {
prefix = var.management_prefix_ipv6
}

resource "netbox_available_ip_address" "management_ipv6" {
status = "active"
data "iphelpers_eui64_address" "supernode_management" {
mac_address = proxmox_vm_qemu.supernode.network[0].macaddr
prefix = trimsuffix(data.netbox_prefix.management_net.prefix, "/64")
}

resource "netbox_ip_address" "management_ipv6" {
ip_address = "${data.iphelpers_eui64_address.supernode_management.ipv6_address}/64"
status = "slaac"

description = "Management Address ${var.supernode_name}"

prefix_id = data.netbox_prefix.management_net.id
// TODO: set interface_id
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
ip_address_version = 6
virtual_machine_id = netbox_virtual_machine.supernode.id
}
12 changes: 12 additions & 0 deletions terraform/modules/supernode/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
58 changes: 56 additions & 2 deletions terraform/modules/supernode/vm.tf
Original file line number Diff line number Diff line change
@@ -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}"
Expand Down Expand Up @@ -40,7 +44,7 @@ resource "proxmox_vm_qemu" "supernode" {

agent = 1
os_type = "cloud-init"
ipconfig0 = "ip=dhcp,ip6=${netbox_available_ip_address.management_ipv6.ip_address}"
ipconfig0 = "ip=dhcp,ip6=auto"
ciuser = "admin"
sshkeys = join("\n", var.vm_ssh_keys)

Expand All @@ -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)
}