Переглянути джерело

Closes #23637: Support module bay types in module type imports

Aaron Axvig 16 годин тому
батько
коміт
18fd28657a

+ 1 - 1
docs/models/dcim/moduletype.md

@@ -91,7 +91,7 @@ The assigned [profile](./moduletypeprofile.md) for the type of module. Profiles
 
 Zero or more [module bay types](./modulebaytype.md) that this module type is compatible with. When at least one bay type is set, the module type may only be installed into bays that share a common type. Leave empty to allow installation into any bay.
 
-Bay types are included, by name, in a module type's exported YAML definition, but are not currently importable back through it; re-importing an exported definition leaves this field unset.
+Bay types are exported and imported by name. On import, a bay type belonging to the module type's manufacturer takes precedence over a global bay type with the same name; a name matching only another manufacturer's bay type is rejected.
 
 ### Attributes
 

+ 41 - 1
netbox/dcim/forms/bulk_import.py

@@ -510,6 +510,12 @@ class ModuleTypeProfileImportForm(PrimaryModelImportForm):
 
 
 class ModuleTypeImportForm(PrimaryModelImportForm):
+    module_bay_types = CSVModelMultipleChoiceField(
+        label=_('Module bay types'),
+        queryset=ModuleBayType.objects.all(),
+        to_field_name='name',
+        required=False,
+    )
     profile = forms.ModelChoiceField(
         label=_('Profile'),
         queryset=ModuleTypeProfile.objects.all(),
@@ -554,7 +560,7 @@ class ModuleTypeImportForm(PrimaryModelImportForm):
         model = ModuleType
         fields = [
             'manufacturer', 'model', 'part_number', 'description', 'cooling_method', 'airflow', 'weight', 'weight_unit',
-            'end_of_life', 'profile', 'attribute_data', 'owner', 'comments', 'tags',
+            'end_of_life', 'profile', 'attribute_data', 'module_bay_types', 'owner', 'comments', 'tags',
         ]
 
     def clean(self):
@@ -568,6 +574,40 @@ class ModuleTypeImportForm(PrimaryModelImportForm):
         if self.cleaned_data.get('profile') and not self.cleaned_data.get('attribute_data'):
             self.cleaned_data['attribute_data'] = {}
 
+        module_bay_types = self.cleaned_data.get('module_bay_types')
+        manufacturer = self.cleaned_data.get('manufacturer')
+
+        # If importing an update to an existing module type and the manufacturer is not in
+        # the import data, use the existing manufacturer.
+        if manufacturer is None and self.instance.pk:
+            manufacturer = self.instance.manufacturer
+        if not module_bay_types or manufacturer is None:
+            return
+
+        # Match the module's manufacturer or a global bay type, preferring the former
+        # when a name is shared, as with module bay template imports.
+        by_name = {}
+        for module_bay_type in module_bay_types:
+            if module_bay_type.manufacturer_id not in (None, manufacturer.pk):
+                continue
+            existing = by_name.get(module_bay_type.name)
+            if existing is None or module_bay_type.manufacturer_id is not None:
+                by_name[module_bay_type.name] = module_bay_type
+
+        # A name matching only some other manufacturer's bay type is rejected rather than
+        # resolved to it.
+        for module_bay_type in module_bay_types:
+            if module_bay_type.name not in by_name:
+                raise forms.ValidationError({
+                    'module_bay_types': forms.ValidationError(
+                        self.fields['module_bay_types'].error_messages['invalid_choice'],
+                        code='invalid_choice',
+                        params={'value': module_bay_type.name},
+                    )
+                })
+
+        self.cleaned_data['module_bay_types'] = list(by_name.values())
+
 
 class DeviceRoleImportForm(NestedGroupModelImportForm):
     parent = CSVModelChoiceField(

+ 75 - 0
netbox/dcim/tests/test_forms.py

@@ -1,5 +1,6 @@
 from unittest.mock import patch
 
+import yaml
 from django import forms
 from django.template.loader import render_to_string
 from django.test import TestCase
@@ -311,6 +312,80 @@ class ModuleTypeFormTestCase(TestCase):
                     self.assertEqual(module_type.attribute_data, {'slots': value})
 
 
+class ModuleTypeImportFormTestCase(TestCase):
+
+    @classmethod
+    def setUpTestData(cls):
+        cls.manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
+        other_manufacturer = Manufacturer.objects.create(name='Manufacturer 2', slug='manufacturer-2')
+        cls.global_type = ModuleBayType.objects.create(name='global-slot', slug='global-slot')
+        cls.manufacturer_type = ModuleBayType.objects.create(
+            manufacturer=cls.manufacturer, name='manufacturer-slot', slug='manufacturer-slot',
+        )
+        ModuleBayType.objects.create(name='manufacturer-slot', slug='manufacturer-slot')
+        cls.other_type = ModuleBayType.objects.create(
+            manufacturer=other_manufacturer, name='Other Bay', slug='other-bay',
+        )
+
+    def test_import_module_bay_types(self):
+        """Save global and manufacturer-specific bay types, preferring the latter when a name is shared."""
+        form = ModuleTypeImportForm(data={
+            'manufacturer': self.manufacturer.name,
+            'model': 'Imported Module',
+            'module_bay_types': ['global-slot', 'manufacturer-slot'],
+        })
+        self.assertTrue(form.is_valid(), form.errors)
+        module_type = form.save()
+        self.assertEqual(set(module_type.module_bay_types.all()), {self.global_type, self.manufacturer_type})
+
+    def test_invalid_module_bay_types(self):
+        """Reject names that are unknown or belong only to another manufacturer."""
+        for name in ('Unknown Bay', self.other_type.name):
+            with self.subTest(name=name):
+                form = ModuleTypeImportForm(data={
+                    'manufacturer': self.manufacturer.name,
+                    'model': 'Imported Module',
+                    'module_bay_types': [self.global_type.name, name],
+                })
+                self.assertFalse(form.is_valid())
+                self.assertEqual(form.errors.as_data()['module_bay_types'][0].code, 'invalid_choice')
+
+    def test_optional_module_bay_types(self):
+        """An omitted or empty bay type list leaves a new module type unconstrained."""
+        for extra_data in ({}, {'module_bay_types': []}):
+            with self.subTest(data=extra_data):
+                form = ModuleTypeImportForm(data={
+                    'manufacturer': self.manufacturer.name,
+                    'model': 'Imported Module',
+                    **extra_data,
+                })
+                self.assertTrue(form.is_valid(), form.errors)
+                module_type = form.save()
+                self.assertFalse(module_type.module_bay_types.exists())
+                module_type.delete()
+
+    def test_update_module_bay_types_without_manufacturer(self):
+        """Resolve bay types against the saved manufacturer when an update omits it."""
+        module_type = ModuleType.objects.create(manufacturer=self.manufacturer, model='Existing Module')
+        form = ModuleTypeImportForm(instance=module_type, data={'module_bay_types': ['manufacturer-slot']})
+        # BulkImportView removes unspecified fields when updating an existing object.
+        for name in list(form.fields):
+            if name not in form.data:
+                del form.fields[name]
+        self.assertTrue(form.is_valid(), form.errors)
+        self.assertEqual(list(form.save().module_bay_types.all()), [self.manufacturer_type])
+
+    def test_import_export_round_trip(self):
+        """Exporting and reimporting a module type preserves its bay type assignments."""
+        original = ModuleType.objects.create(manufacturer=self.manufacturer, model='Original Module')
+        original.module_bay_types.set([self.global_type, self.manufacturer_type])
+        data = yaml.safe_load(original.to_yaml())
+        data['model'] = 'Reimported Module'
+        form = ModuleTypeImportForm(data=data)
+        self.assertTrue(form.is_valid(), form.errors)
+        self.assertEqual(set(form.save().module_bay_types.all()), set(original.module_bay_types.all()))
+
+
 class ModuleTypeProfileDescriptionRenderingTestCase(TestCase):
     """
     A profile schema property's description is rendered as the attribute field's help text via the

+ 77 - 2
netbox/dcim/tests/test_views.py

@@ -9,7 +9,7 @@ from zoneinfo import ZoneInfo
 
 import yaml
 from django.contrib.contenttypes.models import ContentType
-from django.contrib.messages import get_messages
+from django.contrib.messages import WARNING, get_messages
 from django.db import connection
 from django.http import QueryDict, StreamingHttpResponse
 from django.test import override_settings, tag
@@ -23,7 +23,7 @@ from core.models import ObjectChange, ObjectType
 from dcim.choices import *
 from dcim.constants import *
 from dcim.models import *
-from dcim.views import DeviceTypeListView, ModuleTypeListView
+from dcim.views import DeviceTypeListView, ModuleTypeImportView, ModuleTypeListView
 from extras.models import ConfigContext, ConfigTemplate
 from ipam.models import ASN, RIR, VLAN, VRF, IPAddress, VLANGroup
 from netbox.choices import (
@@ -1782,6 +1782,81 @@ class ModuleTypeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
         url = reverse('dcim:moduletype_frontports', kwargs={'pk': moduletype.pk})
         self.assertHttpStatus(self.client.get(url), 200)
 
+    def test_import_module_bay_types(self):
+        """YAML and JSON imports save global bay types and prefer manufacturer-specific matches for shared names."""
+        manufacturer = Manufacturer.objects.first()
+        global_type = ModuleBayType.objects.create(name='global-slot', slug='global-slot')
+        manufacturer_type = ModuleBayType.objects.create(
+            manufacturer=manufacturer, name='manufacturer-slot', slug='manufacturer-slot',
+        )
+        ModuleBayType.objects.create(name='manufacturer-slot', slug='manufacturer-slot')
+        self.add_permissions(
+            'dcim.view_manufacturer',
+            'dcim.view_modulebaytype',
+            'dcim.view_moduletype',
+            'dcim.add_moduletype',
+            'dcim.add_consoleporttemplate',
+            'dcim.add_consoleserverporttemplate',
+            'dcim.add_powerporttemplate',
+            'dcim.add_poweroutlettemplate',
+            'dcim.add_coolingintaketemplate',
+            'dcim.add_coolingoutflowtemplate',
+            'dcim.add_interfacetemplate',
+            'dcim.add_frontporttemplate',
+            'dcim.add_rearporttemplate',
+            'dcim.add_modulebaytemplate',
+        )
+
+        data = {
+            'manufacturer': manufacturer.name,
+            'model': 'Imported Transceiver',
+            'module_bay_types': ['global-slot', 'manufacturer-slot'],
+        }
+        for format_, serialized in (('yaml', yaml.safe_dump(data)), ('json', json.dumps(data))):
+            with self.subTest(format=format_):
+                response = self.client.post(reverse('dcim:moduletype_bulk_import'), data={
+                    'data': serialized,
+                    'format': format_,
+                })
+                self.assertHttpStatus(response, 302)
+                module_type = ModuleType.objects.get(model=data['model'])
+                self.assertEqual(set(module_type.module_bay_types.all()), {global_type, manufacturer_type})
+                module_type.delete()
+
+    def test_import_update_warns_about_incompatible_installed_modules(self):
+        """An import can restrict an existing module type and gives a warning (doesn't remove installed modules)."""
+        self.add_permissions(
+            *ModuleTypeImportView.additional_permissions,
+            'dcim.change_moduletype',
+            'dcim.view_modulebaytype',
+        )
+        module_type = ModuleType.objects.get(model='Module Type 2')
+        device = create_test_device('Device 1')
+        allowed_type = ModuleBayType.objects.create(name='Allowed Slot', slug='allowed-slot')
+        other_type = ModuleBayType.objects.create(name='Other Slot', slug='other-slot')
+        compatible_bay = ModuleBay.objects.create(device=device, name='Compatible Bay')
+        compatible_bay.module_bay_types.add(allowed_type)
+        incompatible_bay = ModuleBay.objects.create(device=device, name='Incompatible Bay')
+        incompatible_bay.module_bay_types.add(other_type)
+
+        # Install modules with no bay type constraints in bays with differing bay type constraints.
+        Module.objects.create(device=device, module_bay=compatible_bay, module_type=module_type)
+        installed_module = Module.objects.create(device=device, module_bay=incompatible_bay, module_type=module_type)
+
+        response = self.client.post(reverse('dcim:moduletype_bulk_import'), data={
+            'data': yaml.safe_dump({'id': module_type.pk, 'module_bay_types': [allowed_type.name]}),
+            'format': 'yaml',
+        })
+
+        self.assertHttpStatus(response, 302)
+        self.assertEqual(list(module_type.module_bay_types.all()), [allowed_type])
+        installed_module.refresh_from_db()
+        self.assertEqual(installed_module.module_bay_id, incompatible_bay.pk)
+        self.assertEqual(
+            [str(message) for message in get_messages(response.wsgi_request) if message.level == WARNING],
+            ['1 installed module is incompatible with its module bay due to conflicting bay type constraints.'],
+        )
+
     def test_import_objects(self):
         """
         Custom import test for YAML-based imports (versus CSV)

+ 25 - 0
netbox/dcim/views.py

@@ -37,6 +37,7 @@ from netbox.ui.panels import (
 from netbox.views import generic
 from utilities.exceptions import PermissionsViolation
 from utilities.forms import ConfirmationForm, restrict_form_fields
+from utilities.jobs import is_background_request
 from utilities.paginator import EnhancedPaginator, get_paginate_count
 from utilities.permissions import get_permission_for_model
 from utilities.query import count_related
@@ -2181,6 +2182,30 @@ class ModuleTypeImportView(generic.BulkImportView):
         data.update({'module_type': parent})
         return data
 
+    def save_object(self, object_form, request):
+        check_compatibility = object_form.instance.pk and 'module_bay_types' in object_form.cleaned_data
+        module_type = super().save_object(object_form, request)
+        if check_compatibility:
+            self._incompatible_count += module_type.get_incompatible_modules().count()
+        return module_type
+
+    def post(self, request):
+        self._incompatible_count = 0
+        response = super().post(request)
+        if self._incompatible_count and (response is None or response.status_code == 302):
+            warning = ngettext(
+                '%(count)d installed module is incompatible with its module bay '
+                'due to conflicting bay type constraints.',
+                '%(count)d installed modules are incompatible with their module bays '
+                'due to conflicting bay type constraints.',
+                self._incompatible_count,
+            ) % {'count': self._incompatible_count}
+            if is_background_request(request):
+                request.job.logger.warning(warning)
+            else:
+                messages.warning(request, warning)
+        return response
+
 
 @register_model_view(ModuleType, 'bulk_edit', path='edit', detail=False)
 class ModuleTypeBulkEditView(generic.BulkEditView):