From 4733fd0d3cd328a8abbd62cbfabd973b0986c58c Mon Sep 17 00:00:00 2001
From: Steve Martinelli <stevemar@ca.ibm.com>
Date: Thu, 10 Sep 2015 16:06:32 -0500
Subject: [PATCH] Add support for showing account details

add the command `openstack account show` that lists details
about the object store account that the user authenticated
against.

Partial-Bug: #1501943

Change-Id: I1246dafee812b63a41d43be4e3598224364a2c11
---
 doc/source/command-objects/account.rst | 10 ++++++++++
 openstackclient/api/object_store_v1.py | 21 +++++++++++++++++++++
 openstackclient/object/v1/account.py   | 14 +++++++++++++-
 setup.cfg                              |  1 +
 4 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/doc/source/command-objects/account.rst b/doc/source/command-objects/account.rst
index 6783fd6be3..db4ad9b16e 100644
--- a/doc/source/command-objects/account.rst
+++ b/doc/source/command-objects/account.rst
@@ -19,6 +19,16 @@ Set account properties
 
     Set a property on this account (repeat option to set multiple properties)
 
+account show
+------------
+
+Display account details
+
+.. program:: account show
+.. code:: bash
+
+    os account show
+
 account unset
 -------------
 
diff --git a/openstackclient/api/object_store_v1.py b/openstackclient/api/object_store_v1.py
index c817b65070..afc5b4c163 100644
--- a/openstackclient/api/object_store_v1.py
+++ b/openstackclient/api/object_store_v1.py
@@ -411,6 +411,23 @@ class APIv1(api.BaseAPI):
             # registered in the catalog
             self.create("", headers=headers)
 
+    def account_show(self):
+        """Show account details"""
+
+        # NOTE(stevemar): Just a HEAD request to the endpoint already in the
+        # catalog should be enough.
+        response = self._request("HEAD", "")
+        data = {}
+        for k, v in response.headers.iteritems():
+            data[k] = v
+        # Map containers, bytes and objects a bit nicer
+        data['Containers'] = data.pop('x-account-container-count', None)
+        data['Objects'] = data.pop('x-account-object-count', None)
+        data['Bytes'] = data.pop('x-account-bytes-used', None)
+        # Add in Account info too
+        data['Account'] = self._find_account_id()
+        return data
+
     def account_unset(
         self,
         properties,
@@ -433,3 +450,7 @@ class APIv1(api.BaseAPI):
 
         if headers:
             self.create("", headers=headers)
+
+    def _find_account_id(self):
+        url_parts = urlparse(self.endpoint)
+        return url_parts.path.split('/')[-1]
diff --git a/openstackclient/object/v1/account.py b/openstackclient/object/v1/account.py
index 1f38b96a82..4ff890ce10 100644
--- a/openstackclient/object/v1/account.py
+++ b/openstackclient/object/v1/account.py
@@ -13,10 +13,11 @@
 
 """Account v1 action implementations"""
 
-
 import logging
 
 from cliff import command
+from cliff import show
+import six
 
 from openstackclient.common import parseractions
 from openstackclient.common import utils
@@ -46,6 +47,17 @@ class SetAccount(command.Command):
         )
 
 
+class ShowAccount(show.ShowOne):
+    """Display account details"""
+
+    log = logging.getLogger(__name__ + '.ShowAccount')
+
+    @utils.log_method(log)
+    def take_action(self, parsed_args):
+        data = self.app.client_manager.object_store.account_show()
+        return zip(*sorted(six.iteritems(data)))
+
+
 class UnsetAccount(command.Command):
     """Unset account properties"""
 
diff --git a/setup.cfg b/setup.cfg
index f33e7c6d67..c84327073b 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -332,6 +332,7 @@ openstack.network.v2 =
 
 openstack.object_store.v1 =
     account_set = openstackclient.object.v1.account:SetAccount
+    account_show = openstackclient.object.v1.account:ShowAccount
     account_unset = openstackclient.object.v1.account:UnsetAccount
     container_create = openstackclient.object.v1.container:CreateContainer
     container_delete = openstackclient.object.v1.container:DeleteContainer