'Gets a fixed IP from the pool.'

def allocate_fixed_ip(self, context, instance_id, network, **kwargs):

    address = None
    instance = objects.Instance.get_by_uuid(context, instance_id)
    LOG.debug('Allocate    fixed    IP    on    network    %s', network['uuid'], instance=instance)
    cleanup = []
    quotas = self.quotas_cls(context=context)
    (quota_project, quota_user) = quotas_obj.ids_from_instance(context, instance)
    try:
        quotas.check_deltas(context, {'fixed_ips': 1}, quota_project)
    except exception.OverQuota as exc:
        count = exc.kwargs['usages']['fixed_ips']
        LOG.warning(_LW('Quota    exceeded    for    project    %(pid)s,    tried    to    allocate    fixed    IP.    %(used)s    of    %(allowed)s    are    in    use    or    are    already    reserved.'), {'pid': quota_project, 'used': count, 'allowed': exc.kwargs['quotas']['fixed_ips']}, instance_uuid=instance_id)
        raise exception.FixedIpLimitExceeded()
    try:
        if network['cidr']:
            vif = objects.VirtualInterface.get_by_instance_and_network(context, instance_id, network['id'])
            if (vif is None):
                LOG.debug('vif    for    network    %(network)s    is    used    up,    trying    to    create    new    vif', {'network': network['id']}, instance=instance)
                vif = self._add_virtual_interface(context, instance_id, network['id'])
            address = kwargs.get('address', None)
            if address:
                LOG.debug('Associating    instance    with    specified    fixed    IP    %(address)s    in    network    %(network)s    on    subnet    %(cidr)s.', {'address': address, 'network': network['id'], 'cidr': network['cidr']}, instance=instance)
                fip = objects.FixedIP.associate(context, str(address), instance_id, network['id'], vif_id=vif.id)
            else:
                LOG.debug('Associating    instance    with    fixed    IP    from    pool    in    network    %(network)s    on    subnet    %(cidr)s.', {'network': network['id'], 'cidr': network['cidr']}, instance=instance)
                fip = objects.FixedIP.associate_pool(context.elevated(), network['id'], instance_id, vif_id=vif.id)
                LOG.debug('Associated    instance    with    fixed    IP:    %s', fip, instance=instance)
                address = str(fip.address)
            cleanup.append(functools.partial(fip.disassociate, context))
            if CONF.quota.recheck_quota:
                try:
                    quotas.check_deltas(context, {'fixed_ips': 0}, quota_project)
                except exception.OverQuota as exc:
                    count = exc.kwargs['usages']['fixed_ips']
                    allowed = exc.kwargs['quotas']['fixed_ips']
                    LOG.warning(_LW('Quota    exceeded    for    project    %(pid)s,    tried    to    allocate    fixed    IP.    %(used)s    of    %(allowed)s    are    in    use    or    are    already    reserved.'), {'pid': quota_project, 'used': count, 'allowed': allowed}, instance_uuid=instance_id)
                    raise exception.FixedIpLimitExceeded()
            LOG.debug('Refreshing    security    group    members    for    instance.', instance=instance)
            self._do_trigger_security_group_members_refresh_for_instance(instance_id)
            cleanup.append(functools.partial(self._do_trigger_security_group_members_refresh_for_instance, instance_id))
        name = instance.display_name
        if self._validate_instance_zone_for_dns_domain(context, instance):
            self.instance_dns_manager.create_entry(name, str(fip.address), 'A', self.instance_dns_domain)
            cleanup.append(functools.partial(self.instance_dns_manager.delete_entry, name, self.instance_dns_domain))
            self.instance_dns_manager.create_entry(instance_id, str(fip.address), 'A', self.instance_dns_domain)
            cleanup.append(functools.partial(self.instance_dns_manager.delete_entry, instance_id, self.instance_dns_domain))
        LOG.debug('Setting    up    network    %(network)s    on    host    %(host)s.', {'network': network['id'], 'host': self.host}, instance=instance)
        self._setup_network_on_host(context, network)
        cleanup.append(functools.partial(self._teardown_network_on_host, context, network))
        if (address is None):
            LOG.debug('Fixed    IP    is    setup    on    network    %s    but    not    returning    the    specific    IP    from    the    base    network    manager.', network['uuid'], instance=instance)
        else:
            LOG.debug('Allocated    fixed    IP    %s    on    network    %s', address, network['uuid'], instance=instance)
        return address
    except Exception:
        with excutils.save_and_reraise_exception():
            for f in cleanup:
                try:
                    f()
                except Exception:
                    LOG.warning(_LW('Error    cleaning    up    fixed    IP    allocation.    Manual    cleanup    may    be    required.'), exc_info=True)
