Skip to content

Commit 681d6dc

Browse files
author
Huanxuan Ao
committed
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
1 parent 9ec41c0 commit 681d6dc

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

openstackclient/compute/v2/flavor.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@
1818
import six
1919

2020
from openstackclient.common import command
21+
from openstackclient.common import exceptions
2122
from openstackclient.common import parseractions
2223
from openstackclient.common import utils
2324

2425

26+
def _find_flavor(compute_client, flavor):
27+
try:
28+
return compute_client.flavors.get(flavor)
29+
except Exception as ex:
30+
if type(ex).__name__ == 'NotFound':
31+
pass
32+
else:
33+
raise
34+
try:
35+
return compute_client.flavors.find(name=flavor, is_public=None)
36+
except Exception as ex:
37+
if type(ex).__name__ == 'NotFound':
38+
msg = "No flavor with a name or ID of '%s' exists." % flavor
39+
raise exceptions.CommandError(msg)
40+
else:
41+
raise
42+
43+
2544
class CreateFlavor(command.ShowOne):
2645
"""Create new flavor"""
2746

@@ -132,8 +151,7 @@ def get_parser(self, prog_name):
132151

133152
def take_action(self, parsed_args):
134153
compute_client = self.app.client_manager.compute
135-
flavor = utils.find_resource(compute_client.flavors,
136-
parsed_args.flavor)
154+
flavor = _find_flavor(compute_client, parsed_args.flavor)
137155
compute_client.flavors.delete(flavor.id)
138156

139157

@@ -239,8 +257,7 @@ def get_parser(self, prog_name):
239257

240258
def take_action(self, parsed_args):
241259
compute_client = self.app.client_manager.compute
242-
flavor = utils.find_resource(compute_client.flavors,
243-
parsed_args.flavor)
260+
flavor = _find_flavor(compute_client, parsed_args.flavor)
244261
flavor.set_keys(parsed_args.property)
245262

246263

@@ -258,8 +275,7 @@ def get_parser(self, prog_name):
258275

259276
def take_action(self, parsed_args):
260277
compute_client = self.app.client_manager.compute
261-
resource_flavor = utils.find_resource(compute_client.flavors,
262-
parsed_args.flavor)
278+
resource_flavor = _find_flavor(compute_client, parsed_args.flavor)
263279
flavor = resource_flavor._info.copy()
264280
flavor.pop("links", None)
265281

@@ -290,6 +306,5 @@ def get_parser(self, prog_name):
290306

291307
def take_action(self, parsed_args):
292308
compute_client = self.app.client_manager.compute
293-
flavor = utils.find_resource(compute_client.flavors,
294-
parsed_args.flavor)
309+
flavor = _find_flavor(compute_client, parsed_args.flavor)
295310
flavor.unset_keys(parsed_args.property)

openstackclient/tests/compute/v2/test_flavor.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def setUp(self):
273273
super(TestFlavorSet, self).setUp()
274274

275275
self.flavors_mock.find.return_value = self.flavor
276-
276+
self.flavors_mock.get.side_effect = exceptions.NotFound(None)
277277
self.cmd = flavor.SetFlavor(self.app, None)
278278

279279
def test_flavor_set(self):
@@ -288,10 +288,8 @@ def test_flavor_set(self):
288288
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
289289

290290
result = self.cmd.take_action(parsed_args)
291-
try:
292-
self.flavors_mock.find.assert_called_with(name=parsed_args.flavor)
293-
except Exception:
294-
self.flavors_mock.get.assert_called_with(parsed_args.flavor)
291+
self.flavors_mock.find.assert_called_with(name=parsed_args.flavor,
292+
is_public=None)
295293
self.assertIsNone(result)
296294

297295

@@ -331,9 +329,9 @@ class TestFlavorShow(TestFlavor):
331329
def setUp(self):
332330
super(TestFlavorShow, self).setUp()
333331

334-
# Return value of utils.find_resource()
335-
self.flavors_mock.get.return_value = self.flavor
336-
332+
# Return value of _find_resource()
333+
self.flavors_mock.find.return_value = self.flavor
334+
self.flavors_mock.get.side_effect = exceptions.NotFound(None)
337335
self.cmd = flavor.ShowFlavor(self.app, None)
338336

339337
def test_show_no_options(self):
@@ -369,7 +367,7 @@ def setUp(self):
369367
super(TestFlavorUnset, self).setUp()
370368

371369
self.flavors_mock.find.return_value = self.flavor
372-
370+
self.flavors_mock.get.side_effect = exceptions.NotFound(None)
373371
self.cmd = flavor.UnsetFlavor(self.app, None)
374372

375373
def test_flavor_unset(self):
@@ -384,8 +382,6 @@ def test_flavor_unset(self):
384382
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
385383

386384
result = self.cmd.take_action(parsed_args)
387-
try:
388-
self.flavors_mock.find.assert_called_with(name=parsed_args.flavor)
389-
except Exception:
390-
self.flavors_mock.get.assert_called_with(parsed_args.flavor)
385+
self.flavors_mock.find.assert_called_with(name=parsed_args.flavor,
386+
is_public=None)
391387
self.assertIsNone(result)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- Fixed ``flavor show/delete/set/unset`` command to properly
4+
find a private flavor by flavor name.
5+
[Bug `1575478 <https://bugs.launchpad.net/bugs/1575478>`_]

0 commit comments

Comments
 (0)