From 55f0924cc60473837571374551d154e4f0090735 Mon Sep 17 00:00:00 2001 From: "Nathan V. Patwardhan" Date: Sun, 1 Dec 2024 14:06:50 -0700 Subject: [PATCH] adds scsi0 to NetBox after Proxmox VM create --- .../helpers/netbox_proxmox.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/example-netbox-webhook-flask-app/helpers/netbox_proxmox.py b/example-netbox-webhook-flask-app/helpers/netbox_proxmox.py index c3ab34b..29b6ccb 100644 --- a/example-netbox-webhook-flask-app/helpers/netbox_proxmox.py +++ b/example-netbox-webhook-flask-app/helpers/netbox_proxmox.py @@ -1,3 +1,4 @@ +import re import pynetbox import requests import urllib @@ -180,6 +181,37 @@ def proxmox_clone_vm(self, json_in): nb_obj_update_vmid.save() except pynetbox.core.query.RequestError as e: raise e + + # set scsi0 in NetBox + try: + proxmox_vm_config = self.proxmox_api.nodes(self.proxmox_api_config['node']).qemu(new_vm_id).config.get() + + if 'bootdisk' in proxmox_vm_config: + os_disk = proxmox_vm_config['bootdisk'] + + if os_disk in proxmox_vm_config: + m = re.search(r'size=(\d+)([MG]){1}', proxmox_vm_config[os_disk]) + + if m: + disk_raw_size = m.group(1) + + if m.group(2) == 'G': + disk_size = int(disk_raw_size) * 1000 + elif m.group(2) == 'M': + disk_size = int(disk_raw_size) + + nb_obj_add_vm_disk = self.netbox_api.virtualization.virtual_disks.create( + virtual_machine=nb_obj_update_vmid['id'], + name=proxmox_vm_config['bootdisk'], + size=disk_size, + description=f"OS/boot disk for {json_in['data']['name']}" + ) + + if not nb_obj_add_vm_disk: + raise ValueError("Unable to add VM disk to NetBox") + + except pynetbox.core.query.RequestError as e: + raise e # update VM vcpus and memory if defined if json_in['data']['vcpus']: