Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content list to container push repositories #918

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/600.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the content list commands to container push repositories.
43 changes: 27 additions & 16 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion pulpcore/cli/container/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
)

Expand Down
7 changes: 7 additions & 0 deletions tests/scripts/pulp_container/test_content.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 --type push destroy --name "cli_test_container_push_repository" || true
pulp container remote destroy --name "cli_test_container_remote" || true
pulp orphan cleanup || true
}
Expand All @@ -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 --type push create --name "cli_test_container_push_repository"

# Check each content list
expect_succ pulp container content -t blob list
Expand Down Expand Up @@ -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"
Loading