def renumber_lans(self, old, new):
        """
        Renumber prefix and netixlan's that fall into that prefix
        """

        old_prefix = ipaddress.ip_network(old)
        new_prefix = ipaddress.ip_network(new)

        if old_prefix.version != new_prefix.version:
            self.log("[error] {}".format("New prefix needs to be of same " \
                                         "protocol as old prefix"))

        prefixes = IXLanPrefix.objects.filter(prefix=old, ixlan__ix_id=self.ix)
        netixlans = NetworkIXLan.objects.filter(ixlan__ix_id=self.ix)

        if self.ixlan:
            self.log("Only replacing in ixlan {}".format(
                self.ixlan.descriptive_name))
            prefixes = prefixes.filter(ixlan=ixlan)
            netixlans = netixlans.filter(ixlan=ixlan)

        for prefix in prefixes:
            self.log("Renumbering {} -> {}".format(prefix.descriptive_name,
                                                   new_prefix))
            prefix.prefix = new_prefix
            prefix.full_clean()
            if self.commit:
                prefix.save()

        for netixlan in netixlans:
            old_addr = netixlan.ipaddr(old_prefix.version)
            new_addr = renumber_ipaddress(old_addr, old_prefix, new_prefix)
            self.log("Renumbering {} -> {}".format(
                netixlan.descriptive_name_ipv(new_addr.version), new_addr))

            if new_addr.version == 4:
                netixlan.ipaddr4 = new_addr
            else:
                netixlan.ipaddr6 = new_addr

            try:
                netixlan.full_clean()
            except ValidationError as exc:
                if not self.commit and str(exc).find("outside of prefix") > -1:
                    continue
                else:
                    self.log("[error] could not renumber {}: {}".format(
                        old_addr, exc))
                    continue
            except Exception as exc:
                self.log("[error] could not renumber {}: {}".format(
                    old_addr, exc))
                continue

            if self.commit:
                netixlan.save()
Example #2
0
def test_renumber_ipaddress():
    ip4 = renumber_ipaddress(
        ipaddress.ip_address("206.41.110.48"),
        ipaddress.ip_network("206.41.110.0/24"),
        ipaddress.ip_network("206.41.111.0/24"),
    )

    assert ip4.compressed == "206.41.111.48"

    ip6 = renumber_ipaddress(
        ipaddress.ip_address("2001:504:41:110::20"),
        ipaddress.ip_network("2001:504:41:110::/64"),
        ipaddress.ip_network("2001:504:41:111::/64"),
    )

    assert ip6.compressed == "2001:504:41:111::20"

    with pytest.raises(ValueError):
        renumber_ipaddress(
            ipaddress.ip_address("2001:504:41:110::20"),
            ipaddress.ip_network("206.41.110.0/24"),
            ipaddress.ip_network("206.41.111.0/24"),
        )

    with pytest.raises(ValueError):
        renumber_ipaddress(
            ipaddress.ip_address("2001:504:41:110::20"),
            ipaddress.ip_network("2001:504:41:110::/64"),
            ipaddress.ip_network("206.41.111.0/24"),
        )

    with pytest.raises(ValueError):
        renumber_ipaddress(
            ipaddress.ip_address("206.41.110.48"),
            ipaddress.ip_network("206.41.0.0/21"),
            ipaddress.ip_network("206.41.111.0/24"),
        )