From 681d6dc2de83ef13b4fb2fb4abe70f3c1ccb0e10 Mon Sep 17 00:00:00 2001 From: Huanxuan Ao Date: Mon, 2 May 2016 16:30:29 +0800 Subject: [PATCH] Make "flavor show" command to show a private flavor properly The "flavor show" command could not show a private flavor by flavor name becauce it could not find a private flavor by flavor name. In "until.find_resource(parsed_args.flavor)", If parsed_args.falvor is a name of a flavor, "flavors.find(name=parsed_args.flavor)"will be called to find a flavor.But the default value of "is_public" is "Ture" in "flavors.find()" so that we can only find public flavors.If we want to find all flaovrs by flavor name,we should add "is_public=None" in "flavors.find()". So I tried to change "until.find_resource(parsed_args.flavor)" to "until.find_resource(parsed_args.flavor, is_public=None)", but then I could not find any flavor by flavor id because "is_public" is an unexpected argument of "flavors.get()" in "until.find_resource()". In this case,I think "until.find_resource()" can not find a private flavor properly,and we should combine "manager.get(flavor.id)" and "manager.find(name=flavor.name, is_public=None)" by ourselve to find a flavor. Also,this bug affects other flavor commands like "flavor set/unset/delete",so I fix them in this patch too. Change-Id: I4a4ed7b0a2f522ee04d1c3270afcda7064285c39 Closes-Bug: #1575478 --- openstackclient/compute/v2/flavor.py | 31 ++++++++++++++----- .../tests/compute/v2/test_flavor.py | 22 ++++++------- .../notes/bug-1575478-5a0a923c3a32f96a.yaml | 5 +++ 3 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 releasenotes/notes/bug-1575478-5a0a923c3a32f96a.yaml diff --git a/openstackclient/compute/v2/flavor.py b/openstackclient/compute/v2/flavor.py index 04674614b5..4b918369f2 100644 --- a/openstackclient/compute/v2/flavor.py +++ b/openstackclient/compute/v2/flavor.py @@ -18,10 +18,29 @@ import six from openstackclient.common import command +from openstackclient.common import exceptions from openstackclient.common import parseractions from openstackclient.common import utils +def _find_flavor(compute_client, flavor): + try: + return compute_client.flavors.get(flavor) + except Exception as ex: + if type(ex).__name__ == 'NotFound': + pass + else: + raise + try: + return compute_client.flavors.find(name=flavor, is_public=None) + except Exception as ex: + if type(ex).__name__ == 'NotFound': + msg = "No flavor with a name or ID of '%s' exists." % flavor + raise exceptions.CommandError(msg) + else: + raise + + class CreateFlavor(command.ShowOne): """Create new flavor""" @@ -132,8 +151,7 @@ class DeleteFlavor(command.Command): def take_action(self, parsed_args): compute_client = self.app.client_manager.compute - flavor = utils.find_resource(compute_client.flavors, - parsed_args.flavor) + flavor = _find_flavor(compute_client, parsed_args.flavor) compute_client.flavors.delete(flavor.id) @@ -239,8 +257,7 @@ class SetFlavor(command.Command): def take_action(self, parsed_args): compute_client = self.app.client_manager.compute - flavor = utils.find_resource(compute_client.flavors, - parsed_args.flavor) + flavor = _find_flavor(compute_client, parsed_args.flavor) flavor.set_keys(parsed_args.property) @@ -258,8 +275,7 @@ class ShowFlavor(command.ShowOne): def take_action(self, parsed_args): compute_client = self.app.client_manager.compute - resource_flavor = utils.find_resource(compute_client.flavors, - parsed_args.flavor) + resource_flavor = _find_flavor(compute_client, parsed_args.flavor) flavor = resource_flavor._info.copy() flavor.pop("links", None) @@ -290,6 +306,5 @@ class UnsetFlavor(command.Command): def take_action(self, parsed_args): compute_client = self.app.client_manager.compute - flavor = utils.find_resource(compute_client.flavors, - parsed_args.flavor) + flavor = _find_flavor(compute_client, parsed_args.flavor) flavor.unset_keys(parsed_args.property) diff --git a/openstackclient/tests/compute/v2/test_flavor.py b/openstackclient/tests/compute/v2/test_flavor.py index fa29111b34..529a3c25b3 100644 --- a/openstackclient/tests/compute/v2/test_flavor.py +++ b/openstackclient/tests/compute/v2/test_flavor.py @@ -273,7 +273,7 @@ class TestFlavorSet(TestFlavor): super(TestFlavorSet, self).setUp() self.flavors_mock.find.return_value = self.flavor - + self.flavors_mock.get.side_effect = exceptions.NotFound(None) self.cmd = flavor.SetFlavor(self.app, None) def test_flavor_set(self): @@ -288,10 +288,8 @@ class TestFlavorSet(TestFlavor): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - try: - self.flavors_mock.find.assert_called_with(name=parsed_args.flavor) - except Exception: - self.flavors_mock.get.assert_called_with(parsed_args.flavor) + self.flavors_mock.find.assert_called_with(name=parsed_args.flavor, + is_public=None) self.assertIsNone(result) @@ -331,9 +329,9 @@ class TestFlavorShow(TestFlavor): def setUp(self): super(TestFlavorShow, self).setUp() - # Return value of utils.find_resource() - self.flavors_mock.get.return_value = self.flavor - + # Return value of _find_resource() + self.flavors_mock.find.return_value = self.flavor + self.flavors_mock.get.side_effect = exceptions.NotFound(None) self.cmd = flavor.ShowFlavor(self.app, None) def test_show_no_options(self): @@ -369,7 +367,7 @@ class TestFlavorUnset(TestFlavor): super(TestFlavorUnset, self).setUp() self.flavors_mock.find.return_value = self.flavor - + self.flavors_mock.get.side_effect = exceptions.NotFound(None) self.cmd = flavor.UnsetFlavor(self.app, None) def test_flavor_unset(self): @@ -384,8 +382,6 @@ class TestFlavorUnset(TestFlavor): parsed_args = self.check_parser(self.cmd, arglist, verifylist) result = self.cmd.take_action(parsed_args) - try: - self.flavors_mock.find.assert_called_with(name=parsed_args.flavor) - except Exception: - self.flavors_mock.get.assert_called_with(parsed_args.flavor) + self.flavors_mock.find.assert_called_with(name=parsed_args.flavor, + is_public=None) self.assertIsNone(result) diff --git a/releasenotes/notes/bug-1575478-5a0a923c3a32f96a.yaml b/releasenotes/notes/bug-1575478-5a0a923c3a32f96a.yaml new file mode 100644 index 0000000000..b043a0e751 --- /dev/null +++ b/releasenotes/notes/bug-1575478-5a0a923c3a32f96a.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Fixed ``flavor show/delete/set/unset`` command to properly + find a private flavor by flavor name. + [Bug `1575478 `_]