Example #1
0
 def add_addr(addr, expect_family):
     if not addr:
         return
     if addr.version != expect_family:
         raise RuntimeError("Wrong family for %r" % a)
     try:
         a = IPAddress.objects.get(
             address=addr,
             vrf=data.get("vrf"),
         )
         result = "Assigned"
     except ObjectDoesNotExist:
         a = IPAddress(
             address=addr,
             vrf=data.get("vrf"),
         )
         result = "Created"
     a.status = IPAddressStatusChoices.STATUS_ACTIVE
     a.dns_name = data["dns_name"]
     if a.interface:
         raise RuntimeError("Address %s is already assigned" % addr)
     a.interface = interface
     a.tenant = data.get("tenant")
     a.save()
     self.log_info("%s IP address %s %s" %
                   (result, a.address, a.vrf or ""))
     setattr(vm, "primary_ip%d" % a.family, a)
    def mutate_and_get_payload(cls, input, context, info):
        temp = IPAddress()

        vrf = input.get('vrf')
        tenant = input.get('tenant')
        interface = input.get('interface')
        nat_inside = input.get('nat_inside')

        if not_none(vrf):
            temp.vrf = VRF.objects.get(pk=from_global_id(vrf)[1])

        if not_none(tenant):
            temp.tenant = Tenant.objects.get(pk=from_global_id(tenant)[1])

        if not_none(interface):
            temp.interface = Interface.objects.get(
                pk=from_global_id(interface)[1])

        if not_none(nat_inside):
            temp.nat_inside = IPAddress.objects.get(
                pk=from_global_id(nat_inside)[1])

        fields = ['family', 'address', 'status', 'description']

        return NewIPAddress(ip_address=set_and_save(fields, input, temp))
Example #3
0
 def add_addr(addr, family):
     if not addr:
         return
     if addr.version != family:
         raise RuntimeError(f"Wrong family for {a}")
     try:
         a = IPAddress.objects.get(
             address=addr,
             vrf=data.get("vrf"),
         )
         result = "Assigned"
     except ObjectDoesNotExist:
         a = IPAddress(
             address=addr,
             vrf=data.get("vrf"),
         )
         result = "Created"
     a.status = IPAddressStatusChoices.STATUS_ACTIVE
     a.dns_name = data["dns_name"]
     if a.assigned_object:
         raise RuntimeError(f"Address {addr} is already assigned")
     a.assigned_object = vminterface
     a.tenant = data.get("tenant")
     a.full_clean()
     a.save()
     #a.tags.set(data[f"primary_ip{family}_tags"])
     self.log_info(f"{result} IP address {a.address} {a.vrf or ''}")
     setattr(vm, f"primary_ip{family}", a)
    def _add_ip_to_interface(self, device, interface):
        # determine prefix appropriate to site of device
        try:
            prefix = Prefix.objects.get(site=device.site,
                                        role__slug="management",
                                        tenant=device.tenant)
        except ObjectDoesNotExist:
            message = "Can't find prefix for site {} on device {}".format(
                device.site.slug, device.name)
            self.log_failure(message)
            return message
        self.log_info("Selecting address from network {}".format(
            prefix.prefix))
        available_ips = iter(prefix.get_available_ips())

        # disable 0net skipping on frack
        if device.tenant and device.tenant.slug == 'fr-tech':
            zeroth_net = None
        else:
            # skip the first /24 net as this is reserved for network devices
            zeroth_net = list(
                ipaddress.ip_network(prefix.prefix).subnets(new_prefix=24))[0]

        ip = None
        for ip in available_ips:
            address = ipaddress.ip_address(ip)
            if zeroth_net is None or address not in zeroth_net:
                break
            else:
                ip = None

        if ip:
            # create IP address as child of appropriate prefix
            newip = IPAddress(
                address="{}/{}".format(ip, prefix.prefix.prefixlen),
                status=IPADDRESS_STATUS_ACTIVE,
                family=prefix.family,
            )
            # save ASAP
            newip.save()
            newip.vrf = prefix.vrf.pk if prefix.vrf else None
            # assign ip to interface
            newip.interface = interface
            newip.tenant = device.tenant
            newip.save()

            message = "Created ip {} for mgmt on device {}".format(
                newip, device.name)
            self.log_success(message)
            return message

        # fall through to failure
        message = "Not enough IPs to allocate one on prefix {}".format(
            prefix.prefix)
        self.log_failure(message)
        return message