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))
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