Skip to content

Commit

Permalink
fix(snmp): Enhance API output
Browse files Browse the repository at this point in the history
Enhanced the /api/plugins/cmdb/snmp/ endpoint to include detailed information for devices and communities, not just the primary key (pk).
  • Loading branch information
cpaillet committed Jun 11, 2024
1 parent 8fa2862 commit 9e41ff7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
20 changes: 20 additions & 0 deletions netbox_cmdb/netbox_cmdb/api/snmp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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__"
12 changes: 11 additions & 1 deletion netbox_cmdb/netbox_cmdb/api/snmp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
3 changes: 2 additions & 1 deletion netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down

0 comments on commit 9e41ff7

Please sign in to comment.