diff options
| author | Zuul <zuul@review.opendev.org> | 2019-06-23 04:49:33 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2019-06-23 04:49:33 +0000 |
| commit | eada2db332caa3dc042650437a1536e589505c2b (patch) | |
| tree | 4359e107710853135e6a1166c107f88cb867ce97 /openstackclient/identity | |
| parent | f0fafec54f0d0ad5bc241d5d93c98b4d23f94dfc (diff) | |
| parent | fa5046a3dbf8cf398b9a05c68e6ee4badbb14ee7 (diff) | |
| download | python-openstackclient-eada2db332caa3dc042650437a1536e589505c2b.tar.gz | |
Merge "Use cliff formattable columns in identity commands"
Diffstat (limited to 'openstackclient/identity')
| -rw-r--r-- | openstackclient/identity/v2_0/catalog.py | 34 | ||||
| -rw-r--r-- | openstackclient/identity/v2_0/project.py | 3 | ||||
| -rw-r--r-- | openstackclient/identity/v2_0/user.py | 41 | ||||
| -rw-r--r-- | openstackclient/identity/v3/catalog.py | 24 | ||||
| -rw-r--r-- | openstackclient/identity/v3/identity_provider.py | 5 |
5 files changed, 67 insertions, 40 deletions
diff --git a/openstackclient/identity/v2_0/catalog.py b/openstackclient/identity/v2_0/catalog.py index 993bdd53..5d1e3062 100644 --- a/openstackclient/identity/v2_0/catalog.py +++ b/openstackclient/identity/v2_0/catalog.py @@ -15,6 +15,7 @@ import logging +from cliff import columns as cliff_columns from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -26,20 +27,21 @@ from openstackclient.i18n import _ LOG = logging.getLogger(__name__) -def _format_endpoints(eps=None): - if not eps: - return "" - ret = '' - for index, ep in enumerate(eps): - region = eps[index].get('region') - if region is None: - region = '<none>' - ret += region + '\n' - for endpoint_type in ['publicURL', 'internalURL', 'adminURL']: - url = eps[index].get(endpoint_type) - if url: - ret += " %s: %s\n" % (endpoint_type, url) - return ret +class EndpointsColumn(cliff_columns.FormattableColumn): + def human_readable(self): + if not self._value: + return "" + ret = '' + for ep in self._value: + region = ep.get('region') + if region is None: + region = '<none>' + ret += region + '\n' + for endpoint_type in ['publicURL', 'internalURL', 'adminURL']: + url = ep.get(endpoint_type) + if url: + ret += " %s: %s\n" % (endpoint_type, url) + return ret class ListCatalog(command.Lister): @@ -60,7 +62,7 @@ class ListCatalog(command.Lister): (utils.get_dict_properties( s, columns, formatters={ - 'Endpoints': _format_endpoints, + 'Endpoints': EndpointsColumn, }, ) for s in data)) @@ -91,7 +93,7 @@ class ShowCatalog(command.ShowOne): if (service.get('name') == parsed_args.service or service.get('type') == parsed_args.service): data = service - data['endpoints'] = _format_endpoints(data['endpoints']) + data['endpoints'] = EndpointsColumn(data['endpoints']) if 'endpoints_links' in data: data.pop('endpoints_links') break diff --git a/openstackclient/identity/v2_0/project.py b/openstackclient/identity/v2_0/project.py index 9bb5fc4d..e0018860 100644 --- a/openstackclient/identity/v2_0/project.py +++ b/openstackclient/identity/v2_0/project.py @@ -18,6 +18,7 @@ import logging from keystoneauth1 import exceptions as ks_exc +from osc_lib.cli import format_columns from osc_lib.cli import parseractions from osc_lib.command import command from osc_lib import exceptions @@ -297,7 +298,7 @@ class ShowProject(command.ShowOne): if v is not None: properties[k] = v - info['properties'] = utils.format_dict(properties) + info['properties'] = format_columns.DictColumn(properties) return zip(*sorted(six.iteritems(info))) 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)) diff --git a/openstackclient/identity/v3/catalog.py b/openstackclient/identity/v3/catalog.py index 28f4fada..59430c4c 100644 --- a/openstackclient/identity/v3/catalog.py +++ b/openstackclient/identity/v3/catalog.py @@ -15,6 +15,7 @@ import logging +from cliff import columns as cliff_columns from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -26,15 +27,16 @@ from openstackclient.i18n import _ LOG = logging.getLogger(__name__) -def _format_endpoints(eps=None): - if not eps: - return "" - ret = '' - for ep in eps: - region = ep.get('region_id') or ep.get('region') or '<none>' - ret += region + '\n' - ret += " %s: %s\n" % (ep['interface'], ep['url']) - return ret +class EndpointsColumn(cliff_columns.FormattableColumn): + def human_readable(self): + if not self._value: + return "" + ret = '' + for ep in self._value: + region = ep.get('region_id') or ep.get('region') or '<none>' + ret += region + '\n' + ret += " %s: %s\n" % (ep['interface'], ep['url']) + return ret class ListCatalog(command.Lister): @@ -55,7 +57,7 @@ class ListCatalog(command.Lister): (utils.get_dict_properties( s, columns, formatters={ - 'Endpoints': _format_endpoints, + 'Endpoints': EndpointsColumn, }, ) for s in data)) @@ -86,7 +88,7 @@ class ShowCatalog(command.ShowOne): if (service.get('name') == parsed_args.service or service.get('type') == parsed_args.service): data = dict(service) - data['endpoints'] = _format_endpoints(data['endpoints']) + data['endpoints'] = EndpointsColumn(data['endpoints']) if 'links' in data: data.pop('links') break diff --git a/openstackclient/identity/v3/identity_provider.py b/openstackclient/identity/v3/identity_provider.py index d8951d31..b3315182 100644 --- a/openstackclient/identity/v3/identity_provider.py +++ b/openstackclient/identity/v3/identity_provider.py @@ -15,6 +15,7 @@ import logging +from osc_lib.cli import format_columns from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -103,7 +104,7 @@ class CreateIdentityProvider(command.ShowOne): enabled=parsed_args.enabled) idp._info.pop('links', None) - remote_ids = utils.format_list(idp._info.pop('remote_ids', [])) + remote_ids = format_columns.ListColumn(idp._info.pop('remote_ids', [])) idp._info['remote_ids'] = remote_ids return zip(*sorted(six.iteritems(idp._info))) @@ -245,6 +246,6 @@ class ShowIdentityProvider(command.ShowOne): id=parsed_args.identity_provider) idp._info.pop('links', None) - remote_ids = utils.format_list(idp._info.pop('remote_ids', [])) + remote_ids = format_columns.ListColumn(idp._info.pop('remote_ids', [])) idp._info['remote_ids'] = remote_ids return zip(*sorted(six.iteritems(idp._info))) |
