def model_query(context, *args, **kwargs): """Query helper that accounts for context's `read_deleted` field. :param context: context to query under :param session: if present, the session to use :param read_deleted: if present, overrides context's read_deleted field. :param project_only: if present and context is user-type, then restrict query to match the context's project_id. """ session = kwargs.get('session') or get_session() read_deleted = kwargs.get('read_deleted') or context.read_deleted project_only = kwargs.get('project_only') query = session.query(*args) if read_deleted == 'no': query = query.filter_by(deleted=False) elif read_deleted == 'yes': pass # omit the filter to include deleted and active elif read_deleted == 'only': query = query.filter_by(deleted=True) else: raise Exception("Unrecognized read_deleted value '%s'" % read_deleted) if project_only and is_user_context(context): query = query.filter_by(project_id=context.project_id) return query
def rbd_cache_config_delete_by_rbd_id(context, rbd_id): session = get_session() with session.begin(): session.query(models.RbdCacheConfig).\ filter_by(rbd_id=rbd_id).\ update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')})
def hs_instance_delete(context, hs_instance_id): session = get_session() with session.begin(): session.query(models.HsInstance).\ filter_by(id=hs_instance_id).\ update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')})
def rbd_cache_config_update(context, rbd_cache_config_id, values): session = get_session() with session.begin(): rbd_cache_config_ref = rbd_cache_config_get(context, rbd_cache_config_id, session=session) rbd_cache_config_ref.update(values) rbd_cache_config_ref.save(session=session) return rbd_cache_config_ref
def save(self, session=None): """Save this object.""" if not session: session = get_session() session.add(self) try: session.flush() except IntegrityError, e: if str(e).endswith('is not unique'): raise exception.Duplicate(str(e)) else: raise
def performance_metric_clean_up_data(context, seconds): session = get_session() result = model_query(context, models.PerformanceMetric, session=session).\ all() if not result: return last = result[-1] LOG.info("The latest performance metric data is %s" % str(last)) timestamp = last.timestamp LOG.info("The timestamp of latest performance metric data is %s" % str(timestamp)) min_timestamp = int(timestamp) - int(seconds) LOG.info("min_timestamp: %s" % str(min_timestamp)) sql_str = "UPDATE performance_metrics SET deleted=1 WHERE timestamp < %s;" % min_timestamp session.execute(sql_str)
def service_destroy(context, service_id): session = get_session() with session.begin(): service_ref = service_get(context, service_id, session=session) service_ref.delete(session=session)
def rbd_cache_config_get_all(context): session = get_session() with session.begin(): query = model_query(context, models.RbdCacheConfig) return query.all()
def rbd_update(context, rbd_id, values): session = get_session() with session.begin(): rbd_ref = rbd_get(context, rbd_id, session=session) rbd_ref.update(values) rbd_ref.save(session=session)
def rbd_get_all(context): session = get_session() with session.begin(): query = model_query(context, models.Rbd).\ options(joinedload('hs_instance')) return query.all()
def hs_instance_get_all(context): session = get_session() with session.begin(): query = model_query(context, models.HsInstance).\ options(joinedload('server')) return query.all()
def server_get_all(context): session = get_session() with session.begin(): query = model_query(context, models.Server) return query.all()
def server_update(context, server_id, values): session = get_session() with session.begin(): server_ref = server_get(context, server_id, session=session) server_ref.update(values) server_ref.save(session=session)