summaryrefslogtreecommitdiff
path: root/openstackclient/identity/v2_0/user.py
diff options
context:
space:
mode:
authorAkihiro Motoki <amotoki@gmail.com>2017-05-15 04:00:32 +0000
committerDean Troyer <dtroyer@gmail.com>2019-06-22 16:18:26 +0000
commitfa5046a3dbf8cf398b9a05c68e6ee4badbb14ee7 (patch)
treef6401632eb05db3d21f42e70695196976bc7408c /openstackclient/identity/v2_0/user.py
parentc44f26eb7e41c28bb13ef9bd31c8ddda9e638862 (diff)
downloadpython-openstackclient-fa5046a3dbf8cf398b9a05c68e6ee4badbb14ee7.tar.gz
Use cliff formattable columns in identity commands
Partial-Bug: #1687955 Partially implement blueprint osc-formattable-columns Change-Id: Ia13314a012b3a7363ffb24a13c79c6ecdff1ed7b
Diffstat (limited to 'openstackclient/identity/v2_0/user.py')
-rw-r--r--openstackclient/identity/v2_0/user.py41
1 files changed, 31 insertions, 10 deletions
diff --git a/openstackclient/identity/v2_0/user.py b/openstackclient/identity/v2_0/user.py
index 2a3dde6b..0675877b 100644
--- a/openstackclient/identity/v2_0/user.py
+++ b/openstackclient/identity/v2_0/user.py
@@ -15,8 +15,10 @@
"""Identity v2.0 User action implementations"""
+import functools
import logging
+from cliff import columns as cliff_columns
from keystoneauth1 import exceptions as ks_exc
from osc_lib.command import command
from osc_lib import exceptions
@@ -29,6 +31,31 @@ from openstackclient.i18n import _
LOG = logging.getLogger(__name__)
+class ProjectColumn(cliff_columns.FormattableColumn):
+ """Formattable column for project column.
+
+ Unlike the parent FormattableColumn class, the initializer of the
+ class takes project_cache as the second argument.
+ osc_lib.utils.get_item_properties instantiate cliff FormattableColumn
+ object with a single parameter "column value", so you need to pass
+ a partially initialized class like
+ ``functools.partial(ProjectColumn, project_cache)``.
+ """
+
+ def __init__(self, value, project_cache=None):
+ super(ProjectColumn, self).__init__(value)
+ self.project_cache = project_cache or {}
+
+ def human_readable(self):
+ project = self._value
+ if not project:
+ return ""
+ if project in self.project_cache.keys():
+ return self.project_cache[project].name
+ else:
+ return project
+
+
class CreateUser(command.ShowOne):
_description = _("Create new user")
@@ -187,15 +214,7 @@ class ListUser(command.Lister):
def take_action(self, parsed_args):
identity_client = self.app.client_manager.identity
-
- def _format_project(project):
- if not project:
- return ""
- if project in project_cache.keys():
- return project_cache[project].name
- else:
- return project
-
+ formatters = {}
project = None
if parsed_args.project:
project = utils.find_resource(
@@ -227,6 +246,8 @@ class ListUser(command.Lister):
except Exception:
# Just forget it if there's any trouble
pass
+ formatters['tenantId'] = functools.partial(
+ ProjectColumn, project_cache=project_cache)
else:
columns = column_headers = ('ID', 'Name')
data = identity_client.users.list(tenant_id=project)
@@ -251,7 +272,7 @@ class ListUser(command.Lister):
(utils.get_item_properties(
s, columns,
mixed_case_fields=('tenantId',),
- formatters={'tenantId': _format_project},
+ formatters=formatters,
) for s in data))