Fixes #1982: Swagger NAPALM documentation

This commit is contained in:
Saria Hajjar 2020-01-08 13:34:46 +00:00
parent 770f4c962c
commit ea05b5b606
3 changed files with 16 additions and 0 deletions

View File

@ -2,6 +2,7 @@
## Enhancements ## Enhancements
* [#1982](https://github.com/netbox-community/netbox/issues/1982) - Improved NAPALM method documentation in Swagger
* [#2050](https://github.com/netbox-community/netbox/issues/2050) - Preview image attachments when hovering the link * [#2050](https://github.com/netbox-community/netbox/issues/2050) - Preview image attachments when hovering the link
* [#3187](https://github.com/netbox-community/netbox/issues/3187) - Add rack selection field to rack elevations * [#3187](https://github.com/netbox-community/netbox/issues/3187) - Add rack selection field to rack elevations

View File

@ -370,6 +370,10 @@ class DeviceWithConfigContextSerializer(DeviceSerializer):
return obj.get_config_context() return obj.get_config_context()
class DeviceNAPALMSerializer(serializers.Serializer):
method = serializers.DictField()
class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer): class ConsoleServerPortSerializer(TaggitSerializer, ConnectedEndpointSerializer):
device = NestedDeviceSerializer() device = NestedDeviceSerializer()
cable = NestedCableSerializer(read_only=True) cable = NestedCableSerializer(read_only=True)

View File

@ -327,6 +327,13 @@ class DeviceViewSet(CustomFieldModelViewSet):
) )
filterset_class = filters.DeviceFilter filterset_class = filters.DeviceFilter
_method = Parameter(
name='method',
in_='query',
required=True,
type=openapi.TYPE_STRING
)
def get_serializer_class(self): def get_serializer_class(self):
""" """
Select the specific serializer based on the request context. Select the specific serializer based on the request context.
@ -358,11 +365,15 @@ class DeviceViewSet(CustomFieldModelViewSet):
return Response(serializer.data) return Response(serializer.data)
@swagger_auto_schema(manual_parameters=[_method], responses={'200': serializers.DeviceNAPALMSerializer})
@action(detail=True, url_path='napalm') @action(detail=True, url_path='napalm')
def napalm(self, request, pk): def napalm(self, request, pk):
""" """
Execute a NAPALM method on a Device Execute a NAPALM method on a Device
""" """
if not request.GET.get('method'):
raise ServiceUnavailable('No NAPALM methods were specified.')
device = get_object_or_404(Device, pk=pk) device = get_object_or_404(Device, pk=pk)
if not device.primary_ip: if not device.primary_ip:
raise ServiceUnavailable("This device does not have a primary IP address configured.") raise ServiceUnavailable("This device does not have a primary IP address configured.")