forked from pulp/pulp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pulp_rpm Alternate Content Source commands
fixes pulp#378
- Loading branch information
David Davis
committed
Oct 4, 2021
1 parent
b37b71e
commit 1befdba
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added pulp_rpm Alternate Content Source commands. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 '[[<plugin>:]<resource_type>:]<name>' 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |