From 1befdba579cfb2187d13a05e46c1f4f667245ab5 Mon Sep 17 00:00:00 2001 From: David Davis Date: Thu, 23 Sep 2021 13:39:13 -0400 Subject: [PATCH] Add pulp_rpm Alternate Content Source commands fixes #378 --- CHANGES/378.feature | 1 + pulpcore/cli/rpm/__init__.py | 5 ++ pulpcore/cli/rpm/acs.py | 126 +++++++++++++++++++++++++++++ pulpcore/cli/rpm/context.py | 19 +++++ tests/scripts/pulp_rpm/test_acs.sh | 53 ++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 CHANGES/378.feature create mode 100644 pulpcore/cli/rpm/acs.py create mode 100755 tests/scripts/pulp_rpm/test_acs.sh diff --git a/CHANGES/378.feature b/CHANGES/378.feature new file mode 100644 index 000000000..07375ae47 --- /dev/null +++ b/CHANGES/378.feature @@ -0,0 +1 @@ +Added pulp_rpm Alternate Content Source commands. diff --git a/pulpcore/cli/rpm/__init__.py b/pulpcore/cli/rpm/__init__.py index 53b7beda0..ab15efa32 100644 --- a/pulpcore/cli/rpm/__init__.py +++ b/pulpcore/cli/rpm/__init__.py @@ -2,7 +2,11 @@ from pulpcore.cli.common import main from pulpcore.cli.common.context import PluginRequirement, PulpContext, pass_pulp_context +<<<<<<< HEAD from pulpcore.cli.rpm.content import content +======= +from pulpcore.cli.rpm.acs import acs +>>>>>>> Add pulp_rpm Alternate Content Source commands from pulpcore.cli.rpm.distribution import distribution from pulpcore.cli.rpm.publication import publication from pulpcore.cli.rpm.remote import remote @@ -22,3 +26,4 @@ def rpm(pulp_ctx: PulpContext) -> None: rpm.add_command(publication) rpm.add_command(distribution) rpm.add_command(content) +rpm.add_command(acs) diff --git a/pulpcore/cli/rpm/acs.py b/pulpcore/cli/rpm/acs.py new file mode 100644 index 000000000..84e294c73 --- /dev/null +++ b/pulpcore/cli/rpm/acs.py @@ -0,0 +1,126 @@ +import gettext +from typing import Iterable + +import click + +from pulpcore.cli.common.context import ( + PluginRequirement, + PulpContext, + PulpRemoteContext, + pass_entity_context, + pass_pulp_context, +) +from pulpcore.cli.common.generic import ( + create_command, + destroy_command, + href_option, + list_command, + name_option, + resource_option, + show_command, + update_command, +) +from pulpcore.cli.rpm.context import PulpRpmACSContext, PulpRpmRemoteContext + +_ = gettext.gettext + + +path_option = click.option( + "--path", "paths", multiple=True, help=_("path to add to ACS; can be specified multiple times.") +) + + +@click.group() +@click.option( + "-t", + "--type", + "acs_type", + type=click.Choice(["rpm"], case_sensitive=False), + default="rpm", +) +@pass_pulp_context +@click.pass_context +def acs(ctx: click.Context, pulp_ctx: PulpContext, acs_type: str) -> None: + pulp_ctx.needs_plugin(PluginRequirement("rpm", "3.16.0.dev")) + if acs_type == "rpm": + ctx.obj = PulpRpmACSContext(pulp_ctx) + else: + raise NotImplementedError() + + +@acs.group() +def path() -> None: + """Manage paths for an ACS.""" + pass + + +@path.command() +@name_option +@href_option +@path_option +@pass_entity_context +def add(acs_ctx: PulpRpmACSContext, paths: Iterable[str]) -> None: + """Add path(s) to an existing ACS.""" + paths = set(paths) + href = acs_ctx.entity["pulp_href"] + existing_paths = set(acs_ctx.show(href)["paths"]) + + for path in paths: + if path in existing_paths: + raise click.ClickException(_("ACS already has path '{}'.").format(path)) + + if existing_paths != {""}: + paths = set.union(existing_paths, paths) + acs_ctx.update(href, body={"paths": list(paths)}) + + +@path.command() +@name_option +@href_option +@path_option +@pass_entity_context +def remove(acs_ctx: PulpRpmACSContext, paths: Iterable[str]) -> None: + """Remove path(s) from an existing ACS.""" + paths = set(paths) + href = acs_ctx.entity["pulp_href"] + existing_paths = set(acs_ctx.show(href)["paths"]) + + if paths - existing_paths: + missing_paths = paths - existing_paths + raise click.ClickException(_("ACS does not have path(s): {}.").format(missing_paths)) + + paths = existing_paths - paths + acs_ctx.update(href, body={"paths": list(paths)}) + + +remote_option = resource_option( + "--remote", + default_plugin="rpm", + default_type="rpm", + context_table={"rpm:rpm": PulpRpmRemoteContext}, + href_pattern=PulpRemoteContext.HREF_PATTERN, + help=_("Remote to attach to ACS in the form '[[:]:]' or by href."), +) +path_option = click.option( + "--path", + "paths", + multiple=True, +) +lookup_options = [href_option, name_option] +update_options = [remote_option] +create_options = update_options + [click.option("--name", required=True), path_option] + +acs.add_command(list_command()) +acs.add_command(show_command(decorators=lookup_options)) +acs.add_command(create_command(decorators=create_options)) +acs.add_command(update_command(decorators=lookup_options + update_options)) +acs.add_command(destroy_command(decorators=lookup_options)) + + +@acs.command() +@pass_entity_context +@pass_pulp_context +@href_option +@name_option +def refresh(pulp_ctx: PulpContext, acs_ctx: PulpRpmACSContext) -> None: + acs_ctx.refresh(acs_ctx.pulp_href) diff --git a/pulpcore/cli/rpm/context.py b/pulpcore/cli/rpm/context.py index 2c53d9989..37984c84f 100644 --- a/pulpcore/cli/rpm/context.py +++ b/pulpcore/cli/rpm/context.py @@ -1,4 +1,5 @@ import gettext +from typing import Any from pulpcore.cli.common.context import ( EntityDefinition, @@ -14,6 +15,24 @@ _ = gettext.gettext +class PulpRpmACSContext(PulpEntityContext): + ENTITY = _("rpm ACS") + ENTITIES = _("rpm ACSes") + HREF = "rpm_rpm_alternate_content_source_href" + LIST_ID = "acs_rpm_rpm_list" + READ_ID = "acs_rpm_rpm_read" + CREATE_ID = "acs_rpm_rpm_create" + UPDATE_ID = "acs_rpm_rpm_partial_update" + DELETE_ID = "acs_rpm_rpm_delete" + REFRESH_ID = "acs_rpm_rpm_refresh" + + def refresh(self, href: str) -> Any: + return self.pulp_ctx.call( + self.REFRESH_ID, + parameters={self.HREF: href}, + ) + + class PulpRpmDistributionContext(PulpEntityContext): ENTITY = _("rpm distribution") ENTITIES = _("rpm distributions") diff --git a/tests/scripts/pulp_rpm/test_acs.sh b/tests/scripts/pulp_rpm/test_acs.sh new file mode 100755 index 000000000..f9c990b12 --- /dev/null +++ b/tests/scripts/pulp_rpm/test_acs.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# shellcheck source=tests/scripts/config.source +. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source + +pulp debug has-plugin --name "rpm" --min-version "3.16.0.dev" || exit 3 + +acs_remote="cli_test_rpm_acs_remote" +acs="cli_test_acs" + +cleanup() { + pulp rpm acs destroy --name $acs || true + pulp rpm remote destroy --name $acs_remote || true + pulp rpm repository destroy --name "cli-repo-metadata-only" || true + pulp rpm remote destroy --name "cli-remote-metadata-only" || true +} +trap cleanup EXIT + +cleanup + +expect_succ pulp rpm remote create --name $acs_remote --url "$PULP_FIXTURES_URL" --policy "on_demand" + +expect_succ pulp rpm acs create --name $acs --remote $acs_remote --path "rpm-unsigned/" --path "rpm-richnweak-deps/" +expect_succ pulp rpm acs list +test "$(echo "$OUTPUT" | jq -r length)" -ge 1 +expect_succ pulp rpm acs show --name $acs +test "$(echo "$OUTPUT" | jq ".paths | length")" -eq 2 + +# manipulate paths +expect_succ pulp rpm acs path add --name $acs --path "rpm-invalid/" +expect_succ pulp rpm acs show --name $acs +test "$(echo "$OUTPUT" | jq ".paths | length")" -eq 3 +expect_succ pulp rpm acs path remove --name $acs --path "rpm-invalid/" +expect_succ pulp rpm acs show --name $acs +test "$(echo "$OUTPUT" | jq ".paths | length")" -eq 2 + +# test refresh +expect_succ pulp rpm acs refresh --name $acs +task_group=$(echo "$ERROUTPUT" | grep -E -o "/pulp/api/v3/task-groups/[-[:xdigit:]]*/") +expect_succ pulp task-group show --href "$task_group" +test "$(echo "$OUTPUT" | jq ".tasks | length")" -eq 2 + +# create a remote with metadata only and sync it +expect_succ pulp rpm remote create --name "cli-remote-metadata-only" --url "$PULP_FIXTURES_URL/rpm-unsigned-meta-only/" +remote_href="$(echo "$OUTPUT" | jq -r ".pulp_href")" +expect_succ pulp rpm repository create --name "cli-repo-metadata-only" --remote "$remote_href" +expect_succ pulp rpm repository sync --name "cli-repo-metadata-only" + +# test refresh with bad paths +expect_succ pulp rpm acs path add --name $acs --path "bad-path/" +expect_fail pulp rpm acs refresh --name $acs + +expect_succ pulp rpm acs destroy --name $acs