コード例 #1
0
ファイル: forms.py プロジェクト: coredidan/peering-manager
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?",
        }
コード例 #2
0
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm):
    autonomous_system = forms.ModelChoiceField(
        queryset=AutonomousSystem.objects.all(),
        widget=APISelect(api_url="/api/peering/autonomous-systems/"),
    )
    password = PasswordField(required=False, render_value=True)
    import_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "import-policy"},
        ),
    )
    export_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "export-policy"},
        ),
    )
    comments = CommentField()
    tags = TagField(required=False)

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

    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 = {
            "autonomous_system": "AS",
            "internet_exchange": "IX",
            "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",
        }
        widgets = {
            "autonomous_system":
            APISelect(api_url="/api/peering/autonomous-systems/"),
            "internet_exchange":
            APISelect(api_url="/api/peering/internet-exchanges/"),
        }
コード例 #3
0
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."
                )
コード例 #4
0
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",
        }
コード例 #5
0
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",
        }
コード例 #6
0
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"
        }
コード例 #7
0
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)
コード例 #8
0
class InternetExchangePeeringSessionForm(BootstrapMixin, forms.ModelForm):
    password = PasswordField(required=False, render_value=True)
    import_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "import-policy"},
        ),
    )
    export_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "export-policy"},
        ),
    )
    comments = CommentField()
    tags = TagField(required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["autonomous_system"].widget.attrs[
            "data-live-search"] = "true"

    def clean(self):
        # Do the regular cleanup
        cleaned_data = super().clean()

        # This should be cleaned up, ready to be used
        password = cleaned_data["password"]
        internet_exchange = cleaned_data["internet_exchange"]

        # Process to password check/encryption if we have what we need
        if internet_exchange.router and password:
            # Encrypt the password only if it is not already
            cleaned_data["password"] = internet_exchange.router.encrypt_string(
                password)

        return cleaned_data

    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 = {
            "autonomous_system": "AS",
            "internet_exchange": "IX",
            "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",
        }
        widgets = {
            "autonomous_system":
            APISelect(api_url="/api/peering/autonomous-systems/"),
            "internet_exchange":
            APISelect(api_url="/api/peering/internet-exchanges/"),
        }
コード例 #9
0
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm):
    autonomous_system = forms.ModelChoiceField(
        queryset=AutonomousSystem.objects.all(),
        widget=APISelect(api_url="/api/peering/autonomous-systems/"),
    )
    bgp_group = forms.ModelChoiceField(
        required=False,
        queryset=BGPGroup.objects.all(),
        label="BGP Group",
        widget=APISelect(api_url="/api/peering/bgp-groups/"),
    )
    relationship = forms.ChoiceField(choices=BGP_RELATIONSHIP_CHOICES,
                                     widget=StaticSelect)
    router = forms.ModelChoiceField(
        required=False,
        queryset=Router.objects.all(),
        widget=APISelect(api_url="/api/peering/routers/"),
    )
    import_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "import-policy"},
        ),
    )
    export_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "export-policy"},
        ),
    )
    password = PasswordField(required=False, render_value=True)
    comments = CommentField()
    tags = TagField(required=False)

    def clean(self):
        # Do the regular cleanup
        cleaned_data = super().clean()

        # This should be cleaned up, ready to be used
        password = cleaned_data["password"]
        router = cleaned_data["router"]

        # Process to password check/encryption if we have what we need
        if router and password:
            # Encrypt the password only if it is not already
            cleaned_data["password"] = router.encrypt_string(password)

        return cleaned_data

    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_asn": "Local ASN",
            "local_ip_address": "Local IP Address",
            "autonomous_system": "AS",
            "ip_address": "IP Address",
        }
        help_texts = {
            "local_asn":
            "ASN to be used locally, defaults to {}".format(settings.MY_ASN),
            "local_ip_address":
            "IPv6 or IPv4 address",
            "ip_address":
            "IPv6 or IPv4 address",
            "enabled":
            "Should this session be enabled?",
            "router":
            "Router on which this session is configured",
        }
コード例 #10
0
class DirectPeeringSessionForm(BootstrapMixin, forms.ModelForm):
    autonomous_system = forms.ModelChoiceField(
        queryset=AutonomousSystem.objects.all(),
        widget=APISelect(api_url="/api/peering/autonomous-systems/"),
    )
    bgp_group = forms.ModelChoiceField(
        required=False,
        queryset=BGPGroup.objects.all(),
        label="BGP Group",
        widget=APISelect(api_url="/api/peering/bgp-groups/"),
    )
    relationship = forms.ChoiceField(choices=BGP_RELATIONSHIP_CHOICES,
                                     widget=StaticSelect)
    router = forms.ModelChoiceField(
        required=False,
        queryset=Router.objects.all(),
        widget=APISelect(api_url="/api/peering/routers/"),
    )
    import_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "import-policy"},
        ),
    )
    export_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"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_asn": "Local ASN",
            "local_ip_address": "Local IP Address",
            "autonomous_system": "AS",
            "ip_address": "IP Address",
        }
        help_texts = {
            "local_asn":
            "ASN to be used locally, defaults to {}".format(settings.MY_ASN),
            "local_ip_address":
            "IPv6 or IPv4 address",
            "ip_address":
            "IPv6 or IPv4 address",
            "enabled":
            "Should this session be enabled?",
            "router":
            "Router on which this session is configured",
        }
コード例 #11
0
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."
                )
コード例 #12
0
ファイル: forms.py プロジェクト: kim-netnod/peering-manager
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 = forms.ModelChoiceField(
        queryset=AutonomousSystem.objects.all(),
        label="Autonomous System",
        widget=APISelect(api_url="/api/peering/autonomous-systems/"),
    )
    bgp_group = forms.ModelChoiceField(
        required=False,
        queryset=BGPGroup.objects.all(),
        label="BGP Group",
        widget=APISelect(api_url="/api/peering/bgp-groups/"),
    )
    relationship = forms.ChoiceField(
        choices=BGP_RELATIONSHIP_CHOICES, widget=StaticSelect
    )
    router = forms.ModelChoiceField(
        required=False,
        queryset=Router.objects.all(),
        widget=APISelect(api_url="/api/peering/routers/"),
    )
    import_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"type": "import-policy"},
        ),
    )
    export_routing_policies = FilterChoiceField(
        required=False,
        queryset=RoutingPolicy.objects.all(),
        widget=APISelectMultiple(
            api_url="/api/peering/routing-policies/",
            query_filters={"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?",
            "router": "Router on which this session is configured",
        }

    def __init__(self, *args, **kwargs):
        initial = kwargs.get("initial", None)
        updated = {}
        if initial:
            updated["autonomous_system"] = initial.get("autonomous_system", None)
        updated["local_asn"] = settings.MY_ASN
        kwargs.update(initial=updated)
        super().__init__(*args, **kwargs)