class ConnectionBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField( queryset=Connection.objects.all(), widget=forms.MultipleHiddenInput ) state = forms.ChoiceField( required=False, choices=add_blank_choice(ConnectionState.choices), widget=StaticSelect, ) internet_exchange_point = DynamicModelChoiceField( required=False, queryset=InternetExchange.objects.all(), help_text="IXP to which this connection connects", ) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router on which this connection is setup", ) config_context = JSONField( required=False, label="Config context", widget=SmallTextarea ) class Meta: model = Connection fields = ("state", "internet_exchange_point", "router", "config_context") nullable_fields = ("router",)
class RouterBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField(queryset=Router.objects.all(), widget=forms.MultipleHiddenInput) local_autonomous_system = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) platform = DynamicModelChoiceField(required=False, queryset=Platform.objects.all()) encrypt_passwords = forms.NullBooleanField(required=False, label="Encrypt Passwords", widget=CustomNullBooleanSelect) configuration_template = DynamicModelChoiceField( required=False, queryset=Configuration.objects.all()) device_state = forms.ChoiceField( required=False, initial=DeviceState.ENABLED, choices=add_blank_choice(DeviceState.choices), widget=StaticSelect, ) comments = CommentField(widget=SmallTextarea) class Meta: nullable_fields = ["comments"]
class ConnectionForm(BootstrapMixin, forms.ModelForm): state = forms.ChoiceField(choices=ConnectionState.choices, widget=StaticSelect) internet_exchange_point = DynamicModelChoiceField( required=False, queryset=InternetExchange.objects.all(), help_text="IXP to which this connection connects", ) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router on which this connection is setup", ) comments = CommentField() tags = TagField(required=False) class Meta: model = Connection fields = ( "vlan", "ipv6_address", "ipv4_address", "internet_exchange_point", "router", "interface", "description", "comments", "tags", ) labels = { "vlan": "VLAN", "ipv6_address": "IPv6 address", "ipv4_address": "IPv4 address", }
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm): local_autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), label="Autonomous System") bgp_group = DynamicModelChoiceField(required=False, queryset=BGPGroup.objects.all(), label="BGP Group") relationship = forms.ChoiceField(choices=BGPRelationship.choices, widget=StaticSelect) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router on which this session is configured", ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) password = PasswordField(required=False, render_value=True) comments = CommentField() tags = TagField(required=False) class Meta: model = DirectPeeringSession fields = ( "local_autonomous_system", "local_ip_address", "autonomous_system", "bgp_group", "relationship", "ip_address", "password", "multihop_ttl", "enabled", "router", "import_routing_policies", "export_routing_policies", "comments", "tags", ) labels = { "local_ip_address": "Local IP Address", "ip_address": "IP Address" } help_texts = { "local_ip_address": "IPv6 or IPv4 address", "ip_address": "IPv6 or IPv4 address", "enabled": "Should this session be enabled?", }
class InternetExchangeForm(BootstrapMixin, forms.ModelForm): slug = SlugField(max_length=255) local_autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) communities = DynamicModelMultipleChoiceField( required=False, queryset=Community.objects.all()) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router connected to the Internet Exchange point", ) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchange fields = ( "name", "slug", "local_autonomous_system", "ipv6_address", "ipv4_address", "communities", "import_routing_policies", "export_routing_policies", "router", "check_bgp_session_states", "comments", "tags", ) labels = { "ipv6_address": "IPv6 Address", "ipv4_address": "IPv4 Address", "check_bgp_session_states": "Poll Peering Session States", } help_texts = { "name": "Full name of the Internet Exchange point", "ipv6_address": "IPv6 Address used to peer", "ipv4_address": "IPv4 Address used to peer", "check_bgp_session_states": "If enabled, with a usable router, the state of peering sessions will be polled.", }
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm): autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.defer("prefixes")) ixp_connection = DynamicModelChoiceField( queryset=Connection.objects.all(), label="IXP connection", ) password = PasswordField(required=False, render_value=True) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchangePeeringSession fields = ( "service_reference", "autonomous_system", "ixp_connection", "ip_address", "password", "multihop_ttl", "is_route_server", "enabled", "import_routing_policies", "export_routing_policies", "comments", "tags", ) help_texts = { "ip_address": "IPv6 or IPv4 address", "is_route_server": "Define if this session is with a route server", } def clean(self): cleaned_data = super().clean() # Make sure that routing policies are compatible (address family) for policy in (cleaned_data["import_routing_policies"] | cleaned_data["export_routing_policies"]): if (policy.address_family != IPFamily.ALL and policy.address_family != cleaned_data["ip_address"].version): raise ValidationError( f"Routing policy '{policy.name}' cannot be used for this session, address families mismatch." )
class InternetExchangeForm(BootstrapMixin, forms.ModelForm): slug = SlugField( max_length=255, help_text= "Friendly unique shorthand used for URL and config. Warning: may result in change of operational state on a router if being used in the configuration.", ) local_autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.defer("prefixes"), query_params={"affiliated": True}, ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) communities = DynamicModelMultipleChoiceField( required=False, queryset=Community.objects.all()) ixapi_endpoint = DynamicModelChoiceField(required=False, label="IX-API endpoint", queryset=IXAPI.objects.all()) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchange fields = ( "name", "slug", "local_autonomous_system", "communities", "import_routing_policies", "export_routing_policies", "check_bgp_session_states", "ixapi_endpoint", "comments", "tags", ) labels = {"check_bgp_session_states": "Poll peering session states"} help_texts = { "name": "Full name of the Internet Exchange point", "check_bgp_session_states": "If enabled, with a usable router, the state of peering sessions will be polled.", }
class InternetExchangeFilterForm(BootstrapMixin, forms.Form): model = InternetExchange q = forms.CharField(required=False, label="Search") local_autonomous_system = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), to_field_name="pk", null_option="None", query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), to_field_name="pk", null_option="None", query_params={"type": "export-policy"}, ) router = DynamicModelMultipleChoiceField( required=False, queryset=Router.objects.all(), to_field_name="pk", null_option="None", ) tag = TagFilterField(model)
class DirectPeeringSessionFilterForm(BootstrapMixin, forms.Form): model = DirectPeeringSession q = forms.CharField(required=False, label="Search") local_autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, to_field_name="pk", label="Local Autonomous System", ) bgp_group = DynamicModelMultipleChoiceField( required=False, queryset=BGPGroup.objects.all(), to_field_name="pk", label="BGP Group", null_option="None", ) address_family = forms.ChoiceField(required=False, choices=IPFamily.choices, widget=StaticSelect) enabled = forms.NullBooleanField(required=False, label="Enabled", widget=CustomNullBooleanSelect) relationship = forms.MultipleChoiceField(required=False, choices=BGPRelationship.choices, widget=StaticSelectMultiple) router = DynamicModelMultipleChoiceField( required=False, queryset=Router.objects.all(), to_field_name="pk", null_option="None", ) tag = TagFilterField(model)
class AutonomousSystemEmailForm(BootstrapMixin, forms.Form): email = DynamicModelChoiceField(required=False, queryset=Email.objects.all()) recipient = forms.ChoiceField(widget=StaticSelect, label="E-mail Recipient") subject = forms.CharField(label="E-mail Subject") body = TextareaField(label="E-mail Body")
class InternetExchangeBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField( queryset=InternetExchange.objects.all(), widget=forms.MultipleHiddenInput) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "import-policy"}), ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "export-policy"}), ) communities = DynamicModelMultipleChoiceField( required=False, queryset=Community.objects.all()) check_bgp_session_states = forms.NullBooleanField( required=False, label="Poll BGP State", widget=CustomNullBooleanSelect) router = DynamicModelChoiceField(required=False, queryset=Router.objects.all()) comments = CommentField(widget=SmallTextarea) class Meta: nullable_fields = [ "import_routing_policies", "export_routing_policies", "communities", "router", "comments", ]
class RouterFilterForm(BootstrapMixin, forms.Form): model = Router q = forms.CharField(required=False, label="Search") local_autonomous_system = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) platform = forms.MultipleChoiceField(required=False, choices=Platform.choices, widget=StaticSelectMultiple) encrypt_passwords = forms.NullBooleanField(required=False, label="Encrypt Passwords", widget=CustomNullBooleanSelect) configuration_template = DynamicModelMultipleChoiceField( required=False, queryset=Configuration.objects.all(), to_field_name="pk", null_option="None", ) local_autonomous_system = DynamicModelMultipleChoiceField( required=False, queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, to_field_name="pk", null_option="None", ) tag = TagFilterField(model)
class RouterFilterForm(BootstrapMixin, forms.Form): model = Router q = forms.CharField(required=False, label="Search") local_autonomous_system_id = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.defer("prefixes"), query_params={"affiliated": True}, label="Local autonomous system", ) platform_id = DynamicModelMultipleChoiceField( required=False, queryset=Platform.objects.all(), to_field_name="pk", null_option="None", label="Platform", ) device_state = forms.MultipleChoiceField( required=False, choices=add_blank_choice(DeviceState.choices), widget=StaticSelect, ) encrypt_passwords = forms.NullBooleanField(required=False, widget=CustomNullBooleanSelect) configuration_template_id = DynamicModelMultipleChoiceField( required=False, queryset=Configuration.objects.all(), to_field_name="pk", null_option="None", label="Configuration", ) tag = TagFilterField(model)
class InternetExchangeForm(BootstrapMixin, forms.ModelForm): slug = SlugField(max_length=255) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "import-policy"}), ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "export-policy"}), ) communities = DynamicModelMultipleChoiceField( required=False, queryset=Community.objects.all()) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router connected to the Internet Exchange point", ) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchange fields = ( "peeringdb_id", "name", "slug", "ipv6_address", "ipv4_address", "communities", "import_routing_policies", "export_routing_policies", "router", "check_bgp_session_states", "comments", "tags", ) labels = { "peeringdb_id": "PeeringDB ID", "ipv6_address": "IPv6 Address", "ipv4_address": "IPv4 Address", "check_bgp_session_states": "Poll Peering Session States", } help_texts = { "peeringdb_id": "The PeeringDB ID for the IX connection (can be left empty)", "name": "Full name of the Internet Exchange point", "ipv6_address": "IPv6 Address used to peer", "ipv4_address": "IPv4 Address used to peer", "check_bgp_session_states": "If enabled, with a usable router, the state of peering sessions will be polled.", }
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm): autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), label="Autonomous System") internet_exchange = DynamicModelChoiceField( queryset=InternetExchange.objects.all(), label="Internet Exchange") password = PasswordField(required=False, render_value=True) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "import-policy"}), ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "export-policy"}), ) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchangePeeringSession fields = ( "autonomous_system", "internet_exchange", "ip_address", "password", "multihop_ttl", "is_route_server", "enabled", "import_routing_policies", "export_routing_policies", "comments", "tags", ) labels = { "ip_address": "IP Address", "is_route_server": "Route Server" } help_texts = { "ip_address": "IPv6 or IPv4 address", "is_route_server": "Define if this session is with a route server", }
class DirectPeeringSessionBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField( queryset=DirectPeeringSession.objects.all(), widget=forms.MultipleHiddenInput) local_autonomous_system = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.all(), query_params={"affiliated": True}, label="Local Autonomous System", ) enabled = forms.NullBooleanField(required=False, label="Enable", widget=CustomNullBooleanSelect) relationship = forms.ChoiceField( required=False, choices=add_blank_choice(BGPRelationship.choices), widget=StaticSelect, ) bgp_group = DynamicModelChoiceField(required=False, queryset=BGPGroup.objects.all(), label="BGP Group") import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) router = DynamicModelChoiceField(required=False, queryset=Router.objects.all()) comments = CommentField() class Meta: nullable_fields = [ "import_routing_policies", "export_routing_policies", "router", "comments", ]
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm): autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all()) ixp_connection = DynamicModelChoiceField( queryset=Connection.objects.all(), label="IXP connection", ) password = PasswordField(required=False, render_value=True) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) comments = CommentField() tags = TagField(required=False) class Meta: model = InternetExchangePeeringSession fields = ( "service_reference", "autonomous_system", "ixp_connection", "ip_address", "password", "multihop_ttl", "is_route_server", "enabled", "import_routing_policies", "export_routing_policies", "comments", "tags", ) help_texts = { "ip_address": "IPv6 or IPv4 address", "is_route_server": "Define if this session is with a route server", }
class InternetExchangeBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField( queryset=InternetExchange.objects.all(), widget=forms.MultipleHiddenInput) local_autonomous_system = DynamicModelChoiceField( required=False, queryset=AutonomousSystem.objects.defer("prefixes"), query_params={"affiliated": True}, ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) communities = DynamicModelMultipleChoiceField( required=False, queryset=Community.objects.all()) check_bgp_session_states = forms.NullBooleanField( required=False, label="Poll peering session states", widget=CustomNullBooleanSelect, ) ixapi_endpoint = DynamicModelChoiceField(required=False, label="IX-API endpoint", queryset=IXAPI.objects.all()) comments = CommentField(widget=SmallTextarea) class Meta: nullable_fields = [ "import_routing_policies", "export_routing_policies", "communities", "ixapi_endpoint", "comments", ]
class DirectPeeringSessionBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField( queryset=DirectPeeringSession.objects.all(), widget=forms.MultipleHiddenInput) enabled = forms.NullBooleanField(required=False, label="Enable", widget=CustomNullBooleanSelect) relationship = forms.ChoiceField( required=False, choices=add_blank_choice(BGP_RELATIONSHIP_CHOICES), widget=StaticSelect, ) bgp_group = DynamicModelChoiceField(required=False, queryset=BGPGroup.objects.all(), label="BGP Group") import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "import-policy"}), ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "export-policy"}), ) router = DynamicModelChoiceField(required=False, queryset=Router.objects.all()) comments = CommentField() class Meta: nullable_fields = [ "import_routing_policies", "export_routing_policies", "router", "comments", ]
class RouterBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm): pk = DynamicModelMultipleChoiceField(queryset=Router.objects.all(), widget=forms.MultipleHiddenInput) platform = forms.ChoiceField(required=False, choices=add_blank_choice(Platform.choices), widget=StaticSelect) encrypt_passwords = forms.NullBooleanField(required=False, label="Encrypt Passwords", widget=CustomNullBooleanSelect) configuration_template = DynamicModelChoiceField( required=False, queryset=Configuration.objects.all()) comments = CommentField(widget=SmallTextarea) class Meta: nullable_fields = ["comments"]
class RouterForm(BootstrapMixin, forms.ModelForm): netbox_device_id = forms.IntegerField(label="NetBox Device", initial=0) platform = forms.ChoiceField(required=False, choices=add_blank_choice(Platform.choices), widget=StaticSelect) configuration_template = DynamicModelChoiceField( required=False, queryset=Configuration.objects.all(), label="Configuration", help_text="Template used to generate device configuration", ) napalm_username = forms.CharField(required=False, label="Username") napalm_password = PasswordField(required=False, render_value=True, label="Password") napalm_timeout = forms.IntegerField( required=False, label="Timeout", help_text="The maximum time to wait for a connection in seconds", ) napalm_args = JSONField( required=False, label="Optional Arguments", help_text= "See NAPALM's <a href='http://napalm.readthedocs.io/en/latest/support/#optional-arguments'>documentation</a> for a complete list of optional arguments", widget=SmallTextarea, ) comments = CommentField() tags = TagField(required=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if settings.NETBOX_API: self.fields["netbox_device_id"] = forms.ChoiceField( label="NetBox Device", choices=[(0, "--------")] + [(device.id, device.display_name) for device in NetBox().get_devices()], widget=StaticSelect, ) self.fields["netbox_device_id"].widget.attrs["class"] = " ".join([ self.fields["netbox_device_id"].widget.attrs.get("class", ""), "form-control", ]).strip() else: self.fields["netbox_device_id"].widget = forms.HiddenInput() class Meta: model = Router fields = ( "netbox_device_id", "use_netbox", "name", "hostname", "platform", "encrypt_passwords", "configuration_template", "napalm_username", "napalm_password", "napalm_timeout", "napalm_args", "comments", "tags", ) labels = {"use_netbox": "Use NetBox"} help_texts = { "hostname": "Router hostname (must be resolvable) or IP address" }
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm): local_autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.defer("prefixes"), query_params={"affiliated": True}, ) autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.defer("prefixes")) bgp_group = DynamicModelChoiceField(required=False, queryset=BGPGroup.objects.all(), label="BGP Group") relationship = DynamicModelChoiceField(queryset=Relationship.objects.all()) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router on which this session is configured", ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "import-policy"}, ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), query_params={"type": "export-policy"}, ) password = PasswordField(required=False, render_value=True) comments = CommentField() tags = TagField(required=False) class Meta: model = DirectPeeringSession fields = ( "service_reference", "local_autonomous_system", "local_ip_address", "autonomous_system", "bgp_group", "relationship", "ip_address", "password", "multihop_ttl", "enabled", "router", "import_routing_policies", "export_routing_policies", "comments", "tags", ) labels = { "local_ip_address": "Local IP Address", "ip_address": "IP Address" } help_texts = { "local_ip_address": "IPv6 or IPv4 address", "ip_address": "IPv6 or IPv4 address", "enabled": "Should this session be enabled?", } def clean(self): cleaned_data = super().clean() # Make sure that both local and remote IP addresses are from the same family if cleaned_data["local_ip_address"] and ( cleaned_data["local_ip_address"].version != cleaned_data["ip_address"].version): raise ValidationError( "Local and remote IP addresses must belong to the same address family." ) # Make sure that routing policies are compatible (address family) for policy in cleaned_data["import_routing_policies"].union( cleaned_data["export_routing_policies"]): if (policy.address_family != IPFamily.ALL and policy.address_family != cleaned_data["ip_address"].version): raise ValidationError( f"Routing policy '{policy.name}' cannot be used for this session, address families mismatch." )
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm): local_asn = forms.IntegerField( min_value=ASN_MIN, max_value=ASN_MAX, label="Local ASN", help_text=f"ASN to be used locally, defaults to {settings.MY_ASN}", ) autonomous_system = DynamicModelChoiceField( queryset=AutonomousSystem.objects.all(), label="Autonomous System") bgp_group = DynamicModelChoiceField(required=False, queryset=BGPGroup.objects.all(), label="BGP Group") relationship = forms.ChoiceField(choices=BGPRelationship.choices, widget=StaticSelect) router = DynamicModelChoiceField( required=False, queryset=Router.objects.all(), help_text="Router on which this session is configured", ) import_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "import-policy"}), ) export_routing_policies = DynamicModelMultipleChoiceField( required=False, queryset=RoutingPolicy.objects.all(), widget=APISelectMultiple( additional_query_params={"type": "export-policy"}), ) password = PasswordField(required=False, render_value=True) comments = CommentField() tags = TagField(required=False) class Meta: model = DirectPeeringSession fields = ( "local_asn", "local_ip_address", "autonomous_system", "bgp_group", "relationship", "ip_address", "password", "multihop_ttl", "enabled", "router", "import_routing_policies", "export_routing_policies", "comments", "tags", ) labels = { "local_ip_address": "Local IP Address", "ip_address": "IP Address" } help_texts = { "local_ip_address": "IPv6 or IPv4 address", "ip_address": "IPv6 or IPv4 address", "enabled": "Should this session be enabled?", } def __init__(self, *args, **kwargs): initial = kwargs.get("initial", {}) # Set local ASN according to the one found in the settings initial.update({"local_asn": settings.MY_ASN}) super().__init__(*args, **kwargs)