'Updates db to show volume is detached.'

@coordination.synchronized('{volume_id}-{f_name}')
def detach_volume(self, context, volume_id, attachment_id=None, volume=None):

    if (volume is None):
        volume = objects.Volume.get_by_id(context, volume_id)
    if attachment_id:
        try:
            attachment = objects.VolumeAttachment.get_by_id(context, attachment_id)
        except exception.VolumeAttachmentNotFound:
            LOG.info('Volume    detach    called,    but    volume    not    attached.', resource=volume)
            volume.finish_detach(attachment_id)
            return
    else:
        attachments = volume.volume_attachment
        if (len(attachments) > 1):
            msg = _('Detach    volume    failed:    More    than    one    attachment,    but    no    attachment_id    provided.')
            LOG.error(msg, resource=volume)
            raise exception.InvalidVolume(reason=msg)
        elif (len(attachments) == 1):
            attachment = attachments[0]
        else:
            LOG.info('Volume    detach    called,    but    volume    not    attached.', resource=volume)
            volume.status = 'available'
            volume.attach_status = fields.VolumeAttachStatus.DETACHED
            volume.save()
            return
    self._notify_about_volume_usage(context, volume, 'detach.start')
    try:
        utils.require_driver_initialized(self.driver)
        LOG.info('Detaching    volume    %(volume_id)s    from    instance    %(instance)s.', {'volume_id': volume_id, 'instance': attachment.get('instance_uuid')}, resource=volume)
        self.driver.detach_volume(context, volume, attachment)
    except Exception:
        with excutils.save_and_reraise_exception():
            self.db.volume_attachment_update(context, attachment.get('id'), {'attach_status': fields.VolumeAttachStatus.ERROR_DETACHING})
    try:
        utils.require_driver_initialized(self.driver)
        self.driver.remove_export(context.elevated(), volume)
    except exception.DriverNotInitialized:
        with excutils.save_and_reraise_exception():
            LOG.exception('Detach    volume    failed,    due    to    uninitialized    driver.', resource=volume)
    except Exception as ex:
        LOG.exception('Detach    volume    failed,    due    to    remove-export    failure.', resource=volume)
        raise exception.RemoveExportException(volume=volume_id, reason=six.text_type(ex))
    volume.finish_detach(attachment.id)
    self._notify_about_volume_usage(context, volume, 'detach.end')
    LOG.info('Detach    volume    completed    successfully.', resource=volume)
