Skip to content

Commit bca8d57

Browse files
nidhimittalstevemar
authored andcommitted
image-list should support filters 'name','status'
nova api support parameters like 'name', 'server', 'status', etc in image-list(). So openstackclient should support this too. DocImpact Closes-Bug: #1698742 Change-Id: Ice66b409f989e6785aa3b2d42f2fdbf6e23fa0aa
1 parent 3cba09e commit bca8d57

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

doc/source/cli/command-objects/image.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ List available images
209209
[--sort <key>[:<direction>]]
210210
[--limit <num-images>]
211211
[--marker <image>]
212+
[--name <name>]
213+
[--status <status>]
214+
212215
213216
.. option:: --public
214217
@@ -248,6 +251,15 @@ List available images
248251
The last image of the previous page. Display list of images
249252
after marker. Display all images if not specified. (name or ID)
250253
254+
.. option:: --name <name>
255+
256+
Filter images based on name
257+
258+
.. option:: --status <status>
259+
260+
Filter images based on status
261+
262+
251263
*Image version 2 only*
252264
253265
image remove project

openstackclient/image/v2/image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ def get_parser(self, prog_name):
452452
action=parseractions.KeyValueAction,
453453
help=_('Filter output based on property'),
454454
)
455+
parser.add_argument(
456+
'--name',
457+
metavar='<name>',
458+
default=None,
459+
help=_("Filter images based on name.")
460+
)
461+
parser.add_argument(
462+
'--status',
463+
metavar='<status>',
464+
default=None,
465+
help=_("Filter images based on status.")
466+
)
455467
parser.add_argument(
456468
'--long',
457469
action='store_true',
@@ -505,6 +517,10 @@ def take_action(self, parsed_args):
505517
if parsed_args.marker:
506518
kwargs['marker'] = utils.find_resource(image_client.images,
507519
parsed_args.marker).id
520+
if parsed_args.name:
521+
kwargs['name'] = parsed_args.name
522+
if parsed_args.status:
523+
kwargs['status'] = parsed_args.status
508524
if parsed_args.long:
509525
columns = (
510526
'ID',

openstackclient/tests/functional/image/v2/test_image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ def test_image_list(self):
5151
[img['Name'] for img in json_output]
5252
)
5353

54+
def test_image_list_with_name_filter(self):
55+
json_output = json.loads(self.openstack(
56+
'image list --name ' + self.NAME + ' -f json'
57+
))
58+
self.assertIn(
59+
self.NAME,
60+
[img['Name'] for img in json_output]
61+
)
62+
63+
def test_image_list_with_status_filter(self):
64+
json_output = json.loads(self.openstack(
65+
'image list ' + ' --status active -f json'
66+
))
67+
self.assertIn(
68+
'active',
69+
[img['Status'] for img in json_output]
70+
)
71+
5472
def test_image_attributes(self):
5573
"""Test set, unset, show on attributes, tags and properties"""
5674

openstackclient/tests/unit/image/v2/test_image.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,34 @@ def test_image_list_marker_option(self, fr_mock):
750750
marker=image_fakes.image_id,
751751
)
752752

753+
def test_image_list_name_option(self):
754+
arglist = [
755+
'--name', 'abc',
756+
]
757+
verifylist = [
758+
('name', 'abc'),
759+
]
760+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
761+
762+
columns, data = self.cmd.take_action(parsed_args)
763+
self.api_mock.image_list.assert_called_with(
764+
name='abc', marker=self._image.id
765+
)
766+
767+
def test_image_list_status_option(self):
768+
arglist = [
769+
'--status', 'active',
770+
]
771+
verifylist = [
772+
('status', 'active'),
773+
]
774+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
775+
776+
columns, data = self.cmd.take_action(parsed_args)
777+
self.api_mock.image_list.assert_called_with(
778+
status='active', marker=self._image.id
779+
)
780+
753781

754782
class TestRemoveProjectImage(TestImage):
755783

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--name`` and ``--status`` options to ``image list`` command
5+
to filter images based on name and status respectively.
6+
[Bug `1698742 <https://bugs.launchpad.net/bugs/1698742>`_]

0 commit comments

Comments
 (0)