Augment user info in image list
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 21 Aug 2013 14:53:07 +0000 (17:53 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Wed, 21 Aug 2013 14:53:07 +0000 (17:53 +0300)
Refs: #4228

kamaki/cli/commands/__init__.py
kamaki/cli/commands/image.py
kamaki/clients/astakos/__init__.py

index 73c532b..bc5f584 100644 (file)
@@ -94,6 +94,16 @@ class _command_init(object):
     def _custom_version(self, service):
         return self.config.get_cloud(self.cloud, '%s_version' % service)
 
+    def _uuid2username(self, uuid):
+        r = self.auth_base.post_user_catalogs([uuid])
+        uuids = r.json['uuid_catalog']
+        return uuids.get(uuid, None)
+
+    def _username2uuid(self, username):
+        r = self.auth_base.post_user_catalogs(displaynames=[username])
+        names = r.json['displayname_catalog']
+        return names.get(username, None)
+
     def _set_log_params(self):
         try:
             self.client.LOG_TOKEN, self.client.LOG_DATA = (
index 2cf300a..6860371 100644 (file)
@@ -199,6 +199,7 @@ class image_list(_init_image, _optional_json):
         size_max=IntArgument('filter by maximum size', '--size-max'),
         status=ValueArgument('filter by status', '--status'),
         owner=ValueArgument('filter by owner', '--owner'),
+        owner_name=ValueArgument('filter by owners username', '--owner-name'),
         order=ValueArgument(
             'order by FIELD ( - to reverse order)',
             '--order',
@@ -213,19 +214,27 @@ class image_list(_init_image, _optional_json):
 
     def _filtered_by_owner(self, detail, *list_params):
         images = []
-        MINKEYS = set([
-            'id', 'size', 'status', 'disk_format', 'container_format', 'name'])
+        ouuid = self['owner'] or self._username2uuid(self['owner_name'])
+        if not ouuid:
+            return images
         for img in self.client.list_public(True, *list_params):
-            if img['owner'] == self['owner']:
+            if img['owner'] == ouuid:
                 if not detail:
-                    for key in set(img.keys()).difference(MINKEYS):
+                    for key in set(img.keys()).difference(self.PERMANENTS):
                         img.pop(key)
                 images.append(img)
         return images
 
     def _filtered_by_name(self, images):
         np, ns, nl = self['name_pref'], self['name_suff'], self['name_like']
-        return [img for img in images if (
+
+        def augment_owner(img):
+            uuid = img.get('owner', None)
+            if uuid and not self['json_output']:
+                img['owner'] = '%s (%s)' % (uuid, self._uuid2username(uuid))
+            return img
+
+        return [augment_owner(img) for img in images if (
             (not np) or img['name'].lower().startswith(np.lower())) and (
             (not ns) or img['name'].lower().endswith(ns.lower())) and (
             (not nl) or nl.lower() in img['name'].lower())]
@@ -259,7 +268,7 @@ class image_list(_init_image, _optional_json):
 
         order = self['order']
         detail = self['detail'] or self['prop']
-        if self['owner']:
+        if self['owner'] or self['owner_name']:
             images = self._filtered_by_owner(detail, filters, order)
         else:
             images = self.client.list_public(detail, filters, order)
index e51b778..4801980 100644 (file)
@@ -155,14 +155,17 @@ class AstakosClient(Client):
         """Get (cached) term, from user credentials"""
         return self.user_info(token).get(key, None)
 
-    def post_user_catalogs(self, uuids):
+    def post_user_catalogs(self, uuids=None, displaynames=None):
         """POST base_url/user_catalogs
 
         :param uuids: (list or tuple) user uuids
 
-        :returns: (dict) {uuid1: name1, uuid2: name2, ...}
+        :param displaynames: (list or tuple) usernames (mut. excl. to uuids)
+
+        :returns: (dict) {uuid1: name1, uuid2: name2, ...} or oposite
         """
         account_url = self.get_service_endpoints('account')['publicURL']
         account = AstakosClient(account_url, self.token)
-        json_data = dict(uuids=uuids)
+        json_data = dict(uuids=uuids) if (
+            uuids) else dict(displaynames=displaynames)
         return account.post('user_catalogs', json=json_data)