'Handles allocating the various network resources for an instance.
rpc.called by network_api'

def allocate_for_instance(self, context, **kwargs):

    instance_uuid = kwargs['instance_id']
    if (not uuidutils.is_uuid_like(instance_uuid)):
        instance_uuid = kwargs.get('instance_uuid')
    host = kwargs['host']
    project_id = kwargs['project_id']
    rxtx_factor = kwargs['rxtx_factor']
    requested_networks = kwargs.get('requested_networks')
    if (requested_networks and (not isinstance(requested_networks, objects.NetworkRequestList))):
        requested_networks = objects.NetworkRequestList.from_tuples(requested_networks)
    vpn = kwargs['vpn']
    macs = kwargs['macs']
    admin_context = context.elevated()
    networks = self._get_networks_for_instance(context, instance_uuid, project_id, requested_networks=requested_networks)
    networks_list = [self._get_network_dict(network) for network in networks]
    LOG.debug('Networks    retrieved    for    instance:    |%s|', networks_list, instance_uuid=instance_uuid)
    try:
        self._allocate_mac_addresses(admin_context, instance_uuid, networks, macs, requested_networks)
    except Exception:
        with excutils.save_and_reraise_exception():
            objects.VirtualInterface.delete_by_instance_uuid(context, instance_uuid)
    self._allocate_fixed_ips(admin_context, instance_uuid, host, networks, vpn=vpn, requested_networks=requested_networks)
    if CONF.update_dns_entries:
        network_ids = [network['id'] for network in networks]
        self.network_rpcapi.update_dns(context, network_ids)
    net_info = self.get_instance_nw_info(admin_context, instance_uuid, rxtx_factor, host)
    LOG.info(_LI("Allocated    network:    '%s'    for    instance"), net_info, instance_uuid=instance_uuid)
    return net_info
