Skip to content

Commit

Permalink
Merge pull request #2520 from stveit/module-endpoint
Browse files Browse the repository at this point in the history
Module endpoint
  • Loading branch information
stveit authored Jan 10, 2023
2 parents 305613c + 6089e9f commit bdd562b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
30 changes: 29 additions & 1 deletion python/nav/web/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,38 @@ class Meta(object):
fields = ('id', 'cabling', 'split')


class NetboxInlineSerializer(serializers.ModelSerializer):
"""Serializer for including netbox information in other serializers"""

class Meta(object):
model = manage.Netbox
fields = ('id', 'sysname')


class DeviceSerializer(serializers.ModelSerializer):
"""Serializer for the device model"""

class Meta(object):
model = manage.Device
fields = '__all__'


class ModuleInlineSerializer(serializers.ModelSerializer):
"""Serializer for including module information in other serializers"""

object_url = serializers.CharField(source='get_absolute_url')

class Meta(object):
model = manage.Module
fields = '__all__'


class ModuleSerializer(serializers.ModelSerializer):
"""Serializer for the module model"""

object_url = serializers.CharField(source='get_absolute_url')
device = DeviceSerializer()
netbox = NetboxInlineSerializer()

class Meta(object):
model = manage.Module
Expand Down Expand Up @@ -315,7 +343,7 @@ class InterfaceSerializer(serializers.ModelSerializer):
"""Serializer for the interface model"""

patches = SpecificPatchSerializer()
module = ModuleSerializer()
module = ModuleInlineSerializer()
object_url = serializers.CharField(source='get_absolute_url')
to_netbox = SubNetboxSerializer()
to_interface = SubInterfaceSerializer()
Expand Down
1 change: 1 addition & 0 deletions python/nav/web/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
basename='unrecognized-neighbor',
)
router.register(r'auditlog', auditlogapi.LogEntryViewSet, basename='auditlog')
router.register(r'module', views.ModuleViewSet, basename='module')


urlpatterns = [
Expand Down
21 changes: 21 additions & 0 deletions python/nav/web/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def get_endpoints(request=None, version=1):
),
'vlan': reverse_lazy('{}vlan-list'.format(prefix), **kwargs),
'rack': reverse_lazy('{}rack-list'.format(prefix), **kwargs),
'module': reverse_lazy('{}module-list'.format(prefix), **kwargs),
}


Expand Down Expand Up @@ -1081,3 +1082,23 @@ def get_nav_version(request):
:type request: django.http.HttpRequest
"""
return JsonResponse({"version": VERSION})


class ModuleViewSet(NAVAPIMixin, viewsets.ReadOnlyModelViewSet):
"""Lists all modules.
Filters
-------
- netbox
- device__serial
Example: `/api/1/module/?netbox=91&device__serial=AB12345`
"""

queryset = manage.Module.objects.all()
filter_backends = NAVAPIMixin.filter_backends
filterset_fields = (
'netbox',
'device__serial',
)
serializer_class = serializers.ModuleSerializer

0 comments on commit bdd562b

Please sign in to comment.