From b0c5ef956d6bdf2a29f11ed0c95b1501c424c9da Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Fri, 12 Nov 2021 20:43:04 +0100 Subject: [PATCH] Call command [noissue] --- pulpcore/cli/common/context.py | 11 +++++++++-- pulpcore/cli/common/debug.py | 35 ++++++++++++++++++++++++++++++++-- pulpcore/cli/common/openapi.py | 9 ++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/pulpcore/cli/common/context.py b/pulpcore/cli/common/context.py index 7b542189e..e15027e35 100644 --- a/pulpcore/cli/common/context.py +++ b/pulpcore/cli/common/context.py @@ -123,7 +123,14 @@ def output_result(self, result: Any) -> None: _("Format '{format}' not implemented.").format(format=self.format) ) - def call(self, operation_id: str, non_blocking: bool = False, *args: Any, **kwargs: Any) -> Any: + def call( + self, + operation_id: str, + non_blocking: bool = False, + parameters: Optional[Dict[str, Any]] = None, + body: Optional[Dict[str, Any]] = None, + uploads: Optional[Dict[str, bytes]] = None, + ) -> Any: """ Perform an API call for operation_id. Wait for triggered tasks to finish if not background. @@ -131,7 +138,7 @@ def call(self, operation_id: str, non_blocking: bool = False, *args: Any, **kwar If non_blocking, returns unfinished tasks. """ try: - result = self.api.call(operation_id, *args, **kwargs) + result = self.api.call(operation_id, parameters=parameters, body=body, uploads=uploads) except OpenAPIError as e: raise click.ClickException(str(e)) except HTTPError as e: diff --git a/pulpcore/cli/common/debug.py b/pulpcore/cli/common/debug.py index 2487dd9a6..6d5c39369 100644 --- a/pulpcore/cli/common/debug.py +++ b/pulpcore/cli/common/debug.py @@ -1,10 +1,11 @@ import gettext import sys -from typing import Optional +from typing import IO, Any, Dict, Iterable, Optional import click from pulpcore.cli.common.context import PluginRequirement, PulpContext, pass_pulp_context +from pulpcore.cli.common.generic import load_json_callback _ = gettext.gettext @@ -66,7 +67,7 @@ def schema(pulp_ctx: PulpContext) -> None: @openapi_group.command() -@click.option("--id", "operation_id", required=True) +@click.option("--id", "operation_id", required=True, help=_("Operation ID in openapi schema")) @pass_pulp_context def operation(pulp_ctx: PulpContext, operation_id: str) -> None: """ @@ -91,4 +92,34 @@ def operation(pulp_ctx: PulpContext, operation_id: str) -> None: @openapi_group.command() @pass_pulp_context def operation_ids(pulp_ctx: PulpContext) -> None: + """ + Print a list of available operation-ids. + """ pulp_ctx.output_result(list(pulp_ctx.api.operations.keys())) + + +@openapi_group.command() +@click.option("--id", "operation_id", required=True, help=_("Operation ID in openapi schema")) +@click.option("--parameter", "parameters", multiple=True) +@click.option("--body", callback=load_json_callback) +@click.option("--upload", "uploads", type=click.File("rb"), multiple=True) +@pass_pulp_context +def call( + pulp_ctx: PulpContext, + operation_id: str, + parameters: Iterable[str], + body: Any, + uploads: Iterable[IO[bytes]], +) -> None: + """ + Make a REST call by operation-id. + + WARNING: Danger ahead! + """ + try: + params: Dict[str, str] = dict(parameter.partition("=")[::2] for parameter in parameters) + except ValueError: + raise click.ClickException("Parameters must be in the form =.") + uploads_dict: Dict[str, bytes] = {file.name: file.read() for file in uploads} + result = pulp_ctx.call(operation_id, parameters=params, body=body, uploads=uploads_dict) + pulp_ctx.output_result(result) diff --git a/pulpcore/cli/common/openapi.py b/pulpcore/cli/common/openapi.py index 2c35fd900..712fc9fd3 100644 --- a/pulpcore/cli/common/openapi.py +++ b/pulpcore/cli/common/openapi.py @@ -166,9 +166,12 @@ def render_request( uploads: Optional[Dict[str, bytes]] = None, ) -> requests.PreparedRequest: method_spec = path_spec[method] - content_types: List[str] = ( - list(method_spec["requestBody"]["content"].keys()) if body or uploads else [] - ) + try: + content_types: List[str] = ( + list(method_spec["requestBody"]["content"].keys()) if body or uploads else [] + ) + except KeyError: + raise OpenAPIError(_("This operation does not expect a request body.")) data: Optional[Dict[str, Any]] = None json: Optional[Dict[str, Any]] = None