You've already forked netbox-proxmox-power-button
added proxmox power button plugin 0.1
This commit is contained in:
40
proxmox_power_button/signals.py
Normal file
40
proxmox_power_button/signals.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
# Copyright (C) 2026 Bryan Joshua Pedini
|
||||
"""
|
||||
Signal receivers for the proxmox_power_button plugin.
|
||||
|
||||
Enforce that a VMID is unique *within its cluster*. This runs on post_clean —
|
||||
fired during full_clean() — so it surfaces as a normal validation error in both
|
||||
the UI forms and the REST API, covering VM creation, VMID edits, and moving a VM
|
||||
to a different cluster.
|
||||
|
||||
(The custom fields themselves are provisioned by migrations/0001_initial.py.)
|
||||
"""
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.dispatch import receiver
|
||||
|
||||
from netbox.signals import post_clean
|
||||
from virtualization.models import VirtualMachine
|
||||
|
||||
VMID_FIELD = "vmid"
|
||||
|
||||
|
||||
@receiver(post_clean, sender=VirtualMachine)
|
||||
def enforce_vmid_unique_per_cluster(sender, instance, **kwargs):
|
||||
data = instance.custom_field_data or {}
|
||||
vmid = data.get(VMID_FIELD)
|
||||
# Nothing to check until both a VMID and a cluster are set.
|
||||
if vmid is None or instance.cluster_id is None:
|
||||
return
|
||||
|
||||
duplicate = (
|
||||
VirtualMachine.objects
|
||||
.filter(cluster_id=instance.cluster_id, custom_field_data__vmid=vmid)
|
||||
.exclude(pk=instance.pk or 0)
|
||||
.first()
|
||||
)
|
||||
if duplicate is not None:
|
||||
raise ValidationError(
|
||||
f"VMID {vmid} is already assigned to VM '{duplicate.name}' in "
|
||||
f"cluster '{instance.cluster}'. VMIDs must be unique within a cluster."
|
||||
)
|
||||
Reference in New Issue
Block a user