Просмотр исходного кода

Re-implemented connected device API view & test

Jeremy Stretch 7 лет назад
Родитель
Сommit
bb114ea66b
2 измененных файлов с 60 добавлено и 51 удалено
  1. 20 13
      netbox/dcim/api/views.py
  2. 40 38
      netbox/dcim/tests/test_api.py

+ 20 - 13
netbox/dcim/api/views.py

@@ -1,7 +1,7 @@
 from collections import OrderedDict
 from collections import OrderedDict
 
 
 from django.conf import settings
 from django.conf import settings
-from django.db.models import Count, F, Q
+from django.db.models import F, Q
 from django.http import HttpResponseForbidden
 from django.http import HttpResponseForbidden
 from django.shortcuts import get_object_or_404
 from django.shortcuts import get_object_or_404
 from drf_yasg import openapi
 from drf_yasg import openapi
@@ -528,32 +528,39 @@ class ConnectedDeviceViewSet(ViewSet):
     * `peer_interface`: The name of the peer interface
     * `peer_interface`: The name of the peer interface
     """
     """
     permission_classes = [IsAuthenticatedOrLoginNotRequired]
     permission_classes = [IsAuthenticatedOrLoginNotRequired]
-    _device_param = Parameter('peer_device', 'query',
-                              description='The name of the peer device', required=True, type=openapi.TYPE_STRING)
-    _interface_param = Parameter('peer_interface', 'query',
-                                 description='The name of the peer interface', required=True, type=openapi.TYPE_STRING)
+    _device_param = Parameter(
+        name='peer_device',
+        in_='query',
+        description='The name of the peer device',
+        required=True,
+        type=openapi.TYPE_STRING
+    )
+    _interface_param = Parameter(
+        name='peer_interface',
+        in_='query',
+        description='The name of the peer interface',
+        required=True,
+        type=openapi.TYPE_STRING
+    )
 
 
     def get_view_name(self):
     def get_view_name(self):
         return "Connected Device Locator"
         return "Connected Device Locator"
 
 
     @swagger_auto_schema(
     @swagger_auto_schema(
-        manual_parameters=[_device_param, _interface_param], responses={'200': serializers.DeviceSerializer})
+        manual_parameters=[_device_param, _interface_param],
+        responses={'200': serializers.DeviceSerializer}
+    )
     def list(self, request):
     def list(self, request):
 
 
         peer_device_name = request.query_params.get(self._device_param.name)
         peer_device_name = request.query_params.get(self._device_param.name)
-        if not peer_device_name:
-            # TODO: remove this after 2.4 as the switch to using underscores is a breaking change
-            peer_device_name = request.query_params.get('peer-device')
         peer_interface_name = request.query_params.get(self._interface_param.name)
         peer_interface_name = request.query_params.get(self._interface_param.name)
-        if not peer_interface_name:
-            # TODO: remove this after 2.4 as the switch to using underscores is a breaking change
-            peer_interface_name = request.query_params.get('peer-interface')
+
         if not peer_device_name or not peer_interface_name:
         if not peer_device_name or not peer_interface_name:
             raise MissingFilterException(detail='Request must include "peer_device" and "peer_interface" filters.')
             raise MissingFilterException(detail='Request must include "peer_device" and "peer_interface" filters.')
 
 
         # Determine local interface from peer interface's connection
         # Determine local interface from peer interface's connection
         peer_interface = get_object_or_404(Interface, device__name=peer_device_name, name=peer_interface_name)
         peer_interface = get_object_or_404(Interface, device__name=peer_device_name, name=peer_interface_name)
-        local_interface = peer_interface.connected_interface
+        local_interface = peer_interface._connected_interface
 
 
         if local_interface is None:
         if local_interface is None:
             return Response()
             return Response()

+ 40 - 38
netbox/dcim/tests/test_api.py

@@ -3300,44 +3300,46 @@ class ConnectionTest(APITestCase):
         self.assertEqual(circuittermination1.connected_endpoint, interface1)
         self.assertEqual(circuittermination1.connected_endpoint, interface1)
 
 
 
 
-# class ConnectedDeviceTest(APITestCase):
-#
-#     def setUp(self):
-#
-#         super(ConnectedDeviceTest, self).setUp()
-#
-#         self.site1 = Site.objects.create(name='Test Site 1', slug='test-site-1')
-#         self.site2 = Site.objects.create(name='Test Site 2', slug='test-site-2')
-#         manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
-#         self.devicetype1 = DeviceType.objects.create(
-#             manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
-#         )
-#         self.devicetype2 = DeviceType.objects.create(
-#             manufacturer=manufacturer, model='Test Device Type 2', slug='test-device-type-2'
-#         )
-#         self.devicerole1 = DeviceRole.objects.create(
-#             name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
-#         )
-#         self.devicerole2 = DeviceRole.objects.create(
-#             name='Test Device Role 2', slug='test-device-role-2', color='00ff00'
-#         )
-#         self.device1 = Device.objects.create(
-#             device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice1', site=self.site1
-#         )
-#         self.device2 = Device.objects.create(
-#             device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice2', site=self.site1
-#         )
-#         self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
-#         self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
-#         InterfaceConnection.objects.create(interface_a=self.interface1, interface_b=self.interface2)
-#
-#     def test_get_connected_device(self):
-#
-#         url = reverse('dcim-api:connected-device-list')
-#         response = self.client.get(url + '?peer-device=TestDevice2&peer-interface=eth0', **self.header)
-#
-#         self.assertHttpStatus(response, status.HTTP_200_OK)
-#         self.assertEqual(response.data['name'], self.device1.name)
+class ConnectedDeviceTest(APITestCase):
+
+    def setUp(self):
+
+        super(ConnectedDeviceTest, self).setUp()
+
+        self.site1 = Site.objects.create(name='Test Site 1', slug='test-site-1')
+        self.site2 = Site.objects.create(name='Test Site 2', slug='test-site-2')
+        manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
+        self.devicetype1 = DeviceType.objects.create(
+            manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
+        )
+        self.devicetype2 = DeviceType.objects.create(
+            manufacturer=manufacturer, model='Test Device Type 2', slug='test-device-type-2'
+        )
+        self.devicerole1 = DeviceRole.objects.create(
+            name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
+        )
+        self.devicerole2 = DeviceRole.objects.create(
+            name='Test Device Role 2', slug='test-device-role-2', color='00ff00'
+        )
+        self.device1 = Device.objects.create(
+            device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice1', site=self.site1
+        )
+        self.device2 = Device.objects.create(
+            device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice2', site=self.site1
+        )
+        self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
+        self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
+
+        cable = Cable(termination_a=self.interface1, termination_b=self.interface2)
+        cable.save()
+
+    def test_get_connected_device(self):
+
+        url = reverse('dcim-api:connected-device-list')
+        response = self.client.get(url + '?peer_device=TestDevice2&peer_interface=eth0', **self.header)
+
+        self.assertHttpStatus(response, status.HTTP_200_OK)
+        self.assertEqual(response.data['name'], self.device1.name)
 
 
 
 
 class VirtualChassisTest(APITestCase):
 class VirtualChassisTest(APITestCase):