Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #8272 - Add bridge to InterfaceTemplate #11869

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ class InterfaceTemplateSerializer(ValidatedModelSerializer):
default=None
)
type = ChoiceField(choices=InterfaceTypeChoices)
bridge = NestedInterfaceTemplateSerializer(required=False, allow_null=True)
poe_mode = ChoiceField(
choices=InterfacePoEModeChoices,
required=False,
Expand All @@ -489,7 +490,7 @@ class InterfaceTemplateSerializer(ValidatedModelSerializer):
class Meta:
model = InterfaceTemplate
fields = [
'id', 'url', 'display', 'device_type', 'module_type', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description',
'id', 'url', 'display', 'device_type', 'module_type', 'name', 'label', 'type', 'bridge', 'enabled', 'mgmt_only', 'description',
'poe_mode', 'poe_type', 'created', 'last_updated',
]

Expand Down
13 changes: 11 additions & 2 deletions netbox/dcim/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,15 +1048,24 @@ class Meta:


class InterfaceTemplateForm(ModularComponentTemplateForm):
bridge = DynamicModelChoiceField(
queryset=InterfaceTemplate.objects.all(),
required=False,
query_params={
'devicetype_id': '$device_type',
'moduletype_id': '$module_type',
}
)

fieldsets = (
(None, ('device_type', 'module_type', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description')),
(None, ('device_type', 'module_type', 'name', 'label', 'type', 'enabled', 'mgmt_only', 'description', 'bridge')),
('PoE', ('poe_mode', 'poe_type'))
)

class Meta:
model = InterfaceTemplate
fields = [
'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'enabled', 'description', 'poe_mode', 'poe_type',
'device_type', 'module_type', 'name', 'label', 'type', 'mgmt_only', 'enabled', 'description', 'poe_mode', 'poe_type', 'bridge',
]


Expand Down
19 changes: 19 additions & 0 deletions netbox/dcim/migrations/0171_devicetype_add_bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.6 on 2023-03-01 13:42

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('dcim', '0170_configtemplate'),
]

operations = [
migrations.AddField(
model_name='interfacetemplate',
name='bridge',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bridge_interfaces', to='dcim.interfacetemplate'),
),
]
22 changes: 22 additions & 0 deletions netbox/dcim/models/device_component_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ class InterfaceTemplate(ModularComponentTemplateModel):
default=False,
verbose_name='Management only'
)
bridge = models.ForeignKey(
to='self',
on_delete=models.SET_NULL,
related_name='bridge_interfaces',
null=True,
blank=True,
verbose_name='Bridge interface'
)
poe_mode = models.CharField(
max_length=50,
choices=InterfacePoEModeChoices,
Expand All @@ -365,6 +373,19 @@ class InterfaceTemplate(ModularComponentTemplateModel):

component_model = Interface

def clean(self):
super().clean()

if self.bridge:
if self.device_type and self.device_type != self.bridge.device_type:
raise ValidationError({
'bridge': f"Bridge interface ({self.bridge}) must belong to the same device type"
})
if self.module_type and self.module_type != self.bridge.module_type:
raise ValidationError({
'bridge': f"Bridge interface ({self.bridge}) must belong to the same module type"
})

def instantiate(self, **kwargs):
return self.component_model(
name=self.resolve_name(kwargs.get('module')),
Expand All @@ -385,6 +406,7 @@ def to_yaml(self):
'mgmt_only': self.mgmt_only,
'label': self.label,
'description': self.description,
'bridge': self.bridge.name if self.bridge else None,
'poe_mode': self.poe_mode,
'poe_type': self.poe_type,
}
Expand Down
23 changes: 23 additions & 0 deletions netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ def clean(self):
'vc_position': "A device assigned to a virtual chassis must have its position defined."
})

def _update_interface_bridges(self, interface_templates, module=None):
for interface_template in interface_templates.exclude(bridge=None):
interface = Interface.objects.get(device=self, name=interface_template.resolve_name(module=module))

if interface_template.bridge:
interface.bridge = Interface.objects.get(device=self, name=interface_template.bridge.resolve_name(module=module))
interface.full_clean()
interface.save()

def _instantiate_components(self, queryset, bulk_create=True):
"""
Instantiate components for the device from the specified component templates.
Expand Down Expand Up @@ -850,6 +859,8 @@ def save(self, *args, **kwargs):
self._instantiate_components(self.device_type.devicebaytemplates.all())
# Disable bulk_create to accommodate MPTT
self._instantiate_components(self.device_type.inventoryitemtemplates.all(), bulk_create=False)
# Interface bridges have to be set after interface instantiation
self._update_interface_bridges(self.device_type.interfacetemplates.all())

# Update Site and Rack assignment for any child Devices
devices = Device.objects.filter(parent_bay__device=self)
Expand Down Expand Up @@ -1011,6 +1022,15 @@ def clean(self):
f"Module must be installed within a module bay belonging to the assigned device ({self.device})."
)

def _update_interface_bridges(self, interface_templates, module=None):
jeremystretch marked this conversation as resolved.
Show resolved Hide resolved
for interface_template in interface_templates.exclude(bridge=None):
interface = Interface.objects.get(device=self.device, name=interface_template.resolve_name(module=module))

if interface_template.bridge:
interface.bridge = Interface.objects.get(device=self.device, name=interface_template.bridge.resolve_name(module=module))
interface.full_clean()
interface.save()

def save(self, *args, **kwargs):
is_new = self.pk is None

Expand Down Expand Up @@ -1086,6 +1106,9 @@ def save(self, *args, **kwargs):
update_fields=update_fields
)

# Interface bridges have to be set after interface instantiation
self._update_interface_bridges(self.module_type.interfacetemplates, self)


#
# Virtual chassis
Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/tables/devicetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class InterfaceTemplateTable(ComponentTemplateTable):

class Meta(ComponentTemplateTable.Meta):
model = models.InterfaceTemplate
fields = ('pk', 'name', 'label', 'enabled', 'mgmt_only', 'type', 'description', 'poe_mode', 'poe_type', 'actions')
fields = ('pk', 'name', 'label', 'enabled', 'mgmt_only', 'type', 'description', 'bridge', 'poe_mode', 'poe_type', 'actions')
empty_text = "None"


Expand Down