mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Introduced 'cable' field on CableTermination to cache connected Cable
This commit is contained in:
parent
dc67028bd9
commit
e21b23cd98
@ -20,6 +20,7 @@ def console_connections_to_cables(apps, schema_editor):
|
|||||||
print("\n Adding console connections... ", end='', flush=True)
|
print("\n Adding console connections... ", end='', flush=True)
|
||||||
for consoleport in ConsolePort.objects.filter(connected_endpoint__isnull=False):
|
for consoleport in ConsolePort.objects.filter(connected_endpoint__isnull=False):
|
||||||
c = Cable()
|
c = Cable()
|
||||||
|
|
||||||
# We have to assign GFK fields manually because we're inside a migration.
|
# We have to assign GFK fields manually because we're inside a migration.
|
||||||
c.termination_a_type = consoleport_type
|
c.termination_a_type = consoleport_type
|
||||||
c.termination_a_id = consoleport.id
|
c.termination_a_id = consoleport.id
|
||||||
@ -28,6 +29,10 @@ def console_connections_to_cables(apps, schema_editor):
|
|||||||
c.connection_status = consoleport.connection_status
|
c.connection_status = consoleport.connection_status
|
||||||
c.save()
|
c.save()
|
||||||
|
|
||||||
|
# Cache the Cable on its two termination points (replicate Cable.save())
|
||||||
|
ConsolePort.objects.filter(pk=consoleport.id).update(cable=c)
|
||||||
|
ConsoleServerPort.objects.filter(pk=consoleport.connected_endpoint_id).update(cable=c)
|
||||||
|
|
||||||
cable_count = Cable.objects.filter(termination_a_type=consoleport_type).count()
|
cable_count = Cable.objects.filter(termination_a_type=consoleport_type).count()
|
||||||
print("{} cables created".format(cable_count))
|
print("{} cables created".format(cable_count))
|
||||||
|
|
||||||
@ -49,6 +54,7 @@ def power_connections_to_cables(apps, schema_editor):
|
|||||||
print(" Adding power connections... ", end='', flush=True)
|
print(" Adding power connections... ", end='', flush=True)
|
||||||
for powerport in PowerPort.objects.filter(connected_endpoint__isnull=False):
|
for powerport in PowerPort.objects.filter(connected_endpoint__isnull=False):
|
||||||
c = Cable()
|
c = Cable()
|
||||||
|
|
||||||
# We have to assign GFK fields manually because we're inside a migration.
|
# We have to assign GFK fields manually because we're inside a migration.
|
||||||
c.termination_a_type = powerport_type
|
c.termination_a_type = powerport_type
|
||||||
c.termination_a_id = powerport.id
|
c.termination_a_id = powerport.id
|
||||||
@ -57,6 +63,10 @@ def power_connections_to_cables(apps, schema_editor):
|
|||||||
c.connection_status = powerport.connection_status
|
c.connection_status = powerport.connection_status
|
||||||
c.save()
|
c.save()
|
||||||
|
|
||||||
|
# Cache the Cable on its two termination points (replicate Cable.save())
|
||||||
|
PowerPort.objects.filter(pk=powerport.id).update(cable=c)
|
||||||
|
PowerOutlet.objects.filter(pk=powerport.connected_endpoint_id).update(cable=c)
|
||||||
|
|
||||||
cable_count = Cable.objects.filter(termination_a_type=powerport_type).count()
|
cable_count = Cable.objects.filter(termination_a_type=powerport_type).count()
|
||||||
print("{} cables created".format(cable_count))
|
print("{} cables created".format(cable_count))
|
||||||
|
|
||||||
@ -77,11 +87,14 @@ def interface_connections_to_cables(apps, schema_editor):
|
|||||||
print(" Adding interface connections... ", end='', flush=True)
|
print(" Adding interface connections... ", end='', flush=True)
|
||||||
for conn in InterfaceConnection.objects.all():
|
for conn in InterfaceConnection.objects.all():
|
||||||
c = Cable()
|
c = Cable()
|
||||||
|
|
||||||
# We have to assign GFK fields manually because we're inside a migration.
|
# We have to assign GFK fields manually because we're inside a migration.
|
||||||
c.termination_a_type = interface_type
|
c.termination_a_type = interface_type
|
||||||
c.termination_a_id = conn.interface_a_id
|
c.termination_a_id = conn.interface_a_id
|
||||||
|
c.termination_a = conn.interface_a
|
||||||
c.termination_b_type = interface_type
|
c.termination_b_type = interface_type
|
||||||
c.termination_b_id = conn.interface_b_id
|
c.termination_b_id = conn.interface_b_id
|
||||||
|
c.termination_b = conn.interface_b
|
||||||
c.connection_status = conn.connection_status
|
c.connection_status = conn.connection_status
|
||||||
c.save()
|
c.save()
|
||||||
|
|
||||||
@ -89,11 +102,13 @@ def interface_connections_to_cables(apps, schema_editor):
|
|||||||
# since these are new fields on Interface
|
# since these are new fields on Interface
|
||||||
Interface.objects.filter(pk=conn.interface_a_id).update(
|
Interface.objects.filter(pk=conn.interface_a_id).update(
|
||||||
connected_endpoint=conn.interface_b_id,
|
connected_endpoint=conn.interface_b_id,
|
||||||
connection_status=conn.connection_status
|
connection_status=conn.connection_status,
|
||||||
|
cable=c
|
||||||
)
|
)
|
||||||
Interface.objects.filter(pk=conn.interface_b_id).update(
|
Interface.objects.filter(pk=conn.interface_b_id).update(
|
||||||
connected_endpoint=conn.interface_a_id,
|
connected_endpoint=conn.interface_a_id,
|
||||||
connection_status=conn.connection_status
|
connection_status=conn.connection_status,
|
||||||
|
cable=c
|
||||||
)
|
)
|
||||||
|
|
||||||
cable_count = Cable.objects.filter(termination_a_type=interface_type).count()
|
cable_count = Cable.objects.filter(termination_a_type=interface_type).count()
|
||||||
@ -146,11 +161,21 @@ class Migration(migrations.Migration):
|
|||||||
name='connected_endpoint',
|
name='connected_endpoint',
|
||||||
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='connected_endpoint', to='dcim.ConsoleServerPort'),
|
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='connected_endpoint', to='dcim.ConsoleServerPort'),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='consoleport',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='consoleserverport',
|
model_name='consoleserverport',
|
||||||
name='device',
|
name='device',
|
||||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='consoleserverports', to='dcim.Device'),
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='consoleserverports', to='dcim.Device'),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='consoleserverport',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
|
|
||||||
# Alter power port models
|
# Alter power port models
|
||||||
migrations.RenameField(
|
migrations.RenameField(
|
||||||
@ -163,11 +188,21 @@ class Migration(migrations.Migration):
|
|||||||
name='connected_endpoint',
|
name='connected_endpoint',
|
||||||
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='connected_endpoint', to='dcim.PowerOutlet'),
|
field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='connected_endpoint', to='dcim.PowerOutlet'),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='powerport',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='poweroutlet',
|
model_name='poweroutlet',
|
||||||
name='device',
|
name='device',
|
||||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='poweroutlets', to='dcim.Device'),
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='poweroutlets', to='dcim.Device'),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='poweroutlet',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
|
|
||||||
# Alter the Interface model
|
# Alter the Interface model
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
@ -180,6 +215,23 @@ class Migration(migrations.Migration):
|
|||||||
name='connection_status',
|
name='connection_status',
|
||||||
field=models.NullBooleanField(default=True),
|
field=models.NullBooleanField(default=True),
|
||||||
),
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='interface',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
|
|
||||||
|
# Alter front/rear port models
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='frontport',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='rearport',
|
||||||
|
name='cable',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='dcim.Cable'),
|
||||||
|
),
|
||||||
|
|
||||||
# Copy console/power/interface connections as Cables
|
# Copy console/power/interface connections as Cables
|
||||||
migrations.RunPython(console_connections_to_cables),
|
migrations.RunPython(console_connections_to_cables),
|
||||||
|
@ -66,6 +66,12 @@ class ComponentModel(models.Model):
|
|||||||
|
|
||||||
|
|
||||||
class CableTermination(models.Model):
|
class CableTermination(models.Model):
|
||||||
|
cable = models.ForeignKey(
|
||||||
|
to='dcim.Cable',
|
||||||
|
on_delete=models.SET_NULL,
|
||||||
|
blank=True,
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
@ -2339,6 +2345,12 @@ class Cable(ChangeLoggedModel):
|
|||||||
|
|
||||||
super(Cable, self).save(*args, **kwargs)
|
super(Cable, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
# Cache the Cable on its two termination points
|
||||||
|
self.termination_a.cable = self
|
||||||
|
self.termination_a.save()
|
||||||
|
self.termination_b.cable = self
|
||||||
|
self.termination_b.save()
|
||||||
|
|
||||||
def get_path_endpoints(self):
|
def get_path_endpoints(self):
|
||||||
"""
|
"""
|
||||||
Traverse both ends of a cable path and return its connected endpoints. Note that one or both endpoints may be
|
Traverse both ends of a cable path and return its connected endpoints. Note that one or both endpoints may be
|
||||||
|
@ -32,7 +32,7 @@ def update_connected_endpoints(instance, **kwargs):
|
|||||||
termination_a.connection_status = True
|
termination_a.connection_status = True
|
||||||
termination_a.save()
|
termination_a.save()
|
||||||
termination_b.connected_endpoint = termination_a
|
termination_b.connected_endpoint = termination_a
|
||||||
termination_a.connection_status = True
|
termination_b.connection_status = True
|
||||||
termination_b.save()
|
termination_b.save()
|
||||||
|
|
||||||
|
|
||||||
|
@ -876,47 +876,53 @@ class DeviceView(View):
|
|||||||
|
|
||||||
# VirtualChassis members
|
# VirtualChassis members
|
||||||
if device.virtual_chassis is not None:
|
if device.virtual_chassis is not None:
|
||||||
vc_members = Device.objects.filter(virtual_chassis=device.virtual_chassis).order_by('vc_position')
|
vc_members = Device.objects.filter(
|
||||||
|
virtual_chassis=device.virtual_chassis
|
||||||
|
).order_by('vc_position')
|
||||||
else:
|
else:
|
||||||
vc_members = []
|
vc_members = []
|
||||||
|
|
||||||
# Console ports
|
# Console ports
|
||||||
console_ports = natsorted(
|
console_ports = natsorted(
|
||||||
ConsolePort.objects.filter(device=device).select_related('connected_endpoint__device'), key=attrgetter('name')
|
device.console_ports.select_related('connected_endpoint__device', 'cable'),
|
||||||
|
key=attrgetter('name')
|
||||||
)
|
)
|
||||||
|
|
||||||
# Console server ports
|
# Console server ports
|
||||||
consoleserverports = ConsoleServerPort.objects.filter(device=device).select_related('connected_endpoint__device')
|
consoleserverports = device.consoleserverports.select_related('connected_endpoint__device', 'cable')
|
||||||
|
|
||||||
# Power ports
|
# Power ports
|
||||||
power_ports = natsorted(
|
power_ports = natsorted(
|
||||||
PowerPort.objects.filter(device=device).select_related('connected_endpoint__device'), key=attrgetter('name')
|
device.power_ports.select_related('connected_endpoint__device', 'cable'),
|
||||||
|
key=attrgetter('name')
|
||||||
)
|
)
|
||||||
|
|
||||||
# Power outlets
|
# Power outlets
|
||||||
poweroutlets = PowerOutlet.objects.filter(device=device).select_related('connected_endpoint__device')
|
poweroutlets = device.poweroutlets.select_related('connected_endpoint__device', 'cable')
|
||||||
|
|
||||||
# Interfaces
|
# Interfaces
|
||||||
interfaces = device.vc_interfaces.order_naturally(
|
interfaces = device.vc_interfaces.order_naturally(
|
||||||
device.device_type.interface_ordering
|
device.device_type.interface_ordering
|
||||||
).select_related(
|
).select_related(
|
||||||
'connected_endpoint__device', 'circuit_termination__circuit'
|
'lag', 'connected_endpoint__device', 'circuit_termination__circuit', 'cable'
|
||||||
).prefetch_related('ip_addresses')
|
).prefetch_related(
|
||||||
|
'cable__termination_a', 'cable__termination_b', 'ip_addresses'
|
||||||
|
)
|
||||||
|
|
||||||
# Front ports
|
# Front ports
|
||||||
front_ports = device.front_ports.select_related('rear_port')
|
front_ports = device.front_ports.select_related('rear_port', 'cable')
|
||||||
|
|
||||||
# Rear ports
|
# Rear ports
|
||||||
rear_ports = device.rear_ports.all()
|
rear_ports = device.rear_ports.select_related('cable')
|
||||||
|
|
||||||
# Device bays
|
# Device bays
|
||||||
device_bays = natsorted(
|
device_bays = natsorted(
|
||||||
DeviceBay.objects.filter(device=device).select_related('installed_device__device_type__manufacturer'),
|
device.device_bays.select_related('installed_device__device_type__manufacturer'),
|
||||||
key=attrgetter('name')
|
key=attrgetter('name')
|
||||||
)
|
)
|
||||||
|
|
||||||
# Services
|
# Services
|
||||||
services = Service.objects.filter(device=device)
|
services = device.services.all()
|
||||||
|
|
||||||
# Secrets
|
# Secrets
|
||||||
secrets = device.secrets.all()
|
secrets = device.secrets.all()
|
||||||
|
@ -7,14 +7,11 @@
|
|||||||
|
|
||||||
{# Cable #}
|
{# Cable #}
|
||||||
<td>
|
<td>
|
||||||
{% with cable=cp.get_connected_cable %}
|
{% if cp.cable %}
|
||||||
{% if cable %}
|
<a href="{{ cp.cable.get_absolute_url }}">{{ cp.cable }}</a>
|
||||||
Cable <a href="{{ cable.get_absolute_url }}">{{ cable }}</a>
|
{% else %}
|
||||||
{% if cable.far_end != cp.connected_endpoint %}
|
—
|
||||||
to <a href="{{ cable.far_end.device.get_absolute_url }}">{{ cable.far_end.device }}</a> {{ cable.far_end }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{# Connection #}
|
{# Connection #}
|
||||||
|
@ -14,14 +14,11 @@
|
|||||||
|
|
||||||
{# Cable #}
|
{# Cable #}
|
||||||
<td>
|
<td>
|
||||||
{% with cable=csp.get_connected_cable %}
|
{% if csp.cable %}
|
||||||
{% if cable %}
|
<a href="{{ csp.cable.get_absolute_url }}">{{ csp.cable }}</a>
|
||||||
<a href="{{ cable.get_absolute_url }}">{{ cable }}</a>
|
{% else %}
|
||||||
{% if cable.far_end != csp.connected_endpoint %}
|
—
|
||||||
to <a href="{{ cable.far_end.device.get_absolute_url }}">{{ cable.far_end.device }}</a> {{ cable.far_end }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{# Connection #}
|
{# Connection #}
|
||||||
|
@ -26,18 +26,15 @@
|
|||||||
<td>{{ iface.description|default:"—" }}</td>
|
<td>{{ iface.description|default:"—" }}</td>
|
||||||
|
|
||||||
{# 802.1Q mode #}
|
{# 802.1Q mode #}
|
||||||
<td>{{ iface.get_mode_display }}</td>
|
<td>{{ iface.get_mode_display|default:"—" }}</td>
|
||||||
|
|
||||||
{# Cable #}
|
{# Cable #}
|
||||||
<td>
|
<td>
|
||||||
{% with cable=iface.get_connected_cable %}
|
{% if iface.cable %}
|
||||||
{% if cable %}
|
<a href="{{ iface.cable.get_absolute_url }}">{{ iface.cable }}</a>
|
||||||
<a href="{{ cable.get_absolute_url }}">{{ cable }}</a>
|
{% else %}
|
||||||
{% if cable.far_end != iface.connected_endpoint %}
|
—
|
||||||
to <a href="{{ cable.far_end.device.get_absolute_url }}">{{ cable.far_end.device }}</a> {{ cable.far_end }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{# Connection or type #}
|
{# Connection or type #}
|
||||||
|
@ -14,14 +14,11 @@
|
|||||||
|
|
||||||
{# Cable #}
|
{# Cable #}
|
||||||
<td>
|
<td>
|
||||||
{% with cable=po.get_connected_cable %}
|
{% if po.cable %}
|
||||||
{% if cable %}
|
<a href="{{ po.cable.get_absolute_url }}">{{ po.cable }}</a>
|
||||||
<a href="{{ cable.get_absolute_url }}">{{ cable }}</a>
|
{% else %}
|
||||||
{% if cable.far_end != po.connected_endpoint %}
|
—
|
||||||
to <a href="{{ cable.far_end.device.get_absolute_url }}">{{ cable.far_end.device }}</a> {{ cable.far_end }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{# Connection #}
|
{# Connection #}
|
||||||
|
@ -7,14 +7,11 @@
|
|||||||
|
|
||||||
{# Cable #}
|
{# Cable #}
|
||||||
<td>
|
<td>
|
||||||
{% with cable=pp.get_connected_cable %}
|
{% if pp.cable %}
|
||||||
{% if cable %}
|
<a href="{{ pp.cable.get_absolute_url }}">{{ pp.cable }}</a>
|
||||||
Cable <a href="{{ cable.get_absolute_url }}">{{ cable }}</a>
|
{% else %}
|
||||||
{% if cable.far_end != pp.connected_endpoint %}
|
—
|
||||||
to <a href="{{ cable.far_end.device.get_absolute_url }}">{{ cable.far_end.device }}</a> {{ cable.far_end }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
{% endwith %}
|
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{# Connection #}
|
{# Connection #}
|
||||||
|
Loading…
Reference in New Issue
Block a user