Exemplo n.º 1
0
class RouterBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm):
    pk = DynamicModelMultipleChoiceField(queryset=Router.objects.all(),
                                         widget=forms.MultipleHiddenInput)
    local_autonomous_system = DynamicModelChoiceField(
        required=False,
        queryset=AutonomousSystem.objects.defer("prefixes"),
        query_params={"affiliated": True},
        label="Local AS",
    )
    platform = DynamicModelChoiceField(required=False,
                                       queryset=Platform.objects.all())
    encrypt_passwords = forms.NullBooleanField(required=False,
                                               widget=CustomNullBooleanSelect)
    poll_bgp_sessions_state = forms.NullBooleanField(
        required=False,
        widget=CustomNullBooleanSelect,
        label="Poll BGP sessions state")
    configuration_template = DynamicModelChoiceField(
        required=False, queryset=Configuration.objects.all())
    device_state = forms.ChoiceField(
        required=False,
        choices=add_blank_choice(DeviceState.choices),
        widget=StaticSelect,
    )
    comments = CommentField(widget=SmallTextarea)

    class Meta:
        nullable_fields = ["comments"]
Exemplo n.º 2
0
class RoutingPolicyForm(BootstrapMixin, forms.ModelForm):
    slug = SlugField(max_length=255)
    type = forms.ChoiceField(choices=RoutingPolicyType.choices,
                             widget=StaticSelect)
    address_family = forms.ChoiceField(choices=IPFamily.choices,
                                       widget=StaticSelect)
    config_context = JSONField(required=False,
                               label="Config context",
                               widget=SmallTextarea)
    comments = CommentField()
    tags = TagField(required=False)

    class Meta:
        model = RoutingPolicy

        fields = (
            "name",
            "slug",
            "type",
            "weight",
            "address_family",
            "config_context",
            "comments",
            "tags",
        )
Exemplo n.º 3
0
class BGPGroupForm(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.",
    )
    comments = CommentField()
    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())
    tags = TagField(required=False)

    class Meta:
        model = BGPGroup
        fields = (
            "name",
            "slug",
            "comments",
            "import_routing_policies",
            "export_routing_policies",
            "communities",
            "tags",
        )
        help_texts = {"name": "Full name of the BGP group"}
Exemplo n.º 4
0
class InternetExchangePeeringSessionBulkEditForm(BootstrapMixin,
                                                 AddRemoveTagsForm,
                                                 BulkEditForm):
    pk = DynamicModelMultipleChoiceField(
        queryset=InternetExchangePeeringSession.objects.all(),
        widget=forms.MultipleHiddenInput,
    )
    is_route_server = forms.NullBooleanField(required=False,
                                             label="Route server",
                                             widget=CustomNullBooleanSelect)
    enabled = forms.NullBooleanField(required=False,
                                     widget=CustomNullBooleanSelect)
    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(widget=SmallTextarea)

    class Meta:
        nullable_fields = [
            "import_routing_policies",
            "export_routing_policies",
            "comments",
        ]
Exemplo n.º 5
0
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm):
    autonomous_system = DynamicModelChoiceField(
        queryset=AutonomousSystem.objects.defer("prefixes"))
    internet_exchange = DynamicModelChoiceField(
        required=False, queryset=InternetExchange.objects.all(), label="IXP")
    ixp_connection = DynamicModelChoiceField(
        queryset=Connection.objects.all(),
        query_params={"internet_exchange_point_id": "$internet_exchange"},
        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."
                )
Exemplo n.º 6
0
class CommunityBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm):
    pk = DynamicModelMultipleChoiceField(queryset=Community.objects.all(),
                                         widget=forms.MultipleHiddenInput)
    type = forms.ChoiceField(
        required=False,
        choices=add_blank_choice(CommunityType.choices),
        widget=StaticSelect,
    )
    comments = CommentField(widget=SmallTextarea)

    class Meta:
        nullable_fields = ["type", "comments"]
