Remove code migrated to osc-lib long ago
* Remove openstackclient.api.utils and use osc_lib.api.utils * Remove openstackclient.common.clientmanager.ClientManager.auth_ref * Remove openstackclient.common.commandmanager Change-Id: I67e1dbc53cc0b37967c0011bcb2fc09bdef62d94 Signed-off-by: Dean Troyer <dtroyer@gmail.com>
This commit is contained in:
parent
4b91cd4965
commit
1b2595a959
@ -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
|
|
@ -91,13 +91,6 @@ class ClientManager(clientmanager.ClientManager):
|
|||||||
|
|
||||||
return super(ClientManager, self).setup_auth()
|
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):
|
def _fallback_load_auth_plugin(self, e):
|
||||||
# NOTES(RuiChen): Hack to avoid auth plugins choking on data they don't
|
# NOTES(RuiChen): Hack to avoid auth plugins choking on data they don't
|
||||||
# expect, delete fake token and endpoint, then try to
|
# expect, delete fake token and endpoint, then try to
|
||||||
|
@ -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())
|
|
@ -22,12 +22,12 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from glanceclient.common import utils as gc_utils
|
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.cli import parseractions
|
||||||
from osc_lib.command import command
|
from osc_lib.command import command
|
||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from openstackclient.api import utils as api_utils
|
|
||||||
from openstackclient.i18n import _
|
from openstackclient.i18n import _
|
||||||
|
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
|
@ -21,13 +21,13 @@ import logging
|
|||||||
|
|
||||||
from glanceclient.common import utils as gc_utils
|
from glanceclient.common import utils as gc_utils
|
||||||
from openstack.image import image_signer
|
from openstack.image import image_signer
|
||||||
|
from osc_lib.api import utils as api_utils
|
||||||
from osc_lib.cli import parseractions
|
from osc_lib.cli import parseractions
|
||||||
from osc_lib.command import command
|
from osc_lib.command import command
|
||||||
from osc_lib import exceptions
|
from osc_lib import exceptions
|
||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from openstackclient.api import utils as api_utils
|
|
||||||
from openstackclient.i18n import _
|
from openstackclient.i18n import _
|
||||||
from openstackclient.identity import common
|
from openstackclient.identity import common
|
||||||
|
|
||||||
|
@ -20,13 +20,13 @@ import locale
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from osc_lib.api import auth
|
from osc_lib.api import auth
|
||||||
|
from osc_lib.command import commandmanager
|
||||||
from osc_lib import shell
|
from osc_lib import shell
|
||||||
import six
|
import six
|
||||||
|
|
||||||
import openstackclient
|
import openstackclient
|
||||||
from openstackclient.common import client_config as cloud_config
|
from openstackclient.common import client_config as cloud_config
|
||||||
from openstackclient.common import clientmanager
|
from openstackclient.common import clientmanager
|
||||||
from openstackclient.common import commandmanager
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_DOMAIN = 'default'
|
DEFAULT_DOMAIN = 'default'
|
||||||
|
@ -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)
|
|
@ -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)
|
|
@ -417,7 +417,7 @@ class TestImageList(TestImage):
|
|||||||
), )
|
), )
|
||||||
self.assertEqual(datalist, tuple(data))
|
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):
|
def test_image_list_property_option(self, sf_mock):
|
||||||
sf_mock.side_effect = [
|
sf_mock.side_effect = [
|
||||||
[self.image_info], [],
|
[self.image_info], [],
|
||||||
|
@ -734,7 +734,7 @@ class TestImageList(TestImage):
|
|||||||
), )
|
), )
|
||||||
self.assertEqual(datalist, tuple(data))
|
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):
|
def test_image_list_property_option(self, sf_mock):
|
||||||
sf_mock.return_value = [copy.deepcopy(self._image)]
|
sf_mock.return_value = [copy.deepcopy(self._image)]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user