From 6b0818ac656b30f49b9d3502e230417d47277095 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Tue, 17 Aug 2021 10:25:41 +0200 Subject: [PATCH] Add href support to resource_option fixes #315 --- CHANGES/315.feature | 1 + pulpcore/cli/common/generic.py | 37 +++++++++++++++++++--- pulpcore/cli/file/repository.py | 2 ++ tests/scripts/pulp_file/test_repository.sh | 5 ++- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 CHANGES/315.feature diff --git a/CHANGES/315.feature b/CHANGES/315.feature new file mode 100644 index 000000000..62531fb43 --- /dev/null +++ b/CHANGES/315.feature @@ -0,0 +1 @@ +Added the ability to pass an href to a resource option. diff --git a/pulpcore/cli/common/generic.py b/pulpcore/cli/common/generic.py index f74b8a5e1..8f31b4a9a 100644 --- a/pulpcore/cli/common/generic.py +++ b/pulpcore/cli/common/generic.py @@ -1,5 +1,6 @@ import gettext import json +import re from typing import ( Any, Callable, @@ -248,6 +249,7 @@ def resource_option(*args: Any, **kwargs: Any) -> Callable[[_FC], _FC]: lookup_key: str = kwargs.pop("lookup_key", "name") context_table: Dict[str, Type[PulpEntityContext]] = kwargs.pop("context_table") capabilities: Optional[List[str]] = kwargs.pop("capabilities", None) + href_pattern: Optional[str] = kwargs.pop("href_pattern", None) def _option_callback( ctx: click.Context, param: click.Parameter, value: Optional[str] @@ -256,10 +258,35 @@ def _option_callback( if not value: return value - split_value = value.split(":", maxsplit=2) - while len(split_value) < 3: - split_value.insert(0, "") - plugin, resource_type, identifier = split_value + pulp_href: Optional[str] = None + entity: Optional[EntityDefinition] = None + + if value.startswith("/"): + # An href was passed + if href_pattern is None: + raise click.ClickException( + _("The option {option_name} does not support href.").format( + option_name=param.name + ) + ) + match = re.match(href_pattern, value) + if match is None: + raise click.ClickException( + _("'{value}' is not a valid href for {option_name}.").format( + value=value, option_name=param.name + ) + ) + match_groups = match.groupdict(default="") + plugin = match_groups.get("plugin", "") + resource_type = match_groups.get("resource_type", "") + pulp_href = value + else: + # A natural key identifier was passed + split_value = value.split(":", maxsplit=2) + while len(split_value) < 3: + split_value.insert(0, "") + plugin, resource_type, identifier = split_value + entity = {lookup_key: identifier} if resource_type == "": if default_type is None: @@ -288,7 +315,7 @@ def _option_callback( ) pulp_ctx = ctx.find_object(PulpContext) assert pulp_ctx is not None - entity_ctx: PulpEntityContext = context_class(pulp_ctx, entity={lookup_key: identifier}) + entity_ctx: PulpEntityContext = context_class(pulp_ctx, pulp_href=pulp_href, entity=entity) if capabilities is not None: for capability in capabilities: diff --git a/pulpcore/cli/file/repository.py b/pulpcore/cli/file/repository.py index 7cd122462..a7101d3a4 100644 --- a/pulpcore/cli/file/repository.py +++ b/pulpcore/cli/file/repository.py @@ -49,6 +49,8 @@ default_plugin="file", default_type="file", context_table={"file:file": PulpFileRemoteContext}, + href_pattern=r"^/pulp/api/v3/remotes/(?P\w+)/(?P\w+)/", + help=_("Remote used for synching in the form '[[:]:]' or by href."), ) diff --git a/tests/scripts/pulp_file/test_repository.sh b/tests/scripts/pulp_file/test_repository.sh index f6361f829..313afcb6c 100755 --- a/tests/scripts/pulp_file/test_repository.sh +++ b/tests/scripts/pulp_file/test_repository.sh @@ -26,6 +26,9 @@ expect_succ pulp file repository update --name "cli_test_file_repo" --remote "fi expect_succ pulp file repository update --name "cli_test_file_repo" --remote "file::cli:test:file:remote2" expect_succ pulp file repository show --name "cli_test_file_repo" expect_succ test "$(echo "$OUTPUT" | jq -r '.remote')" = "$REMOTE2_HREF" +expect_succ pulp file repository update --name "cli_test_file_repo" --remote "$REMOTE1_HREF" +expect_succ pulp file repository show --name "cli_test_file_repo" +expect_succ test "$(echo "$OUTPUT" | jq -r '.remote')" = "$REMOTE1_HREF" expect_succ pulp file repository update --name "cli_test_file_repo" --remote "" expect_succ pulp file repository show --name "cli_test_file_repo" expect_succ test "$(echo "$OUTPUT" | jq -r '.description')" = "null" @@ -34,7 +37,7 @@ expect_succ pulp file repository list test "$(echo "$OUTPUT" | jq -r '.|length')" != "0" expect_succ pulp file repository task list --repository "cli_test_file_repo" -test "$(echo "$OUTPUT" | jq -r '.|length')" = "5" +test "$(echo "$OUTPUT" | jq -r '.|length')" = "6" if [ "$(pulp debug has-plugin --name "file" --min-version "1.7.0")" = "true" ] then