Quellcode durchsuchen

Fixes #23748: Validate inventory item template component

An inventory item template could reference a component template on
another device type or a module type, which made creating any device
of its type fail. Validation now rejects such a component.
Jason Novinger vor 13 Stunden
Ursprung
Commit
334001f07f

+ 9 - 0
netbox/dcim/models/device_component_templates.py

@@ -1092,6 +1092,15 @@ class InventoryItemTemplate(LtreeModel, ComponentTemplateModel):
         verbose_name = _('inventory item template')
         verbose_name_plural = _('inventory item templates')
 
+    def clean(self):
+        super().clean()
+
+        # A component template on a module type has no device type, so this check also rejects it
+        if self.device_type_id and self.component and self.component.device_type_id != self.device_type_id:
+            raise ValidationError({
+                'device_type': _("Cannot assign inventory item template to component template on another device type.")
+            })
+
     def instantiate(self, parent=None, **kwargs):
         """
         Instantiate a new InventoryItem. `parent` is the InventoryItem instantiated from this template's parent.

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

@@ -4773,6 +4773,43 @@ class InventoryItemTemplateTestCase(TestCase):
         self.assertEqual(paginated, expected)
         self.assertTrue(queryset.totally_ordered)
 
+    @tag('regression')  # Ref: #23748
+    def test_component_must_belong_to_device_type(self):
+        """
+        A component template on another device type would make creating any device of this type fail.
+        """
+        manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
+        device_type = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1', slug='device-type-1')
+        other_device_type = DeviceType.objects.create(
+            manufacturer=manufacturer, model='Device Type 2', slug='device-type-2'
+        )
+        module_type = ModuleType.objects.create(manufacturer=manufacturer, model='Module Type 1')
+
+        own_interface = InterfaceTemplate.objects.create(
+            device_type=device_type, name='eth0', type=InterfaceTypeChoices.TYPE_1GE_FIXED
+        )
+        InventoryItemTemplate(device_type=device_type, name='Optic', component=own_interface).full_clean()
+
+        foreign_interfaces = (
+            InterfaceTemplate.objects.create(
+                device_type=other_device_type, name='eth0', type=InterfaceTypeChoices.TYPE_1GE_FIXED
+            ),
+            InterfaceTemplate.objects.create(
+                module_type=module_type, name='eth{module}', type=InterfaceTypeChoices.TYPE_1GE_FIXED
+            ),
+        )
+        for interface in foreign_interfaces:
+            with self.subTest(interface=interface):
+                template = InventoryItemTemplate(device_type=device_type, name='Optic', component=interface)
+                with self.assertRaises(ValidationError) as cm:
+                    template.full_clean()
+                self.assertIn('device_type', cm.exception.message_dict)
+
+        template = InventoryItemTemplate(name='Optic', component=foreign_interfaces[0])
+        with self.assertRaises(ValidationError) as cm:
+            template.full_clean()
+        self.assertEqual(len(cm.exception.message_dict['device_type']), 1)
+
 
 class InventoryItemTemplateCycleTestCase(TestCase):
     """InventoryItemTemplate must likewise reject self/descendant as parent."""