Пример #1
0
 def is_discovery_enabled(cls, vrf, afi, address):
     prefix = Prefix.get_parent(vrf, afi, address)
     k = "ip-discovery-enable-%s" % prefix.id
     ds = cache.get(k)
     if ds:
         return ds == "E"
     r = prefix.effective_ip_discovery
     cache.set(k, r, 600)
     return r == "E"
Пример #2
0
 def has_address_permission(self, vrf, address):
     """
     Check discovery has permission to manipulate address
     :param vrf: VRF instance
     :param address: DiscoveredAddress instance
     :return:
     """
     parent = Prefix.get_parent(vrf, "6" if ":" in address.address else "4",
                                address.address)
     if parent:
         return parent.effective_address_discovery == "E"
     return False
Пример #3
0
 def has_prefix_permission(self, vrf, prefix):
     """
     Check discovery has permission to manipulate prefix
     :param vrf: VRF instance
     :param prefix: DiscoveredPrefix instance
     :return:
     """
     parent = Prefix.get_parent(vrf, "6" if ":" in prefix.prefix else "4",
                                prefix.prefix)
     if parent:
         return parent.effective_prefix_discovery == "E"
     return False
Пример #4
0
    def view_quickjump(self, request, vrf_id, afi):
        """
        Quickjump to closest suitable block
        """

        # Interpolate string to valid IPv4 address
        def interpolate_ipv4(s):
            p = s.split(".")
            if len(p) > 4:
                return None
            elif len(p) < 4:
                p += ["0"] * (4 - len(p))
            s = ".".join(p)
            if not is_ipv4(s):
                return None
            return s

        # Interpolate string to valid IPv6 address
        # @todo: implement
        def interpolate_ipv6(s):
            if not is_ipv6(s):
                return None
            return s

        vrf = self.get_object_or_404(VRF, id=int(vrf_id))
        if (afi == "4" and not vrf.afi_ipv4) or (afi == "6"
                                                 and not vrf.afi_ipv6):
            return self.response_forbidden("Invalid AFI")
        if request.POST:
            form = self.QuickJumpForm(request.POST)
            if form.is_valid():
                prefix = form.cleaned_data["jump"].strip()
                # Interpolate prefix
                if afi == "4":
                    prefix = interpolate_ipv4(prefix)
                else:
                    prefix = interpolate_ipv6(prefix)
                if not prefix:
                    self.message_user(request, _("Invalid address"))
                    return self.response_redirect_to_referrer(request)
                # Find prefix
                prefix = Prefix.get_parent(vrf, afi, prefix).prefix
                # Redirect
                self.message_user(
                    request,
                    _("Redirected to %(prefix)s") % {"prefix": prefix})
                return self.response_redirect("ip:ipam:vrf_index", vrf.id, afi,
                                              prefix)
        return self.response_redirect_to_referrer(request)
Пример #5
0
    def view_quickjump(self, request, vrf_id, afi):
        """
        Quickjump to closest suitable block
        """

        # Interpolate string to valid IPv4 address
        def interpolate_ipv4(s):
            p = s.split(".")
            if len(p) > 4:
                return None
            elif len(p) < 4:
                p += ["0"] * (4 - len(p))
            s = ".".join(p)
            if not is_ipv4(s):
                return None
            return s

        # Interpolate string to valid IPv6 address
        # @todo: implement
        def interpolate_ipv6(s):
            if not is_ipv6(s):
                return None
            return s

        vrf = self.get_object_or_404(VRF, id=int(vrf_id))
        if (afi == "4" and not vrf.afi_ipv4) or (afi == "6"
                                                 and not vrf.afi_ipv6):
            return self.response_forbidden("Invalid AFI")
        if request.POST:
            d = orjson.loads(request.body)
            prefix = d["jump"].strip()
            # Interpolate prefix
            if afi == "4":
                prefix = interpolate_ipv4(prefix)
            else:
                prefix = interpolate_ipv6(prefix)
            if not prefix:
                return self.response_bad_request("Invalid address")
            # Find prefix
            prefix = Prefix.get_parent(vrf, afi, prefix)
            return {"id": prefix.id}
        return self.response_redirect_to_referrer(request)