diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 67550e136..84d4dc39c 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -1117,6 +1117,15 @@ class ConsoleServerPort(models.Model): def __str__(self): return self.name + def clean(self): + + # Check that the parent device's DeviceType is a console server + device_type = self.device.device_type + if not device_type.is_console_server: + raise ValidationError("The {} {} device type not support assignment of console server ports.".format( + device_type.manufacturer, device_type + )) + # # Power ports @@ -1182,6 +1191,15 @@ class PowerOutlet(models.Model): def __str__(self): return self.name + def clean(self): + + # Check that the parent device's DeviceType is a PDU + device_type = self.device.device_type + if not device_type.is_pdu: + raise ValidationError("The {} {} device type not support assignment of power outlets.".format( + device_type.manufacturer, device_type + )) + # # Interfaces @@ -1238,6 +1256,13 @@ class Interface(models.Model): def clean(self): + # Check that the parent device's DeviceType is a network device + device_type = self.device.device_type + if not device_type.is_network_device: + raise ValidationError("The {} {} device type not support assignment of network interfaces.".format( + device_type.manufacturer, device_type + )) + # An Interface must belong to a Device *or* to a VirtualMachine if self.device and self.virtual_machine: raise ValidationError("An interface cannot belong to both a device and a virtual machine.") diff --git a/netbox/dcim/tests/test_api.py b/netbox/dcim/tests/test_api.py index bec333974..5d6f66d30 100644 --- a/netbox/dcim/tests/test_api.py +++ b/netbox/dcim/tests/test_api.py @@ -1432,7 +1432,7 @@ class ConsoleServerPortTest(HttpStatusMixin, APITestCase): site = Site.objects.create(name='Test Site 1', slug='test-site-1') manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1') devicetype = DeviceType.objects.create( - manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1' + manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1', is_console_server=True ) devicerole = DeviceRole.objects.create( name='Test Device Role 1', slug='test-device-role-1', color='ff0000' @@ -1590,7 +1590,7 @@ class PowerOutletTest(HttpStatusMixin, APITestCase): site = Site.objects.create(name='Test Site 1', slug='test-site-1') manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1') devicetype = DeviceType.objects.create( - manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1' + manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1', is_pdu=True ) devicerole = DeviceRole.objects.create( name='Test Device Role 1', slug='test-device-role-1', color='ff0000' @@ -1667,7 +1667,7 @@ class InterfaceTest(HttpStatusMixin, APITestCase): site = Site.objects.create(name='Test Site 1', slug='test-site-1') manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1') devicetype = DeviceType.objects.create( - manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1' + manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1', is_network_device=True ) devicerole = DeviceRole.objects.create( name='Test Device Role 1', slug='test-device-role-1', color='ff0000'