Exemplo n.º 7
0
class ConfigurationForm(BootstrapMixin, forms.ModelForm):
    template = TemplateField()
    comments = CommentField()
    tags = TagField(required=False)

    class Meta:
        model = Configuration
        fields = (
            "name",
            "template",
            "jinja2_trim",
            "jinja2_lstrip",
            "comments",
            "tags",
        )
Exemplo n.º 8
0
class RoutingPolicyBulkEditForm(BootstrapMixin, AddRemoveTagsForm,
                                BulkEditForm):
    pk = DynamicModelMultipleChoiceField(queryset=RoutingPolicy.objects.all(),
                                         widget=forms.MultipleHiddenInput)
    type = forms.ChoiceField(
        required=False,
        choices=add_blank_choice(RoutingPolicyType.choices),
        widget=StaticSelect,
    )
    weight = forms.IntegerField(required=False, min_value=0, max_value=32767)
    address_family = forms.ChoiceField(required=False,
                                       choices=IPFamily.choices,
                                       widget=StaticSelect)
    comments = CommentField(widget=SmallTextarea)

    class Meta:
        nullable_fields = ["comments"]
Exemplo n.º 9
0
class AutonomousSystemForm(BootstrapMixin, forms.ModelForm):
    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())
    comments = CommentField()
    tags = TagField(required=False)

    class Meta:
        model = AutonomousSystem
        fields = (
            "asn",
            "name",
            "name_peeringdb_sync",
            "irr_as_set",
            "irr_as_set_peeringdb_sync",
            "ipv6_max_prefixes",
            "ipv6_max_prefixes_peeringdb_sync",
            "ipv4_max_prefixes",
            "ipv4_max_prefixes_peeringdb_sync",
            "import_routing_policies",
            "export_routing_policies",
            "communities",
            "comments",
            "affiliated",
            "tags",
        )
        labels = {
            "name_peeringdb_sync": "Name",
            "irr_as_set_peeringdb_sync": "IRR AS-SET",
            "ipv6_max_prefixes_peeringdb_sync": "IPv6 max prefixes",
            "ipv4_max_prefixes_peeringdb_sync": "IPv4 max prefixes",
        }
        help_texts = {
            "asn": "BGP autonomous system number (32-bit capable)",
            "name": "Full name of the AS",
            "affiliated": "Check if you own/manage this AS",
        }
Exemplo n.º 10
0
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},
        label="Local AS",
    )
    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",
            "ixapi_endpoint",
            "comments",
            "tags",
        )
        help_texts = {"name": "Full name of the Internet Exchange point"}
Exemplo n.º 11
0
class CommunityForm(BootstrapMixin, forms.ModelForm):
    slug = SlugField(max_length=255)
    type = forms.ChoiceField(
        required=False,
        choices=add_blank_choice(CommunityType.choices),
        widget=StaticSelect,
        help_text=
        "Optional, Ingress for received routes, Egress for advertised routes",
    )
    comments = CommentField()
    tags = TagField(required=False)

    class Meta:
        model = Community

        fields = ("name", "value", "slug", "type", "comments", "tags")
        help_texts = {
            "value":
            'Community (<a target="_blank" href="https://tools.ietf.org/html/rfc1997">RFC1997</a>), Extended Community (<a target="_blank" href="https://tools.ietf.org/html/rfc4360">RFC4360</a>) or Large Community (<a target="_blank" href="https://tools.ietf.org/html/rfc8092">RFC8092</a>)'
        }
Exemplo n.º 12
0
class DirectPeeringSessionBulkEditForm(BootstrapMixin, AddRemoveTagsForm,
                                       BulkEditForm):
    pk = DynamicModelMultipleChoiceField(
        queryset=DirectPeeringSession.objects.all(),
        widget=forms.MultipleHiddenInput)
    local_autonomous_system = DynamicModelChoiceField(
        required=False,
        queryset=AutonomousSystem.objects.defer("prefixes"),
        query_params={"affiliated": True},
        label="Local AS",
    )
    enabled = forms.NullBooleanField(required=False,
                                     label="Enable",
                                     widget=CustomNullBooleanSelect)
    relationship = DynamicModelChoiceField(required=False,
                                           queryset=Relationship.objects.all())
    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",
        ]
