Make "shared" filter more compatible with diff DBs

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
This commit is contained in:
zhhuabj 2013-04-01 13:30:00 +08:00
parent 8ad5612317
commit de5c1e4f28
3 changed files with 20 additions and 1 deletions

View 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

View 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

View 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'])