Revision 1716a15d kamaki/cli/commands/image.py
b/kamaki/cli/commands/image.py | ||
---|---|---|
212 | 212 |
prop=KeyValueArgument('filter by property key=value', ('--property')), |
213 | 213 |
prop_like=KeyValueArgument( |
214 | 214 |
'fliter by property key=value where value is part of actual value', |
215 |
('--property-like')) |
|
215 |
('--property-like')),
|
|
216 | 216 |
) |
217 | 217 |
|
218 | 218 |
def _filtered_by_owner(self, detail, *list_params): |
... | ... | |
642 | 642 |
class image_compute_list(_init_cyclades, _optional_json): |
643 | 643 |
"""List images""" |
644 | 644 |
|
645 |
PERMANENTS = ('id', 'name') |
|
646 |
|
|
645 | 647 |
arguments = dict( |
646 | 648 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
647 | 649 |
limit=IntArgument('limit number listed images', ('-n', '--number')), |
648 | 650 |
more=FlagArgument( |
649 | 651 |
'output results in pages (-n to set items per page, default 10)', |
650 | 652 |
'--more'), |
651 |
enum=FlagArgument('Enumerate results', '--enumerate') |
|
653 |
enum=FlagArgument('Enumerate results', '--enumerate'), |
|
654 |
meta=KeyValueArgument( |
|
655 |
'filter by metadata key=value (can be repeated)', ('--metadata')), |
|
656 |
meta_like=KeyValueArgument( |
|
657 |
'filter by metadata key=value (can be repeated)', |
|
658 |
('--metadata-like')) |
|
652 | 659 |
) |
653 | 660 |
|
661 |
def _filter_by_metadata(self, images): |
|
662 |
new_images = [] |
|
663 |
|
|
664 |
def like_metadata(meta): |
|
665 |
mlike = self['meta_like'] |
|
666 |
for k, v in mlike.items(): |
|
667 |
likestr = meta.get(k, '').lower() |
|
668 |
if v.lower() not in likestr: |
|
669 |
return False |
|
670 |
return True |
|
671 |
|
|
672 |
for img in images: |
|
673 |
meta = img['metadata'] |
|
674 |
if ( |
|
675 |
self['meta'] and set( |
|
676 |
self['meta'].items()).difference(meta.items())) or ( |
|
677 |
self['meta_like'] and not like_metadata(meta)): |
|
678 |
continue |
|
679 |
elif self['detail']: |
|
680 |
new_images.append(dict(img)) |
|
681 |
else: |
|
682 |
new_images.append(dict()) |
|
683 |
for k in set(img).intersection(self.PERMANENTS): |
|
684 |
new_images[-1][k] = img[k] |
|
685 |
return new_images |
|
686 |
|
|
687 |
def _add_name(self, images, key='user_id'): |
|
688 |
uuids = self._uuids2usernames( |
|
689 |
list(set([img[key] for img in images]))) |
|
690 |
for img in images: |
|
691 |
img[key] += ' (%s)' % uuids[img[key]] |
|
692 |
return images |
|
693 |
|
|
654 | 694 |
@errors.generic.all |
655 | 695 |
@errors.cyclades.connection |
656 | 696 |
def _run(self): |
657 |
images = self.client.list_images(self['detail']) |
|
697 |
withmeta = bool(self['meta'] or self['meta_like']) |
|
698 |
detail = self['detail'] or withmeta |
|
699 |
images = self.client.list_images(detail) |
|
700 |
if withmeta: |
|
701 |
images = self._filter_by_metadata(images) |
|
702 |
if self['detail'] and not self['json_output']: |
|
703 |
images = self._add_name(self._add_name(images, 'tenant_id')) |
|
658 | 704 |
kwargs = dict(with_enumeration=self['enum']) |
659 | 705 |
if self['more']: |
660 | 706 |
kwargs['page_size'] = self['limit'] or 10 |
Also available in: Unified diff