From d8f7527ff2231c4755b0bce28d1ad38fe11a9370 Mon Sep 17 00:00:00 2001
From: Steve Martinelli <stevemar@ca.ibm.com>
Date: Sun, 20 Sep 2015 15:44:00 -0400
Subject: [PATCH] Format an images properties and tags

Currently, these properties are each top level keys, they should
all be under a single 'properties' field. Secondly, the tags are
kept as an array, but can be shown as a comma separated string.

Change-Id: Ic769c657a86e768fee38acc40434c377de70a7bc
---
 openstackclient/image/v2/image.py | 38 ++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py
index 67390118e7..01468c4d11 100644
--- a/openstackclient/image/v2/image.py
+++ b/openstackclient/image/v2/image.py
@@ -35,6 +35,38 @@ DEFAULT_CONTAINER_FORMAT = 'bare'
 DEFAULT_DISK_FORMAT = 'raw'
 
 
+def _format_image(image):
+    """Format an image to make it more consistent with OSC operations. """
+
+    info = {}
+    properties = {}
+
+    # the only fields we're not including is "links", "tags" and the properties
+    fields_to_show = ['status', 'name', 'container_format', 'created_at',
+                      'size', 'disk_format', 'updated_at', 'visibility',
+                      'min_disk', 'protected', 'id', 'file', 'checksum',
+                      'owner', 'virtual_size', 'min_ram', 'schema']
+
+    # split out the usual key and the properties which are top-level
+    for key in six.iterkeys(image):
+        if key in fields_to_show:
+            info[key] = image.get(key)
+        elif key == 'tags':
+            continue  # handle this later
+        else:
+            properties[key] = image.get(key)
+
+    # format the tags if they are there
+    if image.get('tags'):
+        info['tags'] = utils.format_list(image.get('tags'))
+
+    # add properties back into the dictionary as a top-level key
+    if properties:
+        info['properties'] = utils.format_dict(properties)
+
+    return info
+
+
 class AddProjectToImage(show.ShowOne):
     """Associate project with image"""
 
@@ -254,7 +286,8 @@ class CreateImage(show.ShowOne):
                 # update the image after the data has been uploaded
                 image = image_client.images.get(image.id)
 
-        return zip(*sorted(six.iteritems(image)))
+        info = _format_image(image)
+        return zip(*sorted(six.iteritems(info)))
 
 
 class DeleteImage(command.Command):
@@ -512,8 +545,7 @@ class ShowImage(show.ShowOne):
             parsed_args.image,
         )
 
-        info = {}
-        info.update(image)
+        info = _format_image(image)
         return zip(*sorted(six.iteritems(info)))