Commit 2d1202ee authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Add --property to image list

Refs: #4220

Also, fix various bugs introduced by utils re-writtings
parent daedfe3c
......@@ -280,7 +280,7 @@ class KeyValueArgument(Argument):
:syntax: --<arg> key1=value1 --<arg> key2=value2 ...
"""
def __init__(self, help='', parsed_name=None, default={}):
def __init__(self, help='', parsed_name=None, default=[]):
super(KeyValueArgument, self).__init__(-1, help, parsed_name, default)
@property
......@@ -295,7 +295,7 @@ class KeyValueArgument(Argument):
"""
:param keyvalue_pairs: (str) ['key1=val1', 'key2=val2', ...]
"""
self._value = {}
self._value = getattr(self, '_value', self.value) or {}
try:
for pair in keyvalue_pairs:
key, sep, val = pair.partition('=')
......
......@@ -302,7 +302,7 @@ class KeyValueArgument(TestCase):
def test_value(self):
kva = argument.KeyValueArgument(parsed_name='--keyval')
self.assertEqual(kva.value, {})
self.assertEqual(kva.value, [])
for kvpairs in (
'strval', 'key=val', 2.8, 42, None,
('key', 'val'), ('key val'), ['=val', 'key=val'],
......@@ -311,6 +311,8 @@ class KeyValueArgument(TestCase):
kva.value = kvpairs
except Exception as e:
self.assertTrue(isinstance(e, errors.CLIError))
old = dict()
kva = argument.KeyValueArgument(parsed_name='--keyval')
for kvpairs, exp in (
(('key=val', ), {'key': 'val'}),
(['key1=val1', 'key2=val2'], {'key1': 'val1', 'key2': 'val2'}),
......@@ -320,7 +322,8 @@ class KeyValueArgument(TestCase):
(('k=v1', 'k=v2', 'k=v3'), {'k': 'v3'})
):
kva.value = kvpairs
assert_dicts_are_equal(self, kva.value, exp)
old.update(exp)
assert_dicts_are_equal(self, kva.value, old)
class ProgressBarArgument(TestCase):
......
......@@ -175,6 +175,10 @@ def _validate_image_location(location):
class image_list(_init_image, _optional_json):
"""List images accessible by user"""
PERMANENTS = (
'id', 'name',
'status', 'container_format', 'disk_format', 'size')
arguments = dict(
detail=FlagArgument('show detailed output', ('-l', '--details')),
container_format=ValueArgument(
......@@ -203,7 +207,8 @@ class image_list(_init_image, _optional_json):
more=FlagArgument(
'output results in pages (-n to set items per page, default 10)',
'--more'),
enum=FlagArgument('Enumerate results', '--enumerate')
enum=FlagArgument('Enumerate results', '--enumerate'),
prop=KeyValueArgument('filter by property key=value', ('--property'))
)
def _filtered_by_owner(self, detail, *list_params):
......@@ -225,6 +230,19 @@ class image_list(_init_image, _optional_json):
(not ns) or img['name'].lower().endswith(ns.lower())) and (
(not nl) or nl.lower() in img['name'].lower())]
def _filtered_by_properties(self, images):
new_images = []
for img in images:
if set(self['prop'].items()).difference(img['properties'].items()):
continue
if self['detail']:
new_images.append(dict(img))
else:
new_images.append(dict())
for k in set(img).intersection(self.PERMANENTS):
new_images[-1][k] = img[k]
return new_images
@errors.generic.all
@errors.cyclades.connection
def _run(self):
......@@ -240,13 +258,15 @@ class image_list(_init_image, _optional_json):
filters[arg] = self[arg]
order = self['order']
detail = self['detail']
detail = self['detail'] or self['prop']
if self['owner']:
images = self._filtered_by_owner(detail, filters, order)
else:
images = self.client.list_public(detail, filters, order)
images = self._filtered_by_name(images)
if self['prop']:
images = self._filtered_by_properties(images)
kwargs = dict(with_enumeration=self['enum'])
if self['more']:
kwargs['page_size'] = self['limit'] or 10
......
......@@ -248,8 +248,8 @@ class UtilsMethods(TestCase):
print_items(*args)
if not (isinstance(items, dict) or isinstance(
items, list) or isinstance(items, tuple)):
self.assertEqual(PR.mock_calls[-1], call(
'%s' % items if items is not None else ''))
if items:
self.assertEqual(PR.mock_calls[-1], call('%s' % items))
else:
for i, item in enumerate(items):
if with_enumeration:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment