From f0445a1b443182cf77e27038cc5f90c424e00c62 Mon Sep 17 00:00:00 2001
From: Brian Waldon <bcwaldon@gmail.com>
Date: Fri, 13 Jul 2012 22:03:22 +0000
Subject: [PATCH] Rewrite link parsing for finding v2 schemas

What we called 'links' are no longer returned in a container of
objects, they are top-level entity attribtues. This fixes the
parsing of the entities to look in the correct place when trying
to locate a specific schema.

Add a helper for printing to stderr and exiting with a non-zero
exit code.

Map 'name' to 'Attribute' when explaining a schema.

Related to bp glance-client-v2

Change-Id: Ib98e912a7af0bb570b4fd738733edd9b837d1a05
---
 glanceclient/common/utils.py |  9 ++++++++-
 glanceclient/v2/schemas.py   |  8 ++++----
 glanceclient/v2/shell.py     | 14 ++++++++++----
 tests/v2/test_schemas.py     |  8 ++++----
 4 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py
index 8bbb7acc..07e77a14 100644
--- a/glanceclient/common/utils.py
+++ b/glanceclient/common/utils.py
@@ -14,6 +14,7 @@
 #    under the License.
 
 import os
+import sys
 import uuid
 
 import prettytable
@@ -47,7 +48,7 @@ def print_list(objs, fields, formatters={}):
                 row.append(formatters[field](o))
             else:
                 field_name = field.lower().replace(' ', '_')
-                data = getattr(o, field_name, '')
+                data = getattr(o, field_name, None) or ''
                 row.append(data)
         pt.add_row(row)
 
@@ -123,3 +124,9 @@ def import_versioned_module(version, submodule=None):
     if submodule:
         module = '.'.join((module, submodule))
     return importutils.import_module(module)
+
+
+def exit(msg=''):
+    if msg:
+        print >> sys.stderr, msg
+    sys.exit(1)
diff --git a/glanceclient/v2/schemas.py b/glanceclient/v2/schemas.py
index 8757da1d..b4ed61cf 100644
--- a/glanceclient/v2/schemas.py
+++ b/glanceclient/v2/schemas.py
@@ -52,7 +52,7 @@ class Controller(object):
 
     def _find_schema_uri(self, schema_name):
         _, schema_index = self.http_client.json_request('GET', '/v2/schemas')
-        for link in schema_index['links']:
-            if link['rel'] == schema_name:
-                return link['href']
-        raise exc.SchemaNotFound(schema_name)
+        try:
+            return schema_index[schema_name]
+        except KeyError:
+            raise exc.SchemaNotFound(schema_name)
diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py
index 90c2ea8a..32a84879 100644
--- a/glanceclient/v2/shell.py
+++ b/glanceclient/v2/shell.py
@@ -14,6 +14,7 @@
 #    under the License.
 
 from glanceclient.common import utils
+from glanceclient import exc
 
 
 def do_image_list(gc, args):
@@ -23,9 +24,14 @@ def do_image_list(gc, args):
     utils.print_list(images, columns)
 
 
-@utils.arg('name', metavar='<NAME>', help='Name of model to describe.')
+@utils.arg('model', metavar='<MODEL>', help='Name of model to describe.')
 def do_explain(gc, args):
     """Describe a specific model."""
-    schema = gc.schemas.get(args.name)
-    columns = ['Name', 'Description']
-    utils.print_list(schema.properties, columns)
+    try:
+        schema = gc.schemas.get(args.model)
+    except exc.SchemaNotFound:
+        utils.exit('Unable to find requested model \'%s\'' % args.model)
+    else:
+        formatters = {'Attribute': lambda m: m.name}
+        columns = ['Attribute', 'Description']
+        utils.print_list(schema.properties, columns, formatters)
diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py
index b67b976b..6e64c29f 100644
--- a/tests/v2/test_schemas.py
+++ b/tests/v2/test_schemas.py
@@ -23,10 +23,10 @@ fixtures = {
     '/v2/schemas': {
         'GET': (
             {},
-            {'links': [
-                {'rel': 'image', 'href': '/v2/schemas/image'},
-                {'rel': 'access', 'href': '/v2/schemas/image/access'},
-            ]},
+            {
+                'image': '/v2/schemas/image',
+                'access': '/v2/schemas/image/access',
+            },
         ),
     },
     '/v2/schemas/image': {