Skip to content

Commit 7d7a429

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add ability to filter image list by tag"
2 parents 3493948 + 9edbab8 commit 7d7a429

5 files changed

Lines changed: 55 additions & 7 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ List available images
209209
[--property <key=value>]
210210
[--name <name>]
211211
[--status <status>]
212+
[--tag <tag>]
212213
[--long]
213214
[--sort <key>[:<direction>]]
214215
[--limit <num-images>]
@@ -244,6 +245,12 @@ List available images
244245
245246
*Image version 2 only*
246247
248+
.. option:: --tag <tag>
249+
250+
Filter images based on tag
251+
252+
*Image version 2 only*
253+
247254
.. option:: --long
248255
249256
List additional fields in output

openstackclient/image/v2/image.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ def get_parser(self, prog_name):
464464
default=None,
465465
help=_("Filter images based on status.")
466466
)
467+
parser.add_argument(
468+
'--tag',
469+
metavar='<tag>',
470+
default=None,
471+
help=_('Filter images based on tag.'),
472+
)
467473
parser.add_argument(
468474
'--long',
469475
action='store_true',
@@ -521,6 +527,8 @@ def take_action(self, parsed_args):
521527
kwargs['name'] = parsed_args.name
522528
if parsed_args.status:
523529
kwargs['status'] = parsed_args.status
530+
if parsed_args.tag:
531+
kwargs['tag'] = parsed_args.tag
524532
if parsed_args.long:
525533
columns = (
526534
'ID',

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class ImageTests(base.TestCase):
2828
@classmethod
2929
def setUpClass(cls):
3030
super(ImageTests, cls).setUpClass()
31+
cls.image_tag = 'my_tag'
3132
json_output = json.loads(cls.openstack(
3233
'--os-image-api-version 2 '
33-
'image create -f json ' +
34-
cls.NAME
34+
'image create -f json --tag {tag} {name}'.format(
35+
tag=cls.image_tag, name=cls.NAME)
3536
))
3637
cls.image_id = json_output["id"]
3738
cls.assertOutput(cls.NAME, json_output['name'])
@@ -81,6 +82,16 @@ def test_image_list_with_status_filter(self):
8182
[img['Status'] for img in json_output]
8283
)
8384

85+
def test_image_list_with_tag_filter(self):
86+
json_output = json.loads(self.openstack(
87+
'image list --tag ' + self.image_tag + ' --long -f json'
88+
))
89+
for taglist in [img['Tags'].split(', ') for img in json_output]:
90+
self.assertIn(
91+
self.image_tag,
92+
taglist
93+
)
94+
8495
def test_image_attributes(self):
8596
"""Test set, unset, show on attributes, tags and properties"""
8697

@@ -142,6 +153,10 @@ def test_image_attributes(self):
142153
)
143154

144155
# Test tags
156+
self.assertNotIn(
157+
'01',
158+
json_output["tags"].split(', ')
159+
)
145160
self.openstack(
146161
'image set ' +
147162
'--tag 01 ' +
@@ -151,9 +166,9 @@ def test_image_attributes(self):
151166
'image show -f json ' +
152167
self.NAME
153168
))
154-
self.assertEqual(
169+
self.assertIn(
155170
'01',
156-
json_output["tags"],
171+
json_output["tags"].split(', ')
157172
)
158173

159174
self.openstack(
@@ -165,9 +180,9 @@ def test_image_attributes(self):
165180
'image show -f json ' +
166181
self.NAME
167182
))
168-
self.assertEqual(
169-
'',
170-
json_output["tags"],
183+
self.assertNotIn(
184+
'01',
185+
json_output["tags"].split(', ')
171186
)
172187

173188
def test_image_set_rename(self):

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,20 @@ def test_image_list_status_option(self):
779779
status='active', marker=self._image.id
780780
)
781781

782+
def test_image_list_tag_option(self):
783+
arglist = [
784+
'--tag', 'abc',
785+
]
786+
verifylist = [
787+
('tag', 'abc'),
788+
]
789+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
790+
791+
columns, data = self.cmd.take_action(parsed_args)
792+
self.api_mock.image_list.assert_called_with(
793+
tag='abc', marker=self._image.id
794+
)
795+
782796

783797
class TestListImageProjects(TestImage):
784798

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
Add ``--tag`` option to ``image list`` command to filter by tag.

0 commit comments

Comments
 (0)