Exemplo n.º 13
0
class ConnectionForm(BootstrapMixin, forms.ModelForm):
    state = forms.ChoiceField(choices=ConnectionState.choices,
                              widget=StaticSelect)
    internet_exchange_point = DynamicModelChoiceField(
        required=False,
        queryset=InternetExchange.objects.all(),
        label="IXP",
        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)
    comments = CommentField()
    tags = TagField(required=False)

    class Meta:
        model = Connection
        fields = (
            "state",
            "vlan",
            "ipv6_address",
            "ipv4_address",
            "internet_exchange_point",
            "router",
            "interface",
            "description",
            "config_context",
            "comments",
            "tags",
        )
        labels = {
            "vlan": "VLAN",
            "ipv6_address": "IPv6 address",
            "ipv4_address": "IPv4 address",
        }
Exemplo n.º 14
0
class BGPGroupBulkEditForm(BootstrapMixin, AddRemoveTagsForm, BulkEditForm):
    pk = DynamicModelMultipleChoiceField(queryset=BGPGroup.objects.all(),
                                         widget=forms.MultipleHiddenInput)
    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())
    comments = CommentField(widget=SmallTextarea)

    class Meta:
        nullable_fields = [
            "import_routing_policies",
            "export_routing_policies",
            "communities",
            "comments",
        ]
Exemplo n.º 15
0
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},
        label="Local AS",
    )
    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(widget=SmallTextarea)

    class Meta:
        nullable_fields = [
            "import_routing_policies",
            "export_routing_policies",
            "communities",
            "ixapi_endpoint",
            "comments",
        ]
Exemplo n.º 16
0
class RouterForm(BootstrapMixin, forms.ModelForm):
    netbox_device_id = forms.IntegerField(label="NetBox device", initial=0)
    platform = DynamicModelChoiceField(required=False,
                                       queryset=Platform.objects.all())
    configuration_template = DynamicModelChoiceField(
        required=False,
        queryset=Configuration.objects.all(),
        label="Configuration",
        help_text="Template used to generate device configuration",
    )
    local_autonomous_system = DynamicModelChoiceField(
        queryset=AutonomousSystem.objects.defer("prefixes"),
        query_params={"affiliated": True},
        label="Local AS",
    )
    config_context = JSONField(required=False,
                               label="Config context",
                               widget=SmallTextarea)
    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,
    )
    device_state = forms.ChoiceField(
        required=False,
        choices=add_blank_choice(DeviceState.choices),
        widget=StaticSelect,
    )
    comments = CommentField()
    tags = TagField(required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if settings.NETBOX_API:
            choices = []
            for device in NetBox().get_devices():
                try:
                    choices.append((device.id, device.display))
                except AttributeError:
                    # Fallback to hold API attribute
                    choices.append((device.id, device.display_name))

            self.fields["netbox_device_id"] = forms.ChoiceField(
                label="NetBox device",
                choices=[(0, "---------")] + choices,
                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",
            "poll_bgp_sessions_state",
            "device_state",
            "configuration_template",
            "local_autonomous_system",
            "config_context",
            "napalm_username",
            "napalm_password",
            "napalm_timeout",
            "napalm_args",
            "comments",
            "tags",
        )
        help_texts = {
            "hostname": "Router hostname (must be resolvable) or IP address"
        }
Exemplo n.º 17
0
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm):
    local_autonomous_system = DynamicModelChoiceField(
        queryset=AutonomousSystem.objects.defer("prefixes"),
        query_params={"affiliated": True},
        label="Local AS",
    )
    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()

        ip_src = cleaned_data["local_ip_address"]
        ip_dst = cleaned_data["ip_address"]

        # Make sure that both local qnd remote IP addresses belong in the same subnet
        if (cleaned_data["multihop_ttl"] == 1 and ip_src
                and (ip_src.network != ip_dst.network)):
            raise ValidationError(
                f"{ip_src} and {ip_dst} don't belong to the same subnet.")

        # 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 != ip_dst.version):
                raise ValidationError(
                    f"Routing policy '{policy.name}' cannot be used for this session, address families mismatch."
                )