Browse Source

Fixes #1642: Validate device type classification when creating console server ports and power outlets

Jeremy Stretch 7 years ago
parent
commit
30b544a743
2 changed files with 28 additions and 3 deletions
  1. 25 0
      netbox/dcim/models.py
  2. 3 3
      netbox/dcim/tests/test_api.py

+ 25 - 0
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.")

+ 3 - 3
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'