diff --git a/doc/source/command-objects/catalog.rst b/doc/source/command-objects/catalog.rst index 89db95d5d4..4319fb111f 100644 --- a/doc/source/command-objects/catalog.rst +++ b/doc/source/command-objects/catalog.rst @@ -2,7 +2,7 @@ catalog ======= -Identity v2 +Identity v2, v3 catalog list ------------ diff --git a/openstackclient/identity/v3/catalog.py b/openstackclient/identity/v3/catalog.py index 0971366155..1899f25e63 100644 --- a/openstackclient/identity/v3/catalog.py +++ b/openstackclient/identity/v3/catalog.py @@ -16,8 +16,11 @@ import logging from cliff import lister +from cliff import show +import six from openstackclient.common import utils +from openstackclient.i18n import _ # noqa def _format_endpoints(eps=None): @@ -54,3 +57,44 @@ class ListCatalog(lister.Lister): 'Endpoints': _format_endpoints, }, ) for s in data)) + + +class ShowCatalog(show.ShowOne): + """Display service catalog details""" + + log = logging.getLogger(__name__ + '.ShowCatalog') + + def get_parser(self, prog_name): + parser = super(ShowCatalog, self).get_parser(prog_name) + parser.add_argument( + 'service', + metavar='', + help=_('Service to display (type or name)'), + ) + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)', parsed_args) + + # This is ugly because if auth hasn't happened yet we need + # to trigger it here. + sc = self.app.client_manager.session.auth.get_auth_ref( + self.app.client_manager.session, + ).service_catalog + + data = None + for service in sc.get_data(): + if (service.get('name') == parsed_args.service or + service.get('type') == parsed_args.service): + data = dict(service) + data['endpoints'] = _format_endpoints(data['endpoints']) + if 'links' in data: + data.pop('links') + break + + if not data: + self.app.log.error('service %s not found\n' % + parsed_args.service) + return ([], []) + + return zip(*sorted(six.iteritems(data))) diff --git a/openstackclient/tests/identity/v3/test_catalog.py b/openstackclient/tests/identity/v3/test_catalog.py index 9adda86302..6bb962de09 100644 --- a/openstackclient/tests/identity/v3/test_catalog.py +++ b/openstackclient/tests/identity/v3/test_catalog.py @@ -82,3 +82,37 @@ class TestCatalogList(TestCatalog): '\n internal: https://internal.example.com\n', ), ) self.assertEqual(datalist, tuple(data)) + + +class TestCatalogShow(TestCatalog): + + def setUp(self): + super(TestCatalogShow, self).setUp() + + # Get the command object to test + self.cmd = catalog.ShowCatalog(self.app, None) + + def test_catalog_show(self): + arglist = [ + 'compute', + ] + verifylist = [ + ('service', 'compute'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + self.sc_mock.service_catalog.get_data.assert_called_with() + + collist = ('endpoints', 'id', 'name', 'type') + self.assertEqual(collist, columns) + datalist = ( + 'onlyone\n public: https://public.example.com\nonlyone\n' + ' admin: https://admin.example.com\n' + '\n internal: https://internal.example.com\n', + 'qwertyuiop', + 'supernova', + 'compute', + ) + self.assertEqual(datalist, data) diff --git a/setup.cfg b/setup.cfg index 13fc3d3f23..78da832cb0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -186,6 +186,7 @@ openstack.identity.v3 = access_token_create = openstackclient.identity.v3.token:CreateAccessToken catalog_list = openstackclient.identity.v3.catalog:ListCatalog + catalog_show = openstackclient.identity.v3.catalog:ShowCatalog consumer_create = openstackclient.identity.v3.consumer:CreateConsumer consumer_delete = openstackclient.identity.v3.consumer:DeleteConsumer