Преглед изворни кода

Fixes #23493: Resolve {vc_position} when linking inventory item components (#23745)

InventoryItemTemplate.instantiate() looked up the referenced component by
its template's raw name, but the component itself was created with
resolve_name(), so a template named with {vc_position} never matched and
device creation failed. Resolve the name the same way the sibling
templates do. Same mismatch #22710 fixed for port mappings and interface
bridges.
Jason Novinger пре 1 дан
родитељ
комит
8d2e3f9b84
2 измењених фајлова са 27 додато и 1 уклоњено
  1. 2 1
      netbox/dcim/models/device_component_templates.py
  2. 25 0
      netbox/dcim/tests/test_models.py

+ 2 - 1
netbox/dcim/models/device_component_templates.py

@@ -1096,7 +1096,8 @@ class InventoryItemTemplate(LtreeModel, ComponentTemplateModel):
         parent = InventoryItem.objects.get(name=self.parent.name, **kwargs) if self.parent else None
         parent = InventoryItem.objects.get(name=self.parent.name, **kwargs) if self.parent else None
         if self.component:
         if self.component:
             model = self.component.component_model
             model = self.component.component_model
-            component = model.objects.get(name=self.component.name, **kwargs)
+            component_name = self.component.resolve_name(kwargs.get('module'), kwargs.get('device'))
+            component = model.objects.get(name=component_name, **kwargs)
         else:
         else:
             component = None
             component = None
         return self.component_model(
         return self.component_model(

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

@@ -4208,6 +4208,31 @@ class VCPositionTokenTestCase(TestCase):
         self.assertEqual(mapping.front_port_position, 1)
         self.assertEqual(mapping.front_port_position, 1)
         self.assertEqual(mapping.rear_port_position, 1)
         self.assertEqual(mapping.rear_port_position, 1)
 
 
+    @tag('regression')  # Ref: #23493
+    def test_vc_position_token_inventory_item_component_device_type_template(self):
+        site = Site.objects.first()
+        device_type = DeviceType.objects.first()
+        device_role = DeviceRole.objects.first()
+
+        interface_template = InterfaceTemplate.objects.create(
+            device_type=device_type,
+            name='eth{vc_position}',
+            type='1000base-t',
+        )
+        InventoryItemTemplate.objects.create(
+            device_type=device_type,
+            name='NIC',
+            component=interface_template,
+        )
+        vc = VirtualChassis.objects.create(name='Test VC 7')
+        device = Device.objects.create(
+            name='Device VC 7', device_type=device_type, role=device_role,
+            site=site, virtual_chassis=vc, vc_position=7,
+        )
+
+        inventory_item = device.inventoryitems.get(name='NIC')
+        self.assertEqual(inventory_item.component, device.interfaces.get(name='eth7'))
+
 
 
 class SiteSignalTestCase(TestCase):
 class SiteSignalTestCase(TestCase):