From 70e07629bf263370102350d922d62970c60d5aa4 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Tue, 19 May 2015 09:27:14 -0700 Subject: [PATCH] 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 --- neutron/db/common_db_mixin.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/neutron/db/common_db_mixin.py b/neutron/db/common_db_mixin.py index 143c8f0e416..edd4039a1ac 100644 --- a/neutron/db/common_db_mixin.py +++ b/neutron/db/common_db_mixin.py @@ -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)