diff --git a/netbox_cmdb/netbox_cmdb/constants.py b/netbox_cmdb/netbox_cmdb/constants.py index deecdf7..465de28 100644 --- a/netbox_cmdb/netbox_cmdb/constants.py +++ b/netbox_cmdb/netbox_cmdb/constants.py @@ -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 diff --git a/netbox_cmdb/netbox_cmdb/forms.py b/netbox_cmdb/netbox_cmdb/forms.py index 1c0cc19..37839be 100644 --- a/netbox_cmdb/netbox_cmdb/forms.py +++ b/netbox_cmdb/netbox_cmdb/forms.py @@ -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.netbox_cmdb.constants import MAX_COMMUNITY_PER_DEVICE from utilities.forms import DynamicModelMultipleChoiceField from utilities.forms.fields import DynamicModelChoiceField, MultipleChoiceField @@ -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: