feat: REST API for cable VLAN assignments
- New endpoint /api/plugins/utilities/cable-vlans/ to read and write the connection VLANs of a cable (cable ID and VLAN IDs, filter with ?cable_id=) - Used by NetBox Synchronizer to keep the cable VLANs of switch ports in step with the port - Version 0.31.0 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
280c0e0315
commit
1d6b16c0e4
6 files changed
+36
-5
No files matched your search
@@ -667,6 +667,10 @@ interfaces, since both ends can use different interface modes.
|
|||||||
- An optional **VLAN group** field acts as a dynamic filter for the VLAN selection; it is not stored.
|
- An optional **VLAN group** field acts as a dynamic filter for the VLAN selection; it is not stored.
|
||||||
- Empty selections create no record; deleting a connection removes its VLAN assignment.
|
- Empty selections create no record; deleting a connection removes its VLAN assignment.
|
||||||
- With NetBox-Export installed, the assignments are exported and imported together with connections and VLANs.
|
- With NetBox-Export installed, the assignments are exported and imported together with connections and VLANs.
|
||||||
|
- Cable VLANs can be read and written through `/api/plugins/utilities/cable-vlans/`
|
||||||
|
(`cable` = cable ID, `vlans` = VLAN IDs, filter with `?cable_id=`; permissions
|
||||||
|
`netbox_utilities.*_cablevlanassignment`). NetBox Synchronizer uses it to keep
|
||||||
|
the cable VLANs of switch ports in step with the port.
|
||||||
|
|
||||||
### Multiple devices in one rack unit
|
### Multiple devices in one rack unit
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from netbox.plugins import PluginConfig, get_plugin_config
|
from netbox.plugins import PluginConfig, get_plugin_config
|
||||||
|
|
||||||
__version__ = "0.30.0"
|
__version__ = "0.31.0"
|
||||||
|
|
||||||
|
|
||||||
class NetBoxUtilitiesConfig(PluginConfig):
|
class NetBoxUtilitiesConfig(PluginConfig):
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ from netbox.api.serializers import NetBoxModelSerializer
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from tenancy.api.serializers import TenantSerializer
|
from tenancy.api.serializers import TenantSerializer
|
||||||
|
|
||||||
from dcim.models import Device
|
from dcim.models import Cable, Device
|
||||||
|
from ipam.models import VLAN
|
||||||
|
|
||||||
from ..models import CPUModel, DeviceHardware, Disk, DiskBayLayout, DiskBayTemplate, RAIDGroup, SwitchStack
|
from ..models import CableVLANAssignment, CPUModel, DeviceHardware, Disk, DiskBayLayout, DiskBayTemplate, RAIDGroup, SwitchStack
|
||||||
|
|
||||||
LAYOUT_FIELDS = ("name", "weight", "slot_count", "rows", "first_slot", "numbering", "form_factor")
|
LAYOUT_FIELDS = ("name", "weight", "slot_count", "rows", "first_slot", "numbering", "form_factor")
|
||||||
|
|
||||||
@@ -215,3 +216,14 @@ class SwitchStackSerializer(serializers.ModelSerializer):
|
|||||||
if device is not None and any(member.pk == device.pk for member in data.get("members") or []):
|
if device is not None and any(member.pk == device.pk for member in data.get("members") or []):
|
||||||
raise serializers.ValidationError({"members": "Der Switch kann nicht sein eigenes Mitglied sein."})
|
raise serializers.ValidationError({"members": "Der Switch kann nicht sein eigenes Mitglied sein."})
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class CableVLANAssignmentSerializer(serializers.ModelSerializer):
|
||||||
|
"""VLANs of a cable connection as VLAN IDs (one assignment per cable)."""
|
||||||
|
|
||||||
|
cable = serializers.PrimaryKeyRelatedField(queryset=Cable.objects.all())
|
||||||
|
vlans = serializers.PrimaryKeyRelatedField(queryset=VLAN.objects.all(), many=True, required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = CableVLANAssignment
|
||||||
|
fields = ("id", "cable", "vlans", "updated")
|
||||||
@@ -12,5 +12,6 @@ router.register("disk-bay-templates", views.DiskBayTemplateViewSet)
|
|||||||
router.register("cpu-models", views.CPUModelViewSet)
|
router.register("cpu-models", views.CPUModelViewSet)
|
||||||
router.register("device-hardware", views.DeviceHardwareViewSet)
|
router.register("device-hardware", views.DeviceHardwareViewSet)
|
||||||
router.register("switch-stacks", views.SwitchStackViewSet)
|
router.register("switch-stacks", views.SwitchStackViewSet)
|
||||||
|
router.register("cable-vlans", views.CableVLANAssignmentViewSet)
|
||||||
|
|
||||||
urlpatterns = router.urls
|
urlpatterns = router.urls
|
||||||
@@ -3,7 +3,7 @@ from netbox.api.viewsets import NetBoxModelViewSet
|
|||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
|
||||||
from .. import disk_filtersets, hardware_filtersets
|
from .. import disk_filtersets, hardware_filtersets
|
||||||
from ..models import CPUModel, DeviceHardware, Disk, DiskBayLayout, DiskBayTemplate, RAIDGroup, SwitchStack
|
from ..models import CableVLANAssignment, CPUModel, DeviceHardware, Disk, DiskBayLayout, DiskBayTemplate, RAIDGroup, SwitchStack
|
||||||
from . import serializers
|
from . import serializers
|
||||||
|
|
||||||
|
|
||||||
@@ -58,3 +58,17 @@ class SwitchStackViewSet(ModelViewSet):
|
|||||||
if params.get("member_id"):
|
if params.get("member_id"):
|
||||||
queryset = queryset.filter(members__in=params.getlist("member_id")).distinct()
|
queryset = queryset.filter(members__in=params.getlist("member_id")).distinct()
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class CableVLANAssignmentViewSet(ModelViewSet):
|
||||||
|
"""VLANs of cable connections; filter with ``?cable_id=``."""
|
||||||
|
|
||||||
|
queryset = CableVLANAssignment.objects.prefetch_related("vlans").order_by("pk")
|
||||||
|
serializer_class = serializers.CableVLANAssignmentSerializer
|
||||||
|
permission_classes = [TokenPermissions]
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
queryset = super().get_queryset()
|
||||||
|
if self.request.query_params.get("cable_id"):
|
||||||
|
queryset = queryset.filter(cable_id__in=self.request.query_params.getlist("cable_id"))
|
||||||
|
return queryset
|
||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "netbox-utilities"
|
name = "netbox-utilities"
|
||||||
version = "0.30.0"
|
version = "0.31.0"
|
||||||
description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7"
|
description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.12"
|
requires-python = ">=3.12"
|
||||||
|
|||||||
Reference in new issue
Block a user