Răsfoiți Sursa

Closes #23747: Resolve VC position tokens in parent interface templates

Pass the device context when resolving interface and parent template
names, consistent with interface bridge resolution.

Add a regression test to verify device creation and parent assignment.
Martin Hauser 1 zi în urmă
părinte
comite
510bf60c35
2 a modificat fișierele cu 31 adăugiri și 2 ștergeri
  1. 26 0
      netbox/dcim/tests/test_models.py
  2. 5 2
      netbox/dcim/utils.py

+ 26 - 0
netbox/dcim/tests/test_models.py

@@ -4247,6 +4247,32 @@ class VCPositionTokenTestCase(TestCase):
         inventory_item = device.inventoryitems.get(name='NIC')
         inventory_item = device.inventoryitems.get(name='NIC')
         self.assertEqual(inventory_item.component, device.interfaces.get(name='eth7'))
         self.assertEqual(inventory_item.component, device.interfaces.get(name='eth7'))
 
 
+    @tag('regression')  # Ref: #23747
+    def test_vc_position_token_interface_parent_device_type_template(self):
+        site = Site.objects.first()
+        device_type = DeviceType.objects.first()
+        device_role = DeviceRole.objects.first()
+
+        parent_template = InterfaceTemplate.objects.create(
+            device_type=device_type,
+            name='eth{vc_position}',
+            type='1000base-t',
+        )
+        InterfaceTemplate.objects.create(
+            device_type=device_type,
+            name='eth{vc_position}.100',
+            type='virtual',
+            parent=parent_template,
+        )
+        vc = VirtualChassis.objects.create(name='Test VC 8')
+        device = Device.objects.create(
+            name='Device VC 8', device_type=device_type, role=device_role,
+            site=site, virtual_chassis=vc, vc_position=8,
+        )
+
+        interface = device.interfaces.get(name='eth8.100')
+        self.assertEqual(interface.parent, device.interfaces.get(name='eth8'))
+
 
 
 class SiteSignalTestCase(TestCase):
 class SiteSignalTestCase(TestCase):
 
 

+ 5 - 2
netbox/dcim/utils.py

@@ -482,11 +482,14 @@ def update_interface_parents(device, interface_templates, module=None):
     Interface = apps.get_model('dcim', 'Interface')
     Interface = apps.get_model('dcim', 'Interface')
 
 
     for interface_template in interface_templates.exclude(parent=None):
     for interface_template in interface_templates.exclude(parent=None):
-        interface = Interface.objects.get(device=device, name=interface_template.resolve_name(module=module))
+        interface = Interface.objects.get(
+            device=device,
+            name=interface_template.resolve_name(module=module, device=device)
+        )
         interface.snapshot()
         interface.snapshot()
         interface.parent = Interface.objects.get(
         interface.parent = Interface.objects.get(
             device=device,
             device=device,
-            name=interface_template.parent.resolve_name(module=module)
+            name=interface_template.parent.resolve_name(module=module, device=device)
         )
         )
         interface.full_clean()
         interface.full_clean()
         interface.save()
         interface.save()