Add a non-mixin function for model queries

This patch simply adds a version of model_query in
neutron.db.common_db_mixin which can be invoked without
having to declare a class which inherits the mixin.

To this aim, model_query_scope has been refactored as well.
As the model query function being introduced in this patch
cannot use model query hooks (and does not need to), the
method was re-implemented rather than bringing out of the
mixin as it has been done for model_query_scope.

This change will allow for developing DB APIs without
having to use the baseDB/mixin classes models used so far.

Related-Blueprint: better-quotas

Change-Id: I7a79980f626e9eaf2775711c8a25f508067e5716
This commit is contained in:
Salvatore Orlando 2015-05-19 09:27:14 -07:00
parent 5eddb2d274
commit 70e07629bf

View File

@ -22,6 +22,25 @@ from neutron.common import exceptions as n_exc
from neutron.db import sqlalchemyutils
def model_query_scope(context, model):
# Unless a context has 'admin' or 'advanced-service' rights the
# query will be scoped to a single tenant_id
return ((not context.is_admin and hasattr(model, 'tenant_id')) and
(not context.is_advsvc and hasattr(model, 'tenant_id')))
def model_query(context, model):
query = context.session.query(model)
# define basic filter condition for model query
query_filter = None
if model_query_scope(context, model):
query_filter = (model.tenant_id == context.tenant_id)
if query_filter is not None:
query = query.filter(query_filter)
return query
class CommonDbMixin(object):
"""Common methods used in core and service plugins."""
# Plugins, mixin classes implementing extension will register
@ -72,11 +91,7 @@ class CommonDbMixin(object):
return weakref.proxy(self)
def model_query_scope(self, context, model):
# NOTE(jkoelker) non-admin queries are scoped to their tenant_id
# NOTE(salvatore-orlando): unless the model allows for shared objects
# NOTE(mestery): Or the user has the advsvc role
return ((not context.is_admin and hasattr(model, 'tenant_id')) and
(not context.is_advsvc and hasattr(model, 'tenant_id')))
return model_query_scope(context, model)
def _model_query(self, context, model):
query = context.session.query(model)