Skip to content

Commit

Permalink
Add pulp_rpm Alternate Content Source commands
Browse files Browse the repository at this point in the history
fixes pulp#378
  • Loading branch information
David Davis committed Oct 4, 2021
1 parent b37b71e commit 1befdba
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/378.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added pulp_rpm Alternate Content Source commands.
5 changes: 5 additions & 0 deletions pulpcore/cli/rpm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
126 changes: 126 additions & 0 deletions pulpcore/cli/rpm/acs.py
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)
19 changes: 19 additions & 0 deletions pulpcore/cli/rpm/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gettext
from typing import Any

from pulpcore.cli.common.context import (
EntityDefinition,
Expand All @@ -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")
Expand Down
53 changes: 53 additions & 0 deletions tests/scripts/pulp_rpm/test_acs.sh
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

0 comments on commit 1befdba

Please sign in to comment.