Files
Netbox-Utilities/netbox_utilities/disk_integration.py
T
MrBlakeandClaude Opus 5.5 d45afa5e54 feat: several slot areas per device, e.g. front bays and a PCIe card
Disks that are not in the front bays, such as NVMe SSDs on a PCIe card or
M.2 SSDs on a boot card, can now be documented in their own slot area.

- A device can have several areas, each with name, order, slot count,
  rows, first slot, numbering and form factor; the tab shows one grid per
  area with its own fill, select and edit actions.
- Add area, edit area and delete empty areas on the Disks tab.
- Slot numbers are unique per area; drag and drop also moves or swaps
  disks between areas.
- Device type templates store all areas; Apply template creates the
  missing ones.
- Disks get a bay field in forms, CSV import (bay column, optional when
  the device has one area), REST API, GraphQL, filters and tables.
- Migration 0010 turns every existing layout into the area "Front" and
  assigns the existing disks to it.
- The grouped disk overview shows the area in its own column and centres
  the slot number.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 14:46:48 +02:00

66 lines
2.0 KiB
Python

import django_tables2 as tables
from django.db.models.signals import post_save
COLUMN_NAME = "netbox_utilities_disks"
def sync_disk_tenants(sender, instance, raw=False, **kwargs):
"""Keep the tenant of all disks in line with their device."""
if raw or not instance.pk:
return
from .models import Disk
Disk.objects.filter(device_id=instance.pk).exclude(tenant_id=instance.tenant_id).update(
tenant_id=instance.tenant_id
)
class DeviceDisksColumn(tables.Column):
"""Shows used/total slots for devices with a disk layout, linked to the tab."""
@staticmethod
def occupancy(record):
from django.db.models import Sum
from . import disks
from .models import Disk, DiskBayLayout
slot_count = DiskBayLayout.objects.filter(device=record).aggregate(total=Sum("slot_count"))["total"]
if not slot_count:
return None
used = Disk.objects.filter(device=record, status__in=disks.SLOT_OCCUPYING_STATUSES).count()
return f"{used}/{slot_count}"
def render(self, record):
from django.urls import reverse
from django.utils.html import format_html
occupancy = self.occupancy(record)
if occupancy is None:
return "—"
return format_html('<a href="{}">{}</a>', reverse("dcim:device_disks", kwargs={"pk": record.pk}), occupancy)
def value(self, record):
return self.occupancy(record) or ""
def install_disk_support():
from dcim.models import Device
from dcim.tables import DeviceTable
from utilities.tables import register_table_column
# Registers the "Datenträger" device tab before NetBox builds the dcim URLs.
from . import disk_views # noqa: F401
post_save.connect(
sync_disk_tenants,
sender=Device,
dispatch_uid="netbox_utilities.sync_disk_tenants",
weak=False,
)
register_table_column(
DeviceDisksColumn(accessor=tables.A("pk"), verbose_name="Datenträger", orderable=False, empty_values=()),
COLUMN_NAME,
DeviceTable,
)