From 9e41ff796e8810bfbc32eb1788eed5aec039e3af Mon Sep 17 00:00:00 2001 From: Cedric Paillet Date: Tue, 11 Jun 2024 15:22:14 +0200 Subject: [PATCH] fix(snmp): Enhance API output Enhanced the /api/plugins/cmdb/snmp/ endpoint to include detailed information for devices and communities, not just the primary key (pk). --- .../netbox_cmdb/api/snmp/serializers.py | 20 +++++++++++++++++++ netbox_cmdb/netbox_cmdb/api/snmp/views.py | 12 ++++++++++- .../tests/snmp/test_snmp_serializer.py | 3 ++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/netbox_cmdb/netbox_cmdb/api/snmp/serializers.py b/netbox_cmdb/netbox_cmdb/api/snmp/serializers.py index 36fb22e..1fd4b24 100644 --- a/netbox_cmdb/netbox_cmdb/api/snmp/serializers.py +++ b/netbox_cmdb/netbox_cmdb/api/snmp/serializers.py @@ -3,6 +3,7 @@ from rest_framework.serializers import ModelSerializer from netbox_cmdb.models.snmp import SNMP, SNMPCommunity +from netbox_cmdb.api.common_serializers import CommonDeviceSerializer class SNMPCommunitySerializer(ModelSerializer): @@ -12,8 +13,27 @@ class Meta: fields = "__all__" +class SNMPCommunityReadSerializer(ModelSerializer): + + class Meta: + model = SNMPCommunity + fields = ["community", "type"] + + +class SNMPReadSerializer(ModelSerializer): + + device = CommonDeviceSerializer() + community_list = SNMPCommunityReadSerializer(many=True) + + class Meta: + model = SNMP + fields = "__all__" + + class SNMPSerializer(ModelSerializer): + device = CommonDeviceSerializer() + class Meta: model = SNMP fields = "__all__" diff --git a/netbox_cmdb/netbox_cmdb/api/snmp/views.py b/netbox_cmdb/netbox_cmdb/api/snmp/views.py index 561635a..e75a37f 100644 --- a/netbox_cmdb/netbox_cmdb/api/snmp/views.py +++ b/netbox_cmdb/netbox_cmdb/api/snmp/views.py @@ -4,7 +4,12 @@ from netbox_cmdb.api.viewsets import CustomNetBoxModelViewSet from netbox_cmdb.models.snmp import SNMP, SNMPCommunity -from netbox_cmdb.api.snmp.serializers import SNMPCommunitySerializer, SNMPSerializer +from netbox_cmdb.api.snmp.serializers import ( + SNMPCommunitySerializer, + SNMPReadSerializer, + SNMPSerializer, +) +from rest_framework.response import Response class SNMPCommunityViewSet(CustomNetBoxModelViewSet): @@ -26,3 +31,8 @@ class SNMPViewSet(CustomNetBoxModelViewSet): "device__id", "device__name", ] + filtersets.device_location_filterset + + def list(self, request): + queryset = SNMP.objects.all() + serializer = SNMPReadSerializer(queryset, many=True) + return Response(serializer.data) diff --git a/netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py b/netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py index 7002579..1bddc43 100644 --- a/netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py +++ b/netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py @@ -15,10 +15,11 @@ def test_create(self): assert community1.community == "my_community" assert community1.type == SNMPCommunityType.RO + community1.save() data = { "device": self.device1.pk, - "community_list": [community1.pk], + "community_list": [{"id": community1.pk}], "location": "my_location", "contact": "my_team", }