mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Deprecated the InterfaceConnection model
This commit is contained in:
@@ -26,10 +26,9 @@ from virtualization.models import Cluster
|
||||
from .constants import *
|
||||
from .models import (
|
||||
Cable, DeviceBay, DeviceBayTemplate, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate,
|
||||
Device, DeviceRole, DeviceType, FrontPanelPort, FrontPanelPortTemplate, Interface, InterfaceConnection,
|
||||
InterfaceTemplate, Manufacturer, InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort,
|
||||
PowerPortTemplate, Rack, RackGroup, RackReservation, RackRole, RearPanelPort, RearPanelPortTemplate, Region, Site,
|
||||
VirtualChassis,
|
||||
Device, DeviceRole, DeviceType, FrontPanelPort, FrontPanelPortTemplate, Interface, InterfaceTemplate, Manufacturer,
|
||||
InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort, PowerPortTemplate, Rack, RackGroup,
|
||||
RackReservation, RackRole, RearPanelPort, RearPanelPortTemplate, Region, Site, VirtualChassis,
|
||||
)
|
||||
|
||||
DEVICE_BY_PK_RE = r'{\d+\}'
|
||||
@@ -2017,173 +2016,6 @@ class InterfaceBulkDisconnectForm(ConfirmationForm):
|
||||
pk = forms.ModelMultipleChoiceField(queryset=Interface.objects.all(), widget=forms.MultipleHiddenInput)
|
||||
|
||||
|
||||
#
|
||||
# Interface connections
|
||||
#
|
||||
|
||||
class InterfaceConnectionForm(BootstrapMixin, ChainedFieldsMixin, forms.ModelForm):
|
||||
interface_a = forms.ChoiceField(
|
||||
choices=[],
|
||||
widget=SelectWithDisabled,
|
||||
label='Interface'
|
||||
)
|
||||
site_b = forms.ModelChoiceField(
|
||||
queryset=Site.objects.all(),
|
||||
label='Site',
|
||||
required=False,
|
||||
widget=forms.Select(
|
||||
attrs={'filter-for': 'rack_b'}
|
||||
)
|
||||
)
|
||||
rack_b = ChainedModelChoiceField(
|
||||
queryset=Rack.objects.all(),
|
||||
chains=(
|
||||
('site', 'site_b'),
|
||||
),
|
||||
label='Rack',
|
||||
required=False,
|
||||
widget=APISelect(
|
||||
api_url='/api/dcim/racks/?site_id={{site_b}}',
|
||||
attrs={'filter-for': 'device_b', 'nullable': 'true'}
|
||||
)
|
||||
)
|
||||
device_b = ChainedModelChoiceField(
|
||||
queryset=Device.objects.all(),
|
||||
chains=(
|
||||
('site', 'site_b'),
|
||||
('rack', 'rack_b'),
|
||||
),
|
||||
label='Device',
|
||||
required=False,
|
||||
widget=APISelect(
|
||||
api_url='/api/dcim/devices/?site_id={{site_b}}&rack_id={{rack_b}}',
|
||||
display_field='display_name',
|
||||
attrs={'filter-for': 'interface_b'}
|
||||
)
|
||||
)
|
||||
livesearch = forms.CharField(
|
||||
required=False,
|
||||
label='Device',
|
||||
widget=Livesearch(
|
||||
query_key='q',
|
||||
query_url='dcim-api:device-list',
|
||||
field_to_update='device_b'
|
||||
)
|
||||
)
|
||||
interface_b = ChainedModelChoiceField(
|
||||
queryset=Interface.objects.connectable().select_related(
|
||||
'circuit_termination', 'connected_as_a', 'connected_as_b'
|
||||
),
|
||||
chains=(
|
||||
('device', 'device_b'),
|
||||
),
|
||||
label='Interface',
|
||||
widget=APISelect(
|
||||
api_url='/api/dcim/interfaces/?device_id={{device_b}}&type=physical',
|
||||
disabled_indicator='is_connected'
|
||||
)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = InterfaceConnection
|
||||
fields = ['interface_a', 'site_b', 'rack_b', 'device_b', 'interface_b', 'livesearch', 'connection_status']
|
||||
|
||||
def __init__(self, device_a, *args, **kwargs):
|
||||
|
||||
super(InterfaceConnectionForm, self).__init__(*args, **kwargs)
|
||||
|
||||
# Initialize interface A choices
|
||||
device_a_interfaces = device_a.vc_interfaces.connectable().order_naturally().select_related(
|
||||
'circuit_termination', 'connected_as_a', 'connected_as_b'
|
||||
)
|
||||
self.fields['interface_a'].choices = [
|
||||
(iface.id, {'label': iface.name, 'disabled': iface.is_connected}) for iface in device_a_interfaces
|
||||
]
|
||||
|
||||
# Mark connected interfaces as disabled
|
||||
if self.data.get('device_b'):
|
||||
self.fields['interface_b'].choices = []
|
||||
for iface in self.fields['interface_b'].queryset:
|
||||
self.fields['interface_b'].choices.append(
|
||||
(iface.id, {'label': iface.name, 'disabled': iface.is_connected})
|
||||
)
|
||||
|
||||
|
||||
class InterfaceConnectionCSVForm(forms.ModelForm):
|
||||
device_a = FlexibleModelChoiceField(
|
||||
queryset=Device.objects.all(),
|
||||
to_field_name='name',
|
||||
help_text='Name or ID of device A',
|
||||
error_messages={'invalid_choice': 'Device A not found.'}
|
||||
)
|
||||
interface_a = forms.CharField(
|
||||
help_text='Name of interface A'
|
||||
)
|
||||
device_b = FlexibleModelChoiceField(
|
||||
queryset=Device.objects.all(),
|
||||
to_field_name='name',
|
||||
help_text='Name or ID of device B',
|
||||
error_messages={'invalid_choice': 'Device B not found.'}
|
||||
)
|
||||
interface_b = forms.CharField(
|
||||
help_text='Name of interface B'
|
||||
)
|
||||
connection_status = CSVChoiceField(
|
||||
choices=CONNECTION_STATUS_CHOICES,
|
||||
help_text='Connection status'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = InterfaceConnection
|
||||
fields = InterfaceConnection.csv_headers
|
||||
|
||||
def clean_interface_a(self):
|
||||
|
||||
interface_name = self.cleaned_data.get('interface_a')
|
||||
if not interface_name:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Retrieve interface by name
|
||||
interface = Interface.objects.get(
|
||||
device=self.cleaned_data['device_a'], name=interface_name
|
||||
)
|
||||
# Check for an existing connection to this interface
|
||||
if InterfaceConnection.objects.filter(Q(interface_a=interface) | Q(interface_b=interface)).count():
|
||||
raise forms.ValidationError("{} {} is already connected".format(
|
||||
self.cleaned_data['device_a'], interface_name
|
||||
))
|
||||
except Interface.DoesNotExist:
|
||||
raise forms.ValidationError("Invalid interface ({} {})".format(
|
||||
self.cleaned_data['device_a'], interface_name
|
||||
))
|
||||
|
||||
return interface
|
||||
|
||||
def clean_interface_b(self):
|
||||
|
||||
interface_name = self.cleaned_data.get('interface_b')
|
||||
if not interface_name:
|
||||
return None
|
||||
|
||||
try:
|
||||
# Retrieve interface by name
|
||||
interface = Interface.objects.get(
|
||||
device=self.cleaned_data['device_b'], name=interface_name
|
||||
)
|
||||
# Check for an existing connection to this interface
|
||||
if InterfaceConnection.objects.filter(Q(interface_a=interface) | Q(interface_b=interface)).count():
|
||||
raise forms.ValidationError("{} {} is already connected".format(
|
||||
self.cleaned_data['device_b'], interface_name
|
||||
))
|
||||
except Interface.DoesNotExist:
|
||||
raise forms.ValidationError("Invalid interface ({} {})".format(
|
||||
self.cleaned_data['device_b'], interface_name
|
||||
))
|
||||
|
||||
return interface
|
||||
|
||||
|
||||
#
|
||||
# Front panel ports
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user