]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Make "shared" filter more compatible with diff DBs
authorzhhuabj <zhhuabj@cn.ibm.com>
Mon, 1 Apr 2013 05:30:00 +0000 (13:30 +0800)
committerzhhuabj <zhhuabj@cn.ibm.com>
Wed, 10 Apr 2013 10:10:23 +0000 (18:10 +0800)
For the type BOOLEAN, in sqlalchemy,
it's mapped to BOOLEAN type if the backend database supports it,
otherwise, it's mapped to one of the Integer types, like SMALLINT,
and restrict the values to 1(True) and 0(False).

query_filter = (... | model.shared))
The above filter will generate a SQL where clause like:
where ... OR xxx.shared

This is not supported in databases which don't support BOOLEAN type.
Change it as below to make it more compatible:
query_filter = (... | model.shared == True))

It will generate a SQL where clause as below:
where ... OR xxx.shared = ?

In Python, True == 1, so this change is compatible
with both databases supporting BOOLEAN and those not supporting it.

Fix bug 1161195

Change-Id: Ic0ce0816d63b576a3469de0ed92cae4b19a3690e

quantum/db/db_base_plugin_v2.py
quantum/tests/unit/midonet/test_midonet_plugin.py
quantum/tests/unit/test_db_plugin.py

index 6182c45e2451089bd708c1fb53c2069b18265707..7c3738cbc93114426f056dff8643a12452f854ea 100644 (file)
@@ -98,7 +98,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
         if not context.is_admin and hasattr(model, 'tenant_id'):
             if hasattr(model, 'shared'):
                 query_filter = ((model.tenant_id == context.tenant_id) |
-                                (model.shared))
+                                (model.shared == True))
             else:
                 query_filter = (model.tenant_id == context.tenant_id)
         # Execute query hooks registered from mixins and plugins
index fad116e339e7e90e14922191c589a57f93ba7fcc..11bb9b0632c627f390c6d668ae5338a5a32f3902 100644 (file)
@@ -180,6 +180,9 @@ class TestMidonetNetworksV2(test_plugin.TestNetworksV2,
     def test_list_networks_with_parameters_invalid_values(self):
         pass
 
+    def test_list_shared_networks_with_non_admin_user(self):
+        pass
+
     def test_show_network_with_subnet(self):
         pass
 
index 6d18227dcfec66ce080ee46efff97e1452cebcd1..4801813b3b45f47768c7d4d3331c4817813ef486 100644 (file)
@@ -2240,6 +2240,22 @@ class TestNetworksV2(QuantumDbPluginV2TestCase):
             res = req.get_response(self.api)
             self.assertEqual(400, res.status_int)
 
+    def test_list_shared_networks_with_non_admin_user(self):
+        with contextlib.nested(self.network(shared=False,
+                                            name='net1',
+                                            tenant_id='tenant1'),
+                               self.network(shared=True,
+                                            name='net2',
+                                            tenant_id='another_tenant'),
+                               self.network(shared=False,
+                                            name='net3',
+                                            tenant_id='another_tenant')
+                               ) as (net1, net2, net3):
+            ctx = context.Context(user_id='non_admin',
+                                  tenant_id='tenant1',
+                                  is_admin=False)
+            self._test_list_resources('network', (net1, net2), ctx)
+
     def test_show_network(self):
         with self.network(name='net1') as net:
             req = self.new_show_request('networks', net['network']['id'])