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))
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
def sync_interfaces(device, interfaces): """ Syncing interfaces :param device: object NetBox Device :param interfaces: list of lists interfaces: interface['NAME'] - Name of interface interface['MAC'] - Mac-Address interface['IP'] - List of IP-address interface['MTU'] - MTU interface['DESCR'] - Description of interfaces interface['TYPE'] - Physical type of interface (Default 1G-cooper - cannot get from linux) interface['STATE'] - UP|DOWN :return: status: bool, message: string """ # Updated interface counter count = 0 # Init interfaces filter iface_filter = device.cf().get('Interfaces filter') try: iface_regex = re.compile(iface_filter) except Exception as e: logger.warning("Cannot parse regex for interface filter: {}".format(e)) iface_regex = re.compile('.*') for interface in interfaces: name = interface.get('NAME') mac = interface.get('MAC') ips = interface.get('IP') mtu = interface.get('MTU') description = interface.get('DESCR') iface_type = interface.get('TYPE') iface_state = interface.get('STATE') # TODO: add a bonding support iface_master = interface.get('BOND') # Check interface filter if not iface_regex.match(name): logger.debug("Iface {} not match with regex".format(name)) continue # Get interface from device - for check if exist ifaces = device.interfaces.filter(name=name) if ifaces: logger.info( "Interface {} is exist on device {}, will update".format( name, device.name)) # TODO: I think, that only one item will be in filter, but need to add check for it iface = ifaces[0] else: logger.info( "Interface {} is not exist on device {}, will create new". format(name, device.name)) iface = Interface(name=name) iface.device = device logger.info( "Will be save next parameters: Name:{name}, MAC: {mac}, MTU: {mtu}, Descr: {description}" .format(name=name, mac=mac, mtu=mtu, description=description)) if description: iface.description = description else: iface.description = '' iface.mac_address = mac # MTU should be less 32767 if int(mtu) < MAX_MTU: iface.mtu = mtu logger.info("Interface state is {}".format(iface_state)) iface.enabled = 'up' in iface_state.lower() iface.form_factor = _get_interface_type(name) try: iface.save() except Exception as e: logger.error("Cannot save interface, error is {}".format(e)) else: count += 1 logger.info("Interface {} was succesfully saved".format( name, device.name)) try: _connect_interface(iface) except: logger.error("Problem with connection function") # IP syncing if len(ips) > 0: for address in ips: addr = IPAddress() addr.interface = iface logger.info("Address is: {}".format(addr)) # TODO: Need a test ipv6 addresses try: # tries to determine is this address exist if iface.ip_addresses.filter(address=address): continue addr.address = IPNetwork(address) addr.save() except: logger.warning( "Cannot set address {} on interface".format(address)) if count == 0: return False, "Can't update any interface, see a log for details" return True, "Successfully updated {} interfaces".format(count)