diff --git a/openstackclient/api/utils.py b/openstackclient/api/utils.py deleted file mode 100644 index 6407cd4457..0000000000 --- a/openstackclient/api/utils.py +++ /dev/null @@ -1,84 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# - -"""API Utilities Library""" - - -def simple_filter( - data=None, - attr=None, - value=None, - property_field=None, -): - """Filter a list of dicts - - :param list data: - The list to be filtered. The list is modified in-place and will - be changed if any filtering occurs. - :param string attr: - The name of the attribute to filter. If attr does not exist no - match will succeed and no rows will be returned. If attr is - None no filtering will be performed and all rows will be returned. - :param string value: - The value to filter. None is considered to be a 'no filter' value. - '' matches against a Python empty string. - :param string property_field: - The name of the data field containing a property dict to filter. - If property_field is None, attr is a field name. If property_field - is not None, attr is a property key name inside the named property - field. - - :returns: - Returns the filtered list - :rtype list: - - This simple filter (one attribute, one exact-match value) searches a - list of dicts to select items. It first searches the item dict for a - matching ``attr`` then does an exact-match on the ``value``. If - ``property_field`` is given, it will look inside that field (if it - exists and is a dict) for a matching ``value``. - """ - - # Take the do-nothing case shortcut - if not data or not attr or value is None: - return data - - # NOTE:(dtroyer): This filter modifies the provided list in-place using - # list.remove() so we need to start at the end so the loop pointer does - # not skip any items after a deletion. - for d in reversed(data): - if attr in d: - # Searching data fields - search_value = d[attr] - elif (property_field and property_field in d and - isinstance(d[property_field], dict)): - # Searching a properties field - do this separately because - # we don't want to fail over to checking the fields if a - # property name is given. - if attr in d[property_field]: - search_value = d[property_field][attr] - else: - search_value = None - else: - search_value = None - - # could do regex here someday... - if not search_value or search_value != value: - # remove from list - try: - data.remove(d) - except ValueError: - # it's already gone! - pass - - return data diff --git a/openstackclient/common/clientmanager.py b/openstackclient/common/clientmanager.py index aa1045e469..c1118ad3ef 100644 --- a/openstackclient/common/clientmanager.py +++ b/openstackclient/common/clientmanager.py @@ -91,13 +91,6 @@ class ClientManager(clientmanager.ClientManager): return super(ClientManager, self).setup_auth() - @property - def auth_ref(self): - if not self._auth_required: - return None - else: - return super(ClientManager, self).auth_ref - def _fallback_load_auth_plugin(self, e): # NOTES(RuiChen): Hack to avoid auth plugins choking on data they don't # expect, delete fake token and endpoint, then try to diff --git a/openstackclient/common/commandmanager.py b/openstackclient/common/commandmanager.py deleted file mode 100644 index c190e33e0d..0000000000 --- a/openstackclient/common/commandmanager.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2012-2013 OpenStack Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# - -"""Modify cliff.CommandManager""" - -import pkg_resources - -import cliff.commandmanager - - -class CommandManager(cliff.commandmanager.CommandManager): - """Add additional functionality to cliff.CommandManager - - Load additional command groups after initialization - Add _command_group() methods - """ - - def __init__(self, namespace, convert_underscores=True): - self.group_list = [] - super(CommandManager, self).__init__(namespace, convert_underscores) - - def load_commands(self, namespace): - self.group_list.append(namespace) - return super(CommandManager, self).load_commands(namespace) - - def add_command_group(self, group=None): - """Adds another group of command entrypoints""" - if group: - self.load_commands(group) - - def get_command_groups(self): - """Returns a list of the loaded command groups""" - return self.group_list - - def get_command_names(self, group=None): - """Returns a list of commands loaded for the specified group""" - group_list = [] - if group is not None: - for ep in pkg_resources.iter_entry_points(group): - cmd_name = ( - ep.name.replace('_', ' ') - if self.convert_underscores - else ep.name - ) - group_list.append(cmd_name) - return group_list - return list(self.commands.keys()) diff --git a/openstackclient/image/v1/image.py b/openstackclient/image/v1/image.py index 7ecaa3efed..64c4049cfd 100644 --- a/openstackclient/image/v1/image.py +++ b/openstackclient/image/v1/image.py @@ -22,12 +22,12 @@ import os import sys from glanceclient.common import utils as gc_utils +from osc_lib.api import utils as api_utils from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import utils import six -from openstackclient.api import utils as api_utils from openstackclient.i18n import _ if os.name == "nt": diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 3efde80879..bdec99d7be 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -21,13 +21,13 @@ import logging from glanceclient.common import utils as gc_utils from openstack.image import image_signer +from osc_lib.api import utils as api_utils from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils import six -from openstackclient.api import utils as api_utils from openstackclient.i18n import _ from openstackclient.identity import common diff --git a/openstackclient/shell.py b/openstackclient/shell.py index 58a7712026..22d8412cc7 100644 --- a/openstackclient/shell.py +++ b/openstackclient/shell.py @@ -20,13 +20,13 @@ import locale import sys from osc_lib.api import auth +from osc_lib.command import commandmanager from osc_lib import shell import six import openstackclient from openstackclient.common import client_config as cloud_config from openstackclient.common import clientmanager -from openstackclient.common import commandmanager DEFAULT_DOMAIN = 'default' diff --git a/openstackclient/tests/unit/api/test_utils.py b/openstackclient/tests/unit/api/test_utils.py deleted file mode 100644 index 1f52855864..0000000000 --- a/openstackclient/tests/unit/api/test_utils.py +++ /dev/null @@ -1,115 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# - -"""API Utilities Library Tests""" - -import copy - -from openstackclient.api import api -from openstackclient.api import utils as api_utils -from openstackclient.tests.unit.api import fakes as api_fakes - - -class TestBaseAPIFilter(api_fakes.TestSession): - """The filters can be tested independently""" - - def setUp(self): - super(TestBaseAPIFilter, self).setUp() - self.api = api.BaseAPI( - session=self.sess, - endpoint=self.BASE_URL, - ) - - self.input_list = [ - api_fakes.RESP_ITEM_1, - api_fakes.RESP_ITEM_2, - api_fakes.RESP_ITEM_3, - ] - - def test_simple_filter_none(self): - output = api_utils.simple_filter( - ) - self.assertIsNone(output) - - def test_simple_filter_no_attr(self): - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - ) - self.assertEqual(self.input_list, output) - - def test_simple_filter_attr_only(self): - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='status', - ) - self.assertEqual(self.input_list, output) - - def test_simple_filter_attr_value(self): - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='status', - value='', - ) - self.assertEqual([], output) - - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='status', - value='UP', - ) - self.assertEqual( - [api_fakes.RESP_ITEM_1, api_fakes.RESP_ITEM_3], - output, - ) - - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='fred', - value='UP', - ) - self.assertEqual([], output) - - def test_simple_filter_prop_attr_only(self): - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='b', - property_field='props', - ) - self.assertEqual(self.input_list, output) - - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='status', - property_field='props', - ) - self.assertEqual(self.input_list, output) - - def test_simple_filter_prop_attr_value(self): - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='b', - value=2, - property_field='props', - ) - self.assertEqual( - [api_fakes.RESP_ITEM_1, api_fakes.RESP_ITEM_2], - output, - ) - - output = api_utils.simple_filter( - copy.deepcopy(self.input_list), - attr='b', - value=9, - property_field='props', - ) - self.assertEqual([], output) diff --git a/openstackclient/tests/unit/common/test_commandmanager.py b/openstackclient/tests/unit/common/test_commandmanager.py deleted file mode 100644 index 0c6c99c05d..0000000000 --- a/openstackclient/tests/unit/common/test_commandmanager.py +++ /dev/null @@ -1,107 +0,0 @@ -# Copyright 2012-2013 OpenStack Foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# - -import mock - -from openstackclient.common import commandmanager -from openstackclient.tests.unit import utils - - -class FakeCommand(object): - - @classmethod - def load(cls): - return cls - - def __init__(self): - return - -FAKE_CMD_ONE = FakeCommand -FAKE_CMD_TWO = FakeCommand -FAKE_CMD_ALPHA = FakeCommand -FAKE_CMD_BETA = FakeCommand - - -class FakeCommandManager(commandmanager.CommandManager): - commands = {} - - def load_commands(self, namespace): - if namespace == 'test': - self.commands['one'] = FAKE_CMD_ONE - self.commands['two'] = FAKE_CMD_TWO - self.group_list.append(namespace) - elif namespace == 'greek': - self.commands['alpha'] = FAKE_CMD_ALPHA - self.commands['beta'] = FAKE_CMD_BETA - self.group_list.append(namespace) - - -class TestCommandManager(utils.TestCase): - - def test_add_command_group(self): - mgr = FakeCommandManager('test') - - # Make sure add_command() still functions - mock_cmd_one = mock.Mock() - mgr.add_command('mock', mock_cmd_one) - cmd_mock, name, args = mgr.find_command(['mock']) - self.assertEqual(mock_cmd_one, cmd_mock) - - # Find a command added in initialization - cmd_one, name, args = mgr.find_command(['one']) - self.assertEqual(FAKE_CMD_ONE, cmd_one) - - # Load another command group - mgr.add_command_group('greek') - - # Find a new command - cmd_alpha, name, args = mgr.find_command(['alpha']) - self.assertEqual(FAKE_CMD_ALPHA, cmd_alpha) - - # Ensure that the original commands were not overwritten - cmd_two, name, args = mgr.find_command(['two']) - self.assertEqual(FAKE_CMD_TWO, cmd_two) - - def test_get_command_groups(self): - mgr = FakeCommandManager('test') - - # Make sure add_command() still functions - mock_cmd_one = mock.Mock() - mgr.add_command('mock', mock_cmd_one) - cmd_mock, name, args = mgr.find_command(['mock']) - self.assertEqual(mock_cmd_one, cmd_mock) - - # Load another command group - mgr.add_command_group('greek') - - gl = mgr.get_command_groups() - self.assertEqual(['test', 'greek'], gl) - - def test_get_command_names(self): - mock_cmd_one = mock.Mock() - mock_cmd_one.name = 'one' - mock_cmd_two = mock.Mock() - mock_cmd_two.name = 'cmd two' - mock_pkg_resources = mock.Mock( - return_value=[mock_cmd_one, mock_cmd_two], - ) - with mock.patch( - 'pkg_resources.iter_entry_points', - mock_pkg_resources, - ) as iter_entry_points: - mgr = commandmanager.CommandManager('test') - iter_entry_points.assert_called_once_with('test') - cmds = mgr.get_command_names('test') - self.assertEqual(['one', 'cmd two'], cmds) diff --git a/openstackclient/tests/unit/image/v1/test_image.py b/openstackclient/tests/unit/image/v1/test_image.py index ae578d9138..6babd4ffca 100644 --- a/openstackclient/tests/unit/image/v1/test_image.py +++ b/openstackclient/tests/unit/image/v1/test_image.py @@ -417,7 +417,7 @@ class TestImageList(TestImage): ), ) self.assertEqual(datalist, tuple(data)) - @mock.patch('openstackclient.api.utils.simple_filter') + @mock.patch('osc_lib.api.utils.simple_filter') def test_image_list_property_option(self, sf_mock): sf_mock.side_effect = [ [self.image_info], [], diff --git a/openstackclient/tests/unit/image/v2/test_image.py b/openstackclient/tests/unit/image/v2/test_image.py index 16a393df7d..4cc8f44827 100644 --- a/openstackclient/tests/unit/image/v2/test_image.py +++ b/openstackclient/tests/unit/image/v2/test_image.py @@ -734,7 +734,7 @@ class TestImageList(TestImage): ), ) self.assertEqual(datalist, tuple(data)) - @mock.patch('openstackclient.api.utils.simple_filter') + @mock.patch('osc_lib.api.utils.simple_filter') def test_image_list_property_option(self, sf_mock): sf_mock.return_value = [copy.deepcopy(self._image)]