Skip to content

Commit 9363dc3

Browse files
committed
Add content list to container push repositories
fixes #600
1 parent 8437376 commit 9363dc3

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

CHANGES/600.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added the content list commands to container push repositories.

pulpcore/cli/common/generic.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ def float_or_empty(value: str) -> t.Union[str, float]:
267267
# Custom classes for commands and parameters
268268

269269

270+
def _check_allowed_contexts(
271+
ctx: click.Context, allowed_contexts: t.Optional[t.Tuple[t.Type[PulpEntityContext]]]
272+
) -> bool:
273+
if allowed_contexts is None:
274+
return True
275+
for ctx_type in allowed_contexts:
276+
if ctx.find_object(ctx_type) is not None:
277+
return True
278+
return False
279+
280+
270281
class PulpCommand(click.Command):
271282
def __init__(
272283
self,
@@ -308,10 +319,10 @@ def get_params(self, ctx: click.Context) -> t.List[click.Parameter]:
308319
params = super().get_params(ctx)
309320
new_params: t.List[click.Parameter] = []
310321
for param in params:
311-
if isinstance(param, PulpOption):
312-
if param.allowed_with_contexts is not None:
313-
if not isinstance(ctx.obj, param.allowed_with_contexts):
314-
continue
322+
if isinstance(param, PulpOption) and not _check_allowed_contexts(
323+
ctx, param.allowed_with_contexts
324+
):
325+
continue
315326
new_params.append(param)
316327
return new_params
317328

@@ -323,10 +334,8 @@ class PulpGroup(PulpCommand, click.Group):
323334
def get_command(self, ctx: click.Context, cmd_name: str) -> t.Optional[click.Command]:
324335
# Overwriting this removes the command from the help message and from being callable
325336
cmd = super().get_command(ctx, cmd_name)
326-
if (
327-
isinstance(cmd, (PulpCommand, PulpGroup))
328-
and cmd.allowed_with_contexts is not None
329-
and not isinstance(ctx.obj, cmd.allowed_with_contexts)
337+
if isinstance(cmd, (PulpCommand, PulpGroup)) and not _check_allowed_contexts(
338+
ctx, cmd.allowed_with_contexts
330339
):
331340
raise IncompatibleContext(
332341
_("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]:
337346
commands_filtered_by_context = []
338347

339348
for name, cmd in self.commands.items():
340-
if (
341-
isinstance(cmd, (PulpCommand, PulpGroup))
342-
and cmd.allowed_with_contexts is not None
343-
and not isinstance(ctx.obj, cmd.allowed_with_contexts)
349+
if isinstance(cmd, (PulpCommand, PulpGroup)) and not _check_allowed_contexts(
350+
ctx, cmd.allowed_with_contexts
344351
):
345352
continue
346353
commands_filtered_by_context.append(name)
@@ -1530,6 +1537,10 @@ def repository_content_command(**kwargs: t.Any) -> click.Group:
15301537
"""A factory that creates a repository content command group."""
15311538

15321539
content_contexts = kwargs.pop("contexts", {})
1540+
list_kwargs = kwargs.pop("list_kwargs", {})
1541+
add_kwargs = kwargs.pop("add_kwargs", {})
1542+
remove_kwargs = kwargs.pop("remove_kwargs", {})
1543+
modify_kwargs = kwargs.pop("modify_kwargs", {})
15331544

15341545
def version_callback(
15351546
ctx: click.Context, param: click.Parameter, value: t.Optional[int]
@@ -1540,7 +1551,7 @@ def version_callback(
15401551

15411552
# This is a mypy bug getting confused with positional args
15421553
# https://github.com/python/mypy/issues/15037
1543-
@pulp_command("list") # type: ignore [arg-type]
1554+
@pulp_command("list", **list_kwargs) # type: ignore [arg-type]
15441555
@click.option("--all-types", is_flag=True)
15451556
@limit_option
15461557
@offset_option
@@ -1563,7 +1574,7 @@ def content_list(
15631574
result = content_ctx.list(limit=limit, offset=offset, parameters=parameters)
15641575
pulp_ctx.output_result(result)
15651576

1566-
@pulp_command("add")
1577+
@pulp_command("add", **add_kwargs)
15671578
@repository_option
15681579
@click.option("--base-version", type=int, callback=version_callback)
15691580
@pass_content_context
@@ -1574,7 +1585,7 @@ def content_add(
15741585
repo_ctx = base_version.repository_ctx
15751586
repo_ctx.modify(add_content=[content_ctx.pulp_href], base_version=base_version.pulp_href)
15761587

1577-
@pulp_command("remove")
1588+
@pulp_command("remove", **remove_kwargs)
15781589
@click.option("--all", is_flag=True, help=_("Remove all content from repository version"))
15791590
@repository_option
15801591
@click.option("--base-version", type=int, callback=version_callback)
@@ -1588,7 +1599,7 @@ def content_remove(
15881599
remove_content = ["*" if all else content_ctx.pulp_href]
15891600
repo_ctx.modify(remove_content=remove_content, base_version=base_version.pulp_href)
15901601

1591-
@pulp_command("modify")
1602+
@pulp_command("modify", **modify_kwargs)
15921603
@repository_option
15931604
@click.option("--base-version", type=int, callback=version_callback)
15941605
def content_modify(

pulpcore/cli/container/repository.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def repository() -> None:
135135
contexts=contexts,
136136
add_decorators=show_options,
137137
remove_decorators=show_options,
138-
allowed_with_contexts=container_context,
138+
add_kwargs={"allowed_with_contexts": container_context},
139+
remove_kwargs={"allowed_with_contexts": container_context},
140+
modify_kwargs={"allowed_with_contexts": container_context},
139141
)
140142
)
141143

tests/scripts/pulp_container/test_content.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pulp debug has-plugin --name "container" || exit 23
77

88
cleanup() {
99
pulp container repository destroy --name "cli_test_container_repository" || true
10+
pulp container repository destroy --name "cli_test_container_push_repository" || true
1011
pulp container remote destroy --name "cli_test_container_remote" || true
1112
pulp orphan cleanup || true
1213
}
@@ -16,6 +17,7 @@ trap cleanup EXIT
1617
pulp container remote create --name "cli_test_container_remote" --url "$CONTAINER_REMOTE_URL" --upstream-name "$CONTAINER_IMAGE"
1718
pulp container repository create --name "cli_test_container_repository"
1819
pulp container repository sync --name "cli_test_container_repository" --remote "cli_test_container_remote"
20+
pulp container repository create --name "cli_test_container_push_repository"
1921

2022
# Check each content list
2123
expect_succ pulp container content -t blob list
@@ -59,3 +61,8 @@ expect_succ pulp container repository content --type "tag" remove --repository "
5961
expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$blob_href"
6062
expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$manifest_href"
6163
expect_succ pulp container repository content add --repository "cli_test_container_repository" --href "$tag_href"
64+
65+
expect_succ pulp container repository -t push content list --repository "cli_test_container_push_repository" --all-types
66+
expect_succ pulp container repository -t push content --type "tag" list --repository "cli_test_container_push_repository"
67+
expect_succ pulp container repository -t push content --type "manifest" list --repository "cli_test_container_push_repository"
68+
expect_succ pulp container repository -t push content --type "blob" list --repository "cli_test_container_push_repository"

0 commit comments

Comments
 (0)