mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Re-implemented connected device API view & test
This commit is contained in:
parent
ce00226bc6
commit
bb114ea66b
@ -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',
|
_device_param = Parameter(
|
||||||
description='The name of the peer device', required=True, type=openapi.TYPE_STRING)
|
name='peer_device',
|
||||||
_interface_param = Parameter('peer_interface', 'query',
|
in_='query',
|
||||||
description='The name of the peer interface', required=True, type=openapi.TYPE_STRING)
|
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()
|
||||||
|
@ -3300,44 +3300,46 @@ class ConnectionTest(APITestCase):
|
|||||||
self.assertEqual(circuittermination1.connected_endpoint, interface1)
|
self.assertEqual(circuittermination1.connected_endpoint, interface1)
|
||||||
|
|
||||||
|
|
||||||
# class ConnectedDeviceTest(APITestCase):
|
class ConnectedDeviceTest(APITestCase):
|
||||||
#
|
|
||||||
# def setUp(self):
|
def setUp(self):
|
||||||
#
|
|
||||||
# super(ConnectedDeviceTest, self).setUp()
|
super(ConnectedDeviceTest, self).setUp()
|
||||||
#
|
|
||||||
# self.site1 = Site.objects.create(name='Test Site 1', slug='test-site-1')
|
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')
|
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')
|
manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
|
||||||
# self.devicetype1 = DeviceType.objects.create(
|
self.devicetype1 = 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'
|
||||||
# )
|
)
|
||||||
# self.devicetype2 = DeviceType.objects.create(
|
self.devicetype2 = DeviceType.objects.create(
|
||||||
# manufacturer=manufacturer, model='Test Device Type 2', slug='test-device-type-2'
|
manufacturer=manufacturer, model='Test Device Type 2', slug='test-device-type-2'
|
||||||
# )
|
)
|
||||||
# self.devicerole1 = DeviceRole.objects.create(
|
self.devicerole1 = DeviceRole.objects.create(
|
||||||
# name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
|
name='Test Device Role 1', slug='test-device-role-1', color='ff0000'
|
||||||
# )
|
)
|
||||||
# self.devicerole2 = DeviceRole.objects.create(
|
self.devicerole2 = DeviceRole.objects.create(
|
||||||
# name='Test Device Role 2', slug='test-device-role-2', color='00ff00'
|
name='Test Device Role 2', slug='test-device-role-2', color='00ff00'
|
||||||
# )
|
)
|
||||||
# self.device1 = Device.objects.create(
|
self.device1 = Device.objects.create(
|
||||||
# device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice1', site=self.site1
|
device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice1', site=self.site1
|
||||||
# )
|
)
|
||||||
# self.device2 = Device.objects.create(
|
self.device2 = Device.objects.create(
|
||||||
# device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice2', site=self.site1
|
device_type=self.devicetype1, device_role=self.devicerole1, name='TestDevice2', site=self.site1
|
||||||
# )
|
)
|
||||||
# self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
|
self.interface1 = Interface.objects.create(device=self.device1, name='eth0')
|
||||||
# self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
|
self.interface2 = Interface.objects.create(device=self.device2, name='eth0')
|
||||||
# InterfaceConnection.objects.create(interface_a=self.interface1, interface_b=self.interface2)
|
|
||||||
#
|
cable = Cable(termination_a=self.interface1, termination_b=self.interface2)
|
||||||
# def test_get_connected_device(self):
|
cable.save()
|
||||||
#
|
|
||||||
# url = reverse('dcim-api:connected-device-list')
|
def test_get_connected_device(self):
|
||||||
# response = self.client.get(url + '?peer-device=TestDevice2&peer-interface=eth0', **self.header)
|
|
||||||
#
|
url = reverse('dcim-api:connected-device-list')
|
||||||
# self.assertHttpStatus(response, status.HTTP_200_OK)
|
response = self.client.get(url + '?peer_device=TestDevice2&peer_interface=eth0', **self.header)
|
||||||
# self.assertEqual(response.data['name'], self.device1.name)
|
|
||||||
|
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||||
|
self.assertEqual(response.data['name'], self.device1.name)
|
||||||
|
|
||||||
|
|
||||||
class VirtualChassisTest(APITestCase):
|
class VirtualChassisTest(APITestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user