]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add a non-mixin function for model queries
authorSalvatore Orlando <salv.orlando@gmail.com>
Tue, 19 May 2015 16:27:14 +0000 (09:27 -0700)
committerSalvatore Orlando <salv.orlando@gmail.com>
Mon, 1 Jun 2015 18:22:29 +0000 (11:22 -0700)
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

index 143c8f0e416298e00ca879a09a30d9f9fa7030d3..edd4039a1ac9820af4793e695cfec57601f97c01 100644 (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)