|
2 | 2 | from collections import OrderedDict |
3 | 3 |
|
4 | 4 | from django.conf import settings |
5 | | -from django.http import HttpResponseForbidden, HttpResponse |
| 5 | +from django.http import Http404, HttpResponse, HttpResponseForbidden |
6 | 6 | from django.shortcuts import get_object_or_404 |
7 | 7 | from drf_yasg import openapi |
8 | 8 | from drf_yasg.openapi import Parameter |
|
17 | 17 | from dcim.models import * |
18 | 18 | from extras.api.views import ConfigContextQuerySetMixin, CustomFieldModelViewSet |
19 | 19 | from ipam.models import Prefix, VLAN |
20 | | -from netbox.api.views import ModelViewSet |
21 | 20 | from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired |
22 | 21 | from netbox.api.exceptions import ServiceUnavailable |
23 | 22 | from netbox.api.metadata import ContentTypeMetadata |
| 23 | +from netbox.api.views import ModelViewSet |
24 | 24 | from utilities.api import get_serializer_for_model |
25 | 25 | from utilities.utils import count_related, decode_dict |
26 | 26 | from virtualization.models import VirtualMachine |
@@ -675,15 +675,25 @@ def list(self, request): |
675 | 675 | if not peer_device_name or not peer_interface_name: |
676 | 676 | raise MissingFilterException(detail='Request must include "peer_device" and "peer_interface" filters.') |
677 | 677 |
|
678 | | - # Determine local interface from peer interface's connection |
| 678 | + # Determine local endpoint from peer interface's connection |
| 679 | + peer_device = get_object_or_404( |
| 680 | + Device.objects.restrict(request.user, 'view'), |
| 681 | + name=peer_device_name |
| 682 | + ) |
679 | 683 | peer_interface = get_object_or_404( |
680 | | - Interface.objects.all(), |
681 | | - device__name=peer_device_name, |
| 684 | + Interface.objects.restrict(request.user, 'view'), |
| 685 | + device=peer_device, |
682 | 686 | name=peer_interface_name |
683 | 687 | ) |
684 | | - local_interface = peer_interface.connected_endpoint |
| 688 | + endpoint = peer_interface.connected_endpoint |
685 | 689 |
|
686 | | - if local_interface is None: |
687 | | - return Response() |
| 690 | + # If an Interface, return the parent device |
| 691 | + if type(endpoint) is Interface: |
| 692 | + device = get_object_or_404( |
| 693 | + Device.objects.restrict(request.user, 'view'), |
| 694 | + pk=endpoint.device_id |
| 695 | + ) |
| 696 | + return Response(serializers.DeviceSerializer(device, context={'request': request}).data) |
688 | 697 |
|
689 | | - return Response(serializers.DeviceSerializer(local_interface.device, context={'request': request}).data) |
| 698 | + # Connected endpoint is none or not an Interface |
| 699 | + raise Http404 |
0 commit comments