Skip to content

Commit

Permalink
Merge pull request #35 from cpaillet/snmp
Browse files Browse the repository at this point in the history
Limit SNMP communities
  • Loading branch information
cpaillet authored Jul 12, 2024
2 parents d808393 + b9df8cf commit be064c4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion netbox_cmdb/netbox_cmdb/api/snmp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Route Policy serializers."""

from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import ModelSerializer, ValidationError

from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.api.common_serializers import CommonDeviceSerializer
from netbox_cmdb.constants import MAX_COMMUNITY_PER_DEVICE


class SNMPCommunitySerializer(ModelSerializer):
Expand Down Expand Up @@ -37,3 +38,10 @@ class SNMPSerializer(ModelSerializer):
class Meta:
model = SNMP
fields = "__all__"

def validate_community_list(self, value):
if len(value) > MAX_COMMUNITY_PER_DEVICE:
raise ValidationError(
f"You cannot select more than {MAX_COMMUNITY_PER_DEVICE} SNMP Communities."
)
return value
3 changes: 3 additions & 0 deletions netbox_cmdb/netbox_cmdb/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
BGP_MIN_ASN = 1
BGP_MAX_ASN = 4294967294

# As SONIC device don't support more than 1 community
MAX_COMMUNITY_PER_DEVICE = 1
9 changes: 9 additions & 0 deletions netbox_cmdb/netbox_cmdb/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils.translation import gettext as _
from extras.models import Tag
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.constants import MAX_COMMUNITY_PER_DEVICE
from utilities.forms import DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, MultipleChoiceField

Expand Down Expand Up @@ -93,6 +94,14 @@ class Meta:
model = SNMP
fields = ["device", "community_list", "location", "contact"]

def clean_community_list(self):
community_list = self.cleaned_data.get("community_list")
if len(community_list) > MAX_COMMUNITY_PER_DEVICE:
raise forms.ValidationError(
f"You cannot select more than {MAX_COMMUNITY_PER_DEVICE} SNMP Communities."
)
return community_list


class SNMPCommunityGroupForm(NetBoxModelForm):
class Meta:
Expand Down
17 changes: 17 additions & 0 deletions netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ def test_create(self):
assert conf.contact == "my_team"
assert conf.community_list.all()[0] == community1
assert conf.device == self.device1

data = {"name": "my_comm2", "community": "my_community2", "type": "readonly"}
snmpcommunity_serializer = SNMPCommunitySerializer(data=data)
assert snmpcommunity_serializer.is_valid() == True
snmpcommunity_serializer.save()
community2 = SNMPCommunity.objects.get(name="my_comm2")

data = {
"device": self.device2.pk,
"community_list": [community1.pk, community2.pk],
"location": "my_location",
"contact": "my_team",
}

snmp_serializer = SNMPSerializer(data=data)
# We are trying to add more than 1 community to the device
assert snmp_serializer.is_valid() == False

0 comments on commit be064c4

Please sign in to comment.