'Detach a network adapter from an instance.'

@wrap_exception()
@wrap_instance_fault
def detach_interface(self, context, instance, port_id):

    network_info = instance.info_cache.network_info
    condemned = None
    for vif in network_info:
        if (vif['id'] == port_id):
            condemned = vif
            break
    if (condemned is None):
        raise exception.PortNotFound((_('Port    %s    is    not    attached') % port_id))
    try:
        self.driver.detach_interface(context, instance, condemned)
    except exception.NovaException as ex:
        LOG.warning('Detach    interface    failed,    port_id=%(port_id)s,    reason:    %(msg)s', {'port_id': port_id, 'msg': ex}, instance=instance)
        raise exception.InterfaceDetachFailed(instance_uuid=instance.uuid)
    else:
        try:
            self.network_api.deallocate_port_for_instance(context, instance, port_id)
        except Exception as ex:
            with excutils.save_and_reraise_exception():
                LOG.warning('Failed    to    deallocate    port    %(port_id)s    for    instance.    Error:    %(error)s', {'port_id': port_id, 'error': ex}, instance=instance)
