From 613624650532232032ae54f8be78bce002de8040 Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Fri, 10 Jul 2020 11:46:05 -0400 Subject: [PATCH] CLI: Port "ara playbook show" command from ara 0.x The command is used to return details about a playbook. Change-Id: Ib35863ab436c515120aa2435ad1024a8e1f2f516 --- ara/cli/playbook.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ doc/source/cli.rst | 18 +++++++++++++++ setup.cfg | 1 + 3 files changed, 75 insertions(+) diff --git a/ara/cli/playbook.py b/ara/cli/playbook.py index 63767dd6..84711e37 100644 --- a/ara/cli/playbook.py +++ b/ara/cli/playbook.py @@ -2,8 +2,10 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) import logging +import sys from cliff.lister import Lister +from cliff.show import ShowOne from ara.cli.base import global_arguments from ara.clients.utils import get_client @@ -97,3 +99,57 @@ class PlaybookList(Lister): ) ) # fmt: on + + +class PlaybookShow(ShowOne): + """ Returns a detailed view of a specified playbook """ + + log = logging.getLogger(__name__) + + def get_parser(self, prog_name): + parser = super(PlaybookShow, self).get_parser(prog_name) + parser = global_arguments(parser) + # fmt: off + parser.add_argument( + "playbook_id", + metavar="", + help="Playbook to show", + ) + # fmt: on + return parser + + def take_action(self, args): + # TODO: Render json properly in pretty tables + if args.formatter == "table": + self.log.warn("Rendering using default table formatter, use '-f yaml' or '-f json' for improved display.") + + client = get_client( + client=args.client, + endpoint=args.server, + timeout=args.timeout, + username=args.username, + password=args.password, + verify=False if args.insecure else True, + ) + + # TODO: Improve client to be better at handling exceptions + playbook = client.get("/api/v1/playbooks/%s" % args.playbook_id) + if "detail" in playbook and playbook["detail"] == "Not found.": + self.log.error("Playbook not found: %s" % args.playbook_id) + sys.exit(1) + + playbook["report"] = "%s/playbooks/%s.html" % (args.server, args.playbook_id) + columns = ( + "id", + "report", + "status", + "path", + "started", + "ended", + "duration", + "ansible_version", + "items", + "labels", + "arguments", + ) + return (columns, ([playbook[column] for column in columns])) diff --git a/doc/source/cli.rst b/doc/source/cli.rst index 7ddd4614..7dd9b518 100644 --- a/doc/source/cli.rst +++ b/doc/source/cli.rst @@ -37,6 +37,24 @@ Get a list of playbooks and format the results as json or yaml instead of pretty ara playbook list -f json ara playbook list -f yaml +ara playbook show +----------------- + +.. command-output:: ara playbook show --help + +Show details about a playbook from a running API server and format the result in json: + +.. code-block:: bash + + ara playbook show --client http --server https://api.demo.recordsansible.org 1 -f json + +Show details about a playbook from a local installation using the default offline +API client and format the result in yaml: + +.. code-block:: bash + + ara playbook show 1 -f yaml + CLI: ara-manage (django) ======================== diff --git a/setup.cfg b/setup.cfg index aab0dfe8..67b661b9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,6 +36,7 @@ console_scripts = ara.cli = playbook list = ara.cli.playbook:PlaybookList + playbook show = ara.cli.playbook:PlaybookShow [extras] server=