From 9363dc39ead23a7eafa7323c758dd76e06270062 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Fri, 15 Mar 2024 14:52:25 +0100 Subject: [PATCH] Add content list to container push repositories fixes #600 --- CHANGES/600.feature | 1 + pulpcore/cli/common/generic.py | 43 ++++++++++++-------- pulpcore/cli/container/repository.py | 4 +- tests/scripts/pulp_container/test_content.sh | 7 ++++ 4 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 CHANGES/600.feature diff --git a/CHANGES/600.feature b/CHANGES/600.feature new file mode 100644 index 000000000..48a80f551 --- /dev/null +++ b/CHANGES/600.feature @@ -0,0 +1 @@ +Added the content list commands to container push repositories. diff --git a/pulpcore/cli/common/generic.py b/pulpcore/cli/common/generic.py index 7416ad2b9..f1351a98e 100644 --- a/pulpcore/cli/common/generic.py +++ b/pulpcore/cli/common/generic.py @@ -267,6 +267,17 @@ def float_or_empty(value: str) -> t.Union[str, float]: # Custom classes for commands and parameters +def _check_allowed_contexts( + ctx: click.Context, allowed_contexts: t.Optional[t.Tuple[t.Type[PulpEntityContext]]] +) -> bool: + if allowed_contexts is None: + return True + for ctx_type in allowed_contexts: + if ctx.find_object(ctx_type) is not None: + return True + return False + + class PulpCommand(click.Command): def __init__( self, @@ -308,10 +319,10 @@ def get_params(self, ctx: click.Context) -> t.List[click.Parameter]: params = super().get_params(ctx) new_params: t.List[click.Parameter] = [] for param in params: - if isinstance(param, PulpOption): - if param.allowed_with_contexts is not None: - if not isinstance(ctx.obj, param.allowed_with_contexts): - continue + if isinstance(param, PulpOption) and not _check_allowed_contexts( + ctx, param.allowed_with_contexts + ): + continue new_params.append(param) return new_params @@ -323,10 +334,8 @@ class PulpGroup(PulpCommand, click.Group): def get_command(self, ctx: click.Context, cmd_name: str) -> t.Optional[click.Command]: # Overwriting this removes the command from the help message and from being callable cmd = super().get_command(ctx, cmd_name) - if ( - isinstance(cmd, (PulpCommand, PulpGroup)) - and cmd.allowed_with_contexts is not None - and not isinstance(ctx.obj, cmd.allowed_with_contexts) + if isinstance(cmd, (PulpCommand, PulpGroup)) and not _check_allowed_contexts( + ctx, cmd.allowed_with_contexts ): raise IncompatibleContext( _("The subcommand '{name}' is not available in this context.").format(name=cmd.name) @@ -337,10 +346,8 @@ def list_commands(self, ctx: click.Context) -> t.List[str]: commands_filtered_by_context = [] for name, cmd in self.commands.items(): - if ( - isinstance(cmd, (PulpCommand, PulpGroup)) - and cmd.allowed_with_contexts is not None - and not isinstance(ctx.obj, cmd.allowed_with_contexts) + if isinstance(cmd, (PulpCommand, PulpGroup)) and not _check_allowed_contexts( + ctx, cmd.allowed_with_contexts ): continue commands_filtered_by_context.append(name) @@ -1530,6 +1537,10 @@ def repository_content_command(**kwargs: t.Any) -> click.Group: """A factory that creates a repository content command group.""" content_contexts = kwargs.pop("contexts", {}) + list_kwargs = kwargs.pop("list_kwargs", {}) + add_kwargs = kwargs.pop("add_kwargs", {}) + remove_kwargs = kwargs.pop("remove_kwargs", {}) + modify_kwargs = kwargs.pop("modify_kwargs", {}) def version_callback( ctx: click.Context, param: click.Parameter, value: t.Optional[int] @@ -1540,7 +1551,7 @@ def version_callback( # This is a mypy bug getting confused with positional args # https://github.com/python/mypy/issues/15037 - @pulp_command("list") # type: ignore [arg-type] + @pulp_command("list", **list_kwargs) # type: ignore [arg-type] @click.option("--all-types", is_flag=True) @limit_option @offset_option @@ -1563,7 +1574,7 @@ def content_list( result = content_ctx.list(limit=limit, offset=offset, parameters=parameters) pulp_ctx.output_result(result) - @pulp_command("add") + @pulp_command("add", **add_kwargs) @repository_option @click.option("--base-version", type=int, callback=version_callback) @pass_content_context @@ -1574,7 +1585,7 @@ def content_add( repo_ctx = base_version.repository_ctx repo_ctx.modify(add_content=[content_ctx.pulp_href], base_version=base_version.pulp_href) - @pulp_command("remove") + @pulp_command("remove", **remove_kwargs) @click.option("--all", is_flag=True, help=_("Remove all content from repository version")) @repository_option @click.option("--base-version", type=int, callback=version_callback) @@ -1588,7 +1599,7 @@ def content_remove( remove_content = ["*" if all else content_ctx.pulp_href] repo_ctx.modify(remove_content=remove_content, base_version=base_version.pulp_href) - @pulp_command("modify") + @pulp_command("modify", **modify_kwargs) @repository_option @click.option("--base-version", type=int, callback=version_callback) def content_modify( diff --git a/pulpcore/cli/container/repository.py b/pulpcore/cli/container/repository.py index 992b1e7e4..3d264aca8 100644 --- a/pulpcore/cli/container/repository.py +++ b/pulpcore/cli/container/repository.py @@ -135,7 +135,9 @@ def repository() -> None: contexts=contexts, add_decorators=show_options, remove_decorators=show_options, - allowed_with_contexts=container_context, + add_kwargs={"allowed_with_contexts": container_context}, + remove_kwargs={"allowed_with_contexts": container_context}, + modify_kwargs={"allowed_with_contexts": container_context}, ) ) diff --git a/tests/scripts/pulp_container/test_content.sh b/tests/scripts/pulp_container/test_content.sh index 8d27a975c..cdafd9ff6 100755 --- a/tests/scripts/pulp_container/test_content.sh +++ b/tests/scripts/pulp_container/test_content.sh @@ -7,6 +7,7 @@ pulp debug has-plugin --name "container" || exit 23 cleanup() { pulp container repository destroy --name "cli_test_container_repository" || true + pulp container repository destroy --name "cli_test_container_push_repository" || true pulp container remote destroy --name "cli_test_container_remote" || true pulp orphan cleanup || true } @@ -16,6 +17,7 @@ trap cleanup EXIT pulp container remote create --name "cli_test_container_remote" --url "$CONTAINER_REMOTE_URL" --upstream-name "$CONTAINER_IMAGE" pulp container repository create --name "cli_test_container_repository" pulp container repository sync --name "cli_test_container_repository" --remote "cli_test_container_remote" +pulp container repository create --name "cli_test_container_push_repository" # Check each content list expect_succ pulp container content -t blob list @@ -59,3 +61,8 @@ expect_succ pulp container repository content --type "tag" remove --repository " expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$blob_href" expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$manifest_href" expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$tag_href" + +expect_succ pulp container repository -t push content list --repository "cli_test_container_push_repository" --all-types +expect_succ pulp container repository -t push content --type "tag" list --repository "cli_test_container_push_repository" +expect_succ pulp container repository -t push content --type "manifest" list --repository "cli_test_container_push_repository" +expect_succ pulp container repository -t push content --type "blob" list --repository "cli_test_container_push_repository"