mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-09 13:22:18 -06:00
Merge 85ca55e9e7 into 21f4036782
This commit is contained in:
@@ -33,6 +33,7 @@ from utilities.tracking import TrackingModelMixin
|
|||||||
from .device_components import *
|
from .device_components import *
|
||||||
from .mixins import RenderConfigMixin
|
from .mixins import RenderConfigMixin
|
||||||
from .modules import Module
|
from .modules import Module
|
||||||
|
from ..utils import update_device_components
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@@ -1012,6 +1013,8 @@ class Device(
|
|||||||
self._instantiate_components(self.device_type.inventoryitemtemplates.all(), bulk_create=False)
|
self._instantiate_components(self.device_type.inventoryitemtemplates.all(), bulk_create=False)
|
||||||
# Interface bridges have to be set after interface instantiation
|
# Interface bridges have to be set after interface instantiation
|
||||||
update_interface_bridges(self, self.device_type.interfacetemplates.all())
|
update_interface_bridges(self, self.device_type.interfacetemplates.all())
|
||||||
|
# Update denormalized fields for all components
|
||||||
|
update_device_components(self)
|
||||||
|
|
||||||
# Update Site and Rack assignment for any child Devices
|
# Update Site and Rack assignment for any child Devices
|
||||||
devices = Device.objects.filter(parent_bay__device=self)
|
devices = Device.objects.filter(parent_bay__device=self)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from netbox.models.mixins import WeightMixin
|
|||||||
from utilities.jsonschema import validate_schema
|
from utilities.jsonschema import validate_schema
|
||||||
from utilities.string import title
|
from utilities.string import title
|
||||||
from .device_components import *
|
from .device_components import *
|
||||||
|
from ..utils import update_device_components
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Module',
|
'Module',
|
||||||
@@ -347,3 +348,5 @@ class Module(PrimaryModel, ConfigContextModel):
|
|||||||
|
|
||||||
# Interface bridges have to be set after interface instantiation
|
# Interface bridges have to be set after interface instantiation
|
||||||
update_interface_bridges(self.device, self.module_type.interfacetemplates, self)
|
update_interface_bridges(self.device, self.module_type.interfacetemplates, self)
|
||||||
|
# Update denormalized fields for all components
|
||||||
|
update_device_components(self.device)
|
||||||
|
|||||||
@@ -6,25 +6,11 @@ from django.dispatch import receiver
|
|||||||
from dcim.choices import CableEndChoices, LinkStatusChoices
|
from dcim.choices import CableEndChoices, LinkStatusChoices
|
||||||
from virtualization.models import VMInterface
|
from virtualization.models import VMInterface
|
||||||
from .models import (
|
from .models import (
|
||||||
Cable, CablePath, CableTermination, ConsolePort, ConsoleServerPort, Device, DeviceBay, FrontPort, Interface,
|
Cable, CablePath, CableTermination, Device, FrontPort, Interface, PathEndpoint, PowerPanel, Rack, Location,
|
||||||
InventoryItem, ModuleBay, PathEndpoint, PowerOutlet, PowerPanel, PowerPort, Rack, RearPort, Location,
|
|
||||||
VirtualChassis,
|
VirtualChassis,
|
||||||
)
|
)
|
||||||
from .models.cables import trace_paths
|
from .models.cables import trace_paths
|
||||||
from .utils import create_cablepath, rebuild_paths
|
from .utils import create_cablepath, rebuild_paths, update_device_components
|
||||||
|
|
||||||
COMPONENT_MODELS = (
|
|
||||||
ConsolePort,
|
|
||||||
ConsoleServerPort,
|
|
||||||
DeviceBay,
|
|
||||||
FrontPort,
|
|
||||||
Interface,
|
|
||||||
InventoryItem,
|
|
||||||
ModuleBay,
|
|
||||||
PowerOutlet,
|
|
||||||
PowerPort,
|
|
||||||
RearPort,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -44,6 +30,9 @@ def handle_location_site_change(instance, created, **kwargs):
|
|||||||
Device.objects.filter(location__in=locations).update(site=instance.site)
|
Device.objects.filter(location__in=locations).update(site=instance.site)
|
||||||
PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
|
PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
|
||||||
CableTermination.objects.filter(_location__in=locations).update(_site=instance.site)
|
CableTermination.objects.filter(_location__in=locations).update(_site=instance.site)
|
||||||
|
# Update component models for devices in these locations
|
||||||
|
for device in Device.objects.filter(location__in=locations):
|
||||||
|
update_device_components(device)
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Rack)
|
@receiver(post_save, sender=Rack)
|
||||||
@@ -53,6 +42,9 @@ def handle_rack_site_change(instance, created, **kwargs):
|
|||||||
"""
|
"""
|
||||||
if not created:
|
if not created:
|
||||||
Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
|
Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
|
||||||
|
# Update component models for devices in this rack
|
||||||
|
for device in Device.objects.filter(rack=instance):
|
||||||
|
update_device_components(device)
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Device)
|
@receiver(post_save, sender=Device)
|
||||||
@@ -61,12 +53,7 @@ def handle_device_site_change(instance, created, **kwargs):
|
|||||||
Update child components to update the parent Site, Location, and Rack when a Device is saved.
|
Update child components to update the parent Site, Location, and Rack when a Device is saved.
|
||||||
"""
|
"""
|
||||||
if not created:
|
if not created:
|
||||||
for model in COMPONENT_MODELS:
|
update_device_components(instance)
|
||||||
model.objects.filter(device=instance).update(
|
|
||||||
_site=instance.site,
|
|
||||||
_location=instance.location,
|
|
||||||
_rack=instance.rack,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -76,3 +76,36 @@ def update_interface_bridges(device, interface_templates, module=None):
|
|||||||
)
|
)
|
||||||
interface.full_clean()
|
interface.full_clean()
|
||||||
interface.save()
|
interface.save()
|
||||||
|
|
||||||
|
|
||||||
|
def update_device_components(device):
|
||||||
|
"""
|
||||||
|
Update denormalized fields (_site, _location, _rack) for all component models
|
||||||
|
associated with the specified device.
|
||||||
|
|
||||||
|
:param device: Device instance whose components should be updated
|
||||||
|
"""
|
||||||
|
from dcim.models import (
|
||||||
|
ConsolePort, ConsoleServerPort, DeviceBay, FrontPort, Interface,
|
||||||
|
InventoryItem, ModuleBay, PowerOutlet, PowerPort, RearPort,
|
||||||
|
)
|
||||||
|
|
||||||
|
COMPONENT_MODELS = (
|
||||||
|
ConsolePort,
|
||||||
|
ConsoleServerPort,
|
||||||
|
DeviceBay,
|
||||||
|
FrontPort,
|
||||||
|
Interface,
|
||||||
|
InventoryItem,
|
||||||
|
ModuleBay,
|
||||||
|
PowerOutlet,
|
||||||
|
PowerPort,
|
||||||
|
RearPort,
|
||||||
|
)
|
||||||
|
|
||||||
|
for model in COMPONENT_MODELS:
|
||||||
|
model.objects.filter(device=device).update(
|
||||||
|
_site=device.site,
|
||||||
|
_location=device.location,
|
||||||
|
_rack=device.rack,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user