diff --git a/setup.cfg b/setup.cfg index a9c22e63..f7d7bf6c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -59,6 +59,7 @@ openstack.container.v1 = appcontainer_image_list = zunclient.osc.v1.images:ListImage appcontainer_image_pull = zunclient.osc.v1.images:PullImage appcontainer_host_list = zunclient.osc.v1.hosts:ListHost + appcontainer_host_show = zunclient.osc.v1.hosts:ShowHost [build_sphinx] source-dir = doc/source diff --git a/zunclient/osc/v1/hosts.py b/zunclient/osc/v1/hosts.py index ce284249..6370803f 100644 --- a/zunclient/osc/v1/hosts.py +++ b/zunclient/osc/v1/hosts.py @@ -16,6 +16,11 @@ from osc_lib.command import command from osc_lib import utils +def _host_columns(host): + del host._info['links'] + return host._info.keys() + + def _get_client(obj, parsed_args): obj.log.debug("take_action(%s)" % parsed_args) return obj.app.client_manager.container @@ -61,3 +66,25 @@ class ListHost(command.Lister): columns = ('uuid', 'hostname', 'mem_total', 'cpus', 'os', 'labels') return (columns, (utils.get_item_properties(host, columns) for host in hosts)) + + +class ShowHost(command.ShowOne): + """Show a host""" + + log = logging.getLogger(__name__ + ".ShowHost") + + def get_parser(self, prog_name): + parser = super(ShowHost, self).get_parser(prog_name) + parser.add_argument( + 'host', + metavar='', + help='ID or name of the host to show.') + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + host = parsed_args.host + host = client.hosts.get(host) + columns = _host_columns(host) + + return columns, utils.get_item_properties(host, columns) diff --git a/zunclient/v1/hosts.py b/zunclient/v1/hosts.py index 873adad3..a351ec06 100644 --- a/zunclient/v1/hosts.py +++ b/zunclient/v1/hosts.py @@ -74,3 +74,9 @@ class HostManager(base.Manager): return self._list_pagination(self._path(path), "hosts", limit=limit) + + def get(self, id): + try: + return self._list(self._path(id))[0] + except IndexError: + return None diff --git a/zunclient/v1/hosts_shell.py b/zunclient/v1/hosts_shell.py index a1d6443b..467d2bd0 100644 --- a/zunclient/v1/hosts_shell.py +++ b/zunclient/v1/hosts_shell.py @@ -10,6 +10,9 @@ # License for the specific language governing permissions and limitations # under the License. +import json +import yaml + from zunclient.common import cliutils as utils from zunclient.common import utils as zun_utils @@ -42,3 +45,25 @@ def do_host_list(cs, args): utils.print_list(hosts, columns, {'versions': zun_utils.print_list_field('versions')}, sortby_index=None) + + +@utils.arg('host', + metavar='', + help='ID or name of the host to show.') +@utils.arg('-f', '--format', + metavar='', + action='store', + choices=['json', 'yaml', 'table'], + default='table', + help='Print representation of the host.' + 'The choices of the output format is json,table,yaml.' + 'Defaults to table.') +def do_host_show(cs, args): + """Show details of a host.""" + host = cs.hosts.get(args.host) + if args.format == 'json': + print(json.dumps(host._info, indent=4, sort_keys=True)) + elif args.format == 'yaml': + print(yaml.safe_dump(host._info, default_flow_style=False)) + elif args.format == 'table': + utils.print_dict(host._info)