From 3eceed017bef43ef38f49b83a7f629cd1342ace5 Mon Sep 17 00:00:00 2001 From: xulei Date: Tue, 19 Jun 2018 13:22:08 +0800 Subject: [PATCH] check all_tenants value in share api add manila.utils.is_all_tenants to check all_tenants value. share_networks and security_service api will check all_tenants value in the following patches. Change-Id: I1aa9903276038dc839df45b852b329a75be5657f Partial-Bug: #1777551 Co-Authored-By: Jiao Pengju --- manila/share/api.py | 2 +- manila/tests/share/test_api.py | 31 +++++++++++++++++++++++++++++++ manila/tests/test_utils.py | 23 +++++++++++++++++++++++ manila/utils.py | 18 ++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/manila/share/api.py b/manila/share/api.py index 8b9cd558da..d811f2fcea 100644 --- a/manila/share/api.py +++ b/manila/share/api.py @@ -1518,7 +1518,7 @@ class API(base.Base): shares = self.db.share_get_all_by_share_server( context, search_opts.pop('share_server_id'), filters=filters, sort_key=sort_key, sort_dir=sort_dir) - elif (context.is_admin and 'all_tenants' in search_opts): + elif (context.is_admin and utils.is_all_tenants(search_opts)): shares = self.db.share_get_all( context, filters=filters, sort_key=sort_key, sort_dir=sort_dir) else: diff --git a/manila/tests/share/test_api.py b/manila/tests/share/test_api.py index 7d8732885e..660ea86a03 100644 --- a/manila/tests/share/test_api.py +++ b/manila/tests/share/test_api.py @@ -248,6 +248,37 @@ class ShareAPITestCase(test.TestCase): ctx, sort_dir='desc', sort_key='created_at', filters={}) self.assertEqual(_FAKE_LIST_OF_ALL_SHARES, shares) + def test_get_all_admin_filter_by_all_tenants_with_blank(self): + ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True) + self.mock_object(db_api, 'share_get_all', + mock.Mock(return_value=_FAKE_LIST_OF_ALL_SHARES)) + shares = self.api.get_all(ctx, {'all_tenants': ''}) + share_api.policy.check_policy.assert_called_once_with( + ctx, 'share', 'get_all') + db_api.share_get_all.assert_called_once_with( + ctx, sort_dir='desc', sort_key='created_at', filters={}) + self.assertEqual(_FAKE_LIST_OF_ALL_SHARES, shares) + + def test_get_all_admin_filter_by_all_tenants_with_false(self): + ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True) + self.mock_object(db_api, 'share_get_all_by_project', + mock.Mock(return_value=_FAKE_LIST_OF_ALL_SHARES[0])) + shares = self.api.get_all(ctx, {'all_tenants': 'false'}) + share_api.policy.check_policy.assert_called_once_with( + ctx, 'share', 'get_all') + db_api.share_get_all_by_project.assert_called_once_with( + ctx, sort_dir='desc', sort_key='created_at', + project_id='fake_pid_1', filters={}, is_public=False + ) + self.assertEqual(_FAKE_LIST_OF_ALL_SHARES[0], shares) + + def test_get_all_admin_filter_by_all_tenants_with_invaild_value(self): + ctx = context.RequestContext('fake_uid', 'fake_pid_1', is_admin=True) + self.mock_object(db_api, 'share_get_all') + self.assertRaises( + exception.InvalidInput, + self.api.get_all, ctx, {'all_tenants': 'wonk'}) + @ddt.data( ({'share_server_id': 'fake_share_server'}, 'list_by_share_server_id'), ({'host': 'fake_host'}, 'list_by_host'), diff --git a/manila/tests/test_utils.py b/manila/tests/test_utils.py index ca3bbc8e62..a705340831 100644 --- a/manila/tests/test_utils.py +++ b/manila/tests/test_utils.py @@ -854,3 +854,26 @@ class TestDisableNotifications(test.TestCase): group='oslo_messaging_notifications') result = self._decorated_method() self.assertEqual(utils.DO_NOTHING, result) + + +@ddt.ddt +class TestAllTenantsValueCase(test.TestCase): + @ddt.data(None, '', '1', 'true', 'True') + def test_is_all_tenants_true(self, value): + search_opts = {'all_tenants': value} + self.assertTrue(utils.is_all_tenants(search_opts)) + self.assertIn('all_tenants', search_opts) + + @ddt.data('0', 'false', 'False') + def test_is_all_tenants_false(self, value): + search_opts = {'all_tenants': value} + self.assertFalse(utils.is_all_tenants(search_opts)) + self.assertIn('all_tenants', search_opts) + + def test_is_all_tenants_missing(self): + self.assertFalse(utils.is_all_tenants({})) + + def test_is_all_tenants_invalid(self): + search_opts = {'all_tenants': 'wonk'} + self.assertRaises(exception.InvalidInput, utils.is_all_tenants, + search_opts) diff --git a/manila/utils.py b/manila/utils.py index 3387fcf2e2..a55d1b61a5 100644 --- a/manila/utils.py +++ b/manila/utils.py @@ -414,6 +414,24 @@ def is_valid_ip_address(ip_address, ip_version): return False +def is_all_tenants(search_opts): + """Checks to see if the all_tenants flag is in search_opts + + :param dict search_opts: The search options for a request + :returns: boolean indicating if all_tenants are being requested or not + """ + all_tenants = search_opts.get('all_tenants') + if all_tenants: + try: + all_tenants = strutils.bool_from_string(all_tenants, True) + except ValueError as err: + raise exception.InvalidInput(six.text_type(err)) + else: + # The empty string is considered enabling all_tenants + all_tenants = 'all_tenants' in search_opts + return all_tenants + + class IsAMatcher(object): def __init__(self, expected_value=None): self.expected_value = expected_value