Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit SNMP communities #35

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading