From 0bc05f27f90737514ede57c08f80bc0480665be3 Mon Sep 17 00:00:00 2001 From: Ibtissam El alami <140215889+attoba@users.noreply.github.com> Date: Thu, 9 Apr 2026 15:41:14 +0100 Subject: [PATCH] Fixes #21704: Add port mappings to DeviceType & ModuleType YAML export (#21859) --- netbox/dcim/models/device_component_templates.py | 8 ++++++++ netbox/dcim/models/devices.py | 9 +++++++++ netbox/dcim/models/modules.py | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/netbox/dcim/models/device_component_templates.py b/netbox/dcim/models/device_component_templates.py index ff5df9721..1799e1265 100644 --- a/netbox/dcim/models/device_component_templates.py +++ b/netbox/dcim/models/device_component_templates.py @@ -549,6 +549,14 @@ class PortTemplateMapping(PortMappingBase): self.module_type = self.front_port.module_type super().save(*args, **kwargs) + def to_yaml(self): + return { + 'front_port': self.front_port.name, + 'front_port_position': self.front_port_position, + 'rear_port': self.rear_port.name, + 'rear_port_position': self.rear_port_position, + } + class FrontPortTemplate(ModularComponentTemplateModel): """ diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 4a5d0b3cd..aa4a794e2 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -275,6 +275,15 @@ class DeviceType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): data['rear-ports'] = [ c.to_yaml() for c in self.rearporttemplates.all() ] + + # Port mappings + port_mapping_data = [ + c.to_yaml() for c in self.port_mappings.all() + ] + + if port_mapping_data: + data['port-mappings'] = port_mapping_data + if self.modulebaytemplates.exists(): data['module-bays'] = [ c.to_yaml() for c in self.modulebaytemplates.all() diff --git a/netbox/dcim/models/modules.py b/netbox/dcim/models/modules.py index 89e914366..56cc333ff 100644 --- a/netbox/dcim/models/modules.py +++ b/netbox/dcim/models/modules.py @@ -192,6 +192,14 @@ class ModuleType(ImageAttachmentsMixin, PrimaryModel, WeightMixin): c.to_yaml() for c in self.rearporttemplates.all() ] + # Port mappings + port_mapping_data = [ + c.to_yaml() for c in self.port_mappings.all() + ] + + if port_mapping_data: + data['port-mappings'] = port_mapping_data + return yaml.dump(dict(data), sort_keys=False)