def delete_cgsnapshot(self, context, cgsnapshot):
    'Deletes cgsnapshot.'
    caller_context = context
    context = context.elevated()
    project_id = cgsnapshot.project_id
    LOG.info(_LI('cgsnapshot %s: deleting'), cgsnapshot.id)
    snapshots = objects.SnapshotList.get_all_for_cgsnapshot(context, cgsnapshot.id)
    self._notify_about_cgsnapshot_usage(context, cgsnapshot, 'delete.start')
    snapshots_model_update = None
    model_update = None
    try:
        utils.require_driver_initialized(self.driver)
        LOG.debug('cgsnapshot %(cgsnap_id)s: deleting', {'cgsnap_id': cgsnapshot.id})
        cgsnapshot.context = caller_context
        for snapshot in snapshots:
            snapshot.context = caller_context
        (model_update, snapshots_model_update) = self.driver.delete_cgsnapshot(context, cgsnapshot, snapshots)
        if snapshots_model_update:
            for snap_model in snapshots_model_update:
                snap = next((item for item in snapshots if (item.id == snap_model['id'])), None)
                if snap:
                    snap.status = snap_model['status']
                    snap.save()
                if ((snap_model['status'] in [fields.SnapshotStatus.ERROR_DELETING, fields.SnapshotStatus.ERROR]) and (model_update['status'] not in ['error_deleting', 'error'])):
                    model_update['status'] = snap_model['status']
        if model_update:
            if (model_update['status'] in ['error_deleting', 'error']):
                msg = (_('Error occurred when deleting cgsnapshot %s.') % cgsnapshot.id)
                LOG.error(msg)
                raise exception.VolumeDriverException(message=msg)
            else:
                cgsnapshot.update(model_update)
                cgsnapshot.save()
    except exception.CinderException:
        with excutils.save_and_reraise_exception():
            cgsnapshot.status = 'error'
            cgsnapshot.save()
            if (not snapshots_model_update):
                for snapshot in snapshots:
                    snapshot.status = fields.SnapshotStatus.ERROR
                    snapshot.save()
    for snapshot in snapshots:
        try:
            if CONF.no_snapshot_gb_quota:
                reserve_opts = {'snapshots': (- 1)}
            else:
                reserve_opts = {'snapshots': (- 1), 'gigabytes': (- snapshot['volume_size'])}
            volume_ref = self.db.volume_get(context, snapshot['volume_id'])
            QUOTAS.add_volume_type_opts(context, reserve_opts, volume_ref.get('volume_type_id'))
            reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts)
        except Exception:
            reservations = None
            LOG.exception(_LE('Failed to update usages deleting snapshot'))
        self.db.volume_glance_metadata_delete_by_snapshot(context, snapshot['id'])
        self.db.snapshot_destroy(context, snapshot['id'])
        if reservations:
            QUOTAS.commit(context, reservations, project_id=project_id)
    cgsnapshot.destroy()
    LOG.info(_LI('cgsnapshot %s: deleted successfully'), cgsnapshot.id)
    self._notify_about_cgsnapshot_usage(context, cgsnapshot, 'delete.end', snapshots)
Deletes cgsnapshot.
def delete_cgsnapshot(self, context, cgsnapshot):
    
    caller_context = context
    context = context.elevated()
    project_id = cgsnapshot.project_id
    LOG.info(_LI('cgsnapshot %s: deleting'), cgsnapshot.id)
    snapshots = objects.SnapshotList.get_all_for_cgsnapshot(context, cgsnapshot.id)
    self._notify_about_cgsnapshot_usage(context, cgsnapshot, 'delete.start')
    snapshots_model_update = None
    model_update = None
    try:
        utils.require_driver_initialized(self.driver)
        LOG.debug('cgsnapshot %(cgsnap_id)s: deleting', {'cgsnap_id': cgsnapshot.id})
        cgsnapshot.context = caller_context
        for snapshot in snapshots:
            snapshot.context = caller_context
        (model_update, snapshots_model_update) = self.driver.delete_cgsnapshot(context, cgsnapshot, snapshots)
        if snapshots_model_update:
            for snap_model in snapshots_model_update:
                snap = next((item for item in snapshots if (item.id == snap_model['id'])), None)
                if snap:
                    snap.status = snap_model['status']
                    snap.save()
                if ((snap_model['status'] in [fields.SnapshotStatus.ERROR_DELETING, fields.SnapshotStatus.ERROR]) and (model_update['status'] not in ['error_deleting', 'error'])):
                    model_update['status'] = snap_model['status']
        if model_update:
            if (model_update['status'] in ['error_deleting', 'error']):
                msg = (_('Error occurred when deleting cgsnapshot %s.') % cgsnapshot.id)
                LOG.error(msg)
                raise exception.VolumeDriverException(message=msg)
            else:
                cgsnapshot.update(model_update)
                cgsnapshot.save()
    except exception.CinderException:
        with excutils.save_and_reraise_exception():
            cgsnapshot.status = 'error'
            cgsnapshot.save()
            if (not snapshots_model_update):
                for snapshot in snapshots:
                    snapshot.status = fields.SnapshotStatus.ERROR
                    snapshot.save()
    for snapshot in snapshots:
        try:
            if CONF.no_snapshot_gb_quota:
                reserve_opts = {'snapshots': (- 1)}
            else:
                reserve_opts = {'snapshots': (- 1), 'gigabytes': (- snapshot['volume_size'])}
            volume_ref = self.db.volume_get(context, snapshot['volume_id'])
            QUOTAS.add_volume_type_opts(context, reserve_opts, volume_ref.get('volume_type_id'))
            reservations = QUOTAS.reserve(context, project_id=project_id, **reserve_opts)
        except Exception:
            reservations = None
            LOG.exception(_LE('Failed to update usages deleting snapshot'))
        self.db.volume_glance_metadata_delete_by_snapshot(context, snapshot['id'])
        self.db.snapshot_destroy(context, snapshot['id'])
        if reservations:
            QUOTAS.commit(context, reservations, project_id=project_id)
    cgsnapshot.destroy()
    LOG.info(_LI('cgsnapshot %s: deleted successfully'), cgsnapshot.id)
    self._notify_about_cgsnapshot_usage(context, cgsnapshot, 'delete.end', snapshots)