فهرست منبع

closes #4340 - Enforce unique constraints for device and virtual machine names in the API

John Anderson 6 سال پیش
والد
کامیت
a504f5f309
4فایلهای تغییر یافته به همراه33 افزوده شده و 0 حذف شده
  1. 6 0
      docs/release-notes/version-2.7.md
  2. 14 0
      netbox/dcim/tests/test_api.py
  3. 1 0
      netbox/utilities/api.py
  4. 12 0
      netbox/virtualization/tests/test_api.py

+ 6 - 0
docs/release-notes/version-2.7.md

@@ -1,5 +1,11 @@
 # NetBox v2.7 Release Notes
 
+## v2.7.11 (FUTURE)
+
+### Bug Fixes
+
+* [#4340](https://github.com/netbox-community/netbox/issues/4340) - Enforce unique constraints for device and virtual machine names in the API
+
 ## v2.7.10 (2020-03-10)
 
 **Note:** If your deployment requires any non-core Python packages (such as `napalm`, `django-storages`, or `django-auth-ldap`), list them in a file named `local_requirements.txt` in the NetBox root directory (alongside `requirements.txt`). This will ensure they are detected and re-installed by the upgrade script when the Python virtual environment is rebuilt.

+ 14 - 0
netbox/dcim/tests/test_api.py

@@ -2089,6 +2089,20 @@ class DeviceTest(APITestCase):
 
         self.assertFalse('config_context' in response.data['results'][0])
 
+    def test_unique_name_per_site_constraint(self):
+
+        data = {
+            'device_type': self.devicetype1.pk,
+            'device_role': self.devicerole1.pk,
+            'name': 'Test Device 1',
+            'site': self.site1.pk,
+        }
+
+        url = reverse('dcim-api:device-list')
+        response = self.client.post(url, data, format='json', **self.header)
+
+        self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
+
 
 class ConsolePortTest(APITestCase):
 

+ 1 - 0
netbox/utilities/api.py

@@ -234,6 +234,7 @@ class ValidatedModelSerializer(ModelSerializer):
             for k, v in attrs.items():
                 setattr(instance, k, v)
         instance.clean()
+        instance.validate_unique()
 
         return data
 

+ 12 - 0
netbox/virtualization/tests/test_api.py

@@ -501,6 +501,18 @@ class VirtualMachineTest(APITestCase):
 
         self.assertFalse('config_context' in response.data['results'][0])
 
+    def test_unique_name_per_cluster_constraint(self):
+
+        data = {
+            'name': 'Test Virtual Machine 1',
+            'cluster': self.cluster1.pk,
+        }
+
+        url = reverse('virtualization-api:virtualmachine-list')
+        response = self.client.post(url, data, format='json', **self.header)
+
+        self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST)
+
 
 class InterfaceTest(APITestCase):