def get_unused_ip_addresses(ports_client, subnets_client, network_id, subnet_id, count):
 ports = ports_client.list_ports(network_id=network_id)['ports']
 subnet = subnets_client.show_subnet(subnet_id)
 ip_net = netaddr.IPNetwork(subnet['subnet']['cidr'])
 subnet_set = netaddr.IPSet(ip_net.iter_hosts())
 alloc_set = netaddr.IPSet()
 for port in ports:
  for fixed_ip in port.get('fixed_ips'):
   alloc_set.add(fixed_ip['ip_address'])
 gateway_ip = subnet['subnet']['gateway_ip']
 if gateway_ip:
  alloc_set.add(gateway_ip)
 av_set = (subnet_set - alloc_set)
 addrs = []
 for cidr in reversed(av_set.iter_cidrs()):
  for ip in reversed(cidr):
   addrs.append(str(ip))
   if (len(addrs) == count):
    return addrs
 msg = 'Insufficient IP addresses available'
 raise lib_exc.BadRequest(message=msg)

def get_unused_ip_addresses ( ports_client , subnets_client , network_id , subnet_id , count ) : ports = ports_client . list_ports ( network_id = network_id ) [ 'ports' ] subnet = subnets_client . show_subnet ( subnet_id ) ip_net = netaddr . IPNetwork ( subnet [ 'subnet' ] [ 'cidr' ] ) subnet_set = netaddr . IPSet ( ip_net . iter_hosts ( ) ) alloc_set = netaddr . IPSet ( ) for port in ports : for fixed_ip in port . get ( 'fixed_ips' ) : alloc_set . add ( fixed_ip [ 'ip_address' ] ) gateway_ip = subnet [ 'subnet' ] [ 'gateway_ip' ] if gateway_ip : alloc_set . add ( gateway_ip ) av_set = ( subnet_set - alloc_set ) addrs = [ ] for cidr in reversed ( av_set . iter_cidrs ( ) ) : for ip in reversed ( cidr ) : addrs . append ( str ( ip ) ) if ( len ( addrs ) == count ) : return addrs msg = 'Insufficient IP addresses available' raise lib_exc . BadRequest ( message = msg )
return a list with the specified number of unused ip addresses this method uses the given ports_client to find the specified number of unused ip addresses on the given subnet using the supplied subnets_client .