Skip to content

Commit

Permalink
Fleshed out rpm-content types.
Browse files Browse the repository at this point in the history
closes #505.
  • Loading branch information
ggainey committed Jun 8, 2022
1 parent 819fa20 commit 8a74968
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 80 deletions.
1 change: 1 addition & 0 deletions CHANGES/505.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extended "rpm content" to cover all of the RPM content-types.
229 changes: 182 additions & 47 deletions pulpcore/cli/rpm/content.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import IO, Optional, Union
from typing import Any, Optional, Union

import click

Expand All @@ -14,11 +14,24 @@
href_option,
list_command,
pulp_group,
pulp_option,
show_command,
type_option,
)
from pulpcore.cli.common.i18n import get_translation
from pulpcore.cli.core.context import PulpArtifactContext
from pulpcore.cli.rpm.context import PulpRpmPackageContext
from pulpcore.cli.rpm.context import (
PulpRpmAdvisoryContext,
PulpRpmDistributionTreeContext,
PulpRpmModulemdContext,
PulpRpmModulemdDefaultsContext,
PulpRpmPackageCategoryContext,
PulpRpmPackageContext,
PulpRpmPackageEnvironmentContext,
PulpRpmPackageGroupContext,
PulpRpmPackageLangpacksContext,
PulpRpmRepoMetadataFileContext,
)

translation = get_translation(__name__)
_ = translation.gettext
Expand Down Expand Up @@ -52,74 +65,196 @@ def _sha256_artifact_callback(


@pulp_group()
@click.option(
"-t",
"--type",
"content_type",
type=click.Choice(["package"], case_sensitive=False),
@type_option(
choices={
"package": PulpRpmPackageContext,
"advisory": PulpRpmAdvisoryContext,
"distribution_tree": PulpRpmDistributionTreeContext,
"modulemd_defaults": PulpRpmModulemdDefaultsContext,
"modulemd": PulpRpmModulemdContext,
"package_category": PulpRpmPackageCategoryContext,
"package_environment": PulpRpmPackageEnvironmentContext,
"package_group": PulpRpmPackageGroupContext,
"package_langpack": PulpRpmPackageLangpacksContext,
"repo_metadata_file": PulpRpmRepoMetadataFileContext,
},
default="package",
case_sensitive=False,
)
@pass_pulp_context
@click.pass_context
def content(ctx: click.Context, pulp_ctx: PulpContext, content_type: str) -> None:
if content_type == "package":
ctx.obj = PulpRpmPackageContext(pulp_ctx)
else:
raise NotImplementedError()
def content() -> None:
pass


list_options = [
click.option("--arch"),
click.option("--arch-in", "arch__in"),
click.option("--epoch"),
click.option("--epoch-in", "epoch__in"),
click.option("--fields"),
click.option("--name"),
click.option("--name-in", "name__in"),
click.option("--package-href"),
click.option("--release"),
click.option("--release-in", "release__in"),
click.option("--repository-version"),
click.option("--version"),
click.option("--version-in", "version__in"),
pulp_option("--exclude_fields"),
pulp_option("--fields"),
pulp_option("--repository-version"),
pulp_option("--arch", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--arch-in", "arch__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--arch-ne", "arch__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--epoch", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--epoch-in", "epoch__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--epoch-ne", "epoch__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--id", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmAdvisoryContext)),
pulp_option(
"--id-in", "id__in", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmAdvisoryContext)
),
pulp_option("--module", allowed_with_contexts=(PulpRpmModulemdDefaultsContext,)),
pulp_option(
"--module-in", "module__in", allowed_with_contexts=(PulpRpmModulemdDefaultsContext,)
),
pulp_option("--name", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext)),
pulp_option(
"--name-in",
"name__in",
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
),
pulp_option(
"--name-ne",
"name__ne",
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
),
pulp_option("--package-href", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--pkgId", allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext)),
pulp_option(
"--pkgId-in",
"pkgId__in",
allowed_with_contexts=(PulpRpmPackageContext, PulpRpmModulemdContext),
),
pulp_option("--release", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--release-in", "release__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--release-ne", "release__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--severity", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--severity-in", "severity__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--severity-ne", "severity__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--sha256",
allowed_with_contexts=(
PulpRpmPackageContext,
PulpRpmModulemdDefaultsContext,
PulpRpmModulemdContext,
),
),
pulp_option("--status", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--status-in", "status__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--status-ne", "status__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option(
"--stream", allowed_with_contexts=(PulpRpmModulemdDefaultsContext, PulpRpmModulemdContext)
),
pulp_option(
"--stream-in",
"stream__in",
allowed_with_contexts=(PulpRpmModulemdDefaultsContext, PulpRpmModulemdContext),
),
pulp_option("--type", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--type-in", "type__in", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--type-ne", "type__ne", allowed_with_contexts=(PulpRpmAdvisoryContext,)),
pulp_option("--version", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--version-in", "version__in", allowed_with_contexts=(PulpRpmPackageContext,)),
pulp_option("--version-ne", "version__ne", allowed_with_contexts=(PulpRpmPackageContext,)),
]
lookup_options = [
href_option,
click.option("--relative-path", callback=_relative_path_callback, expose_value=False),
click.option("--sha256", callback=_sha256_callback, expose_value=False),
pulp_option(
"--relative-path",
callback=_relative_path_callback,
expose_value=False,
allowed_with_contexts=(PulpRpmPackageContext,),
),
pulp_option(
"--sha256",
callback=_sha256_callback,
expose_value=False,
allowed_with_contexts=(
PulpRpmPackageContext,
PulpRpmModulemdDefaultsContext,
PulpRpmModulemdContext,
),
),
]

content.add_command(list_command(decorators=list_options))
content.add_command(show_command(decorators=lookup_options))
# create assumes "there exists an Artifact..."
# create is defined for package, modulemd and modulemd_defaults. The implications for modulemd
# and modulemd_defaults are generally not what the user expects. Therefore, leaving those two
# endpoints hidden until we have a better handle on what we should really be doing.
# See https://github.com/pulp/pulp_rpm/issues/2229 and https://github.com/pulp/pulp_rpm/issues/2534
create_options = [
click.option("--relative-path", required=True),
click.option(
pulp_option(
"--relative-path",
required=True,
allowed_with_contexts=(PulpRpmPackageContext,),
),
pulp_option(
"--sha256",
"artifact",
required=True,
help=_("Digest of the artifact to use"),
callback=_sha256_artifact_callback,
allowed_with_contexts=(PulpRpmPackageContext,),
),
]
content.add_command(
create_command(
decorators=create_options,
allowed_with_contexts=(PulpRpmPackageContext,),
)
)


content.add_command(list_command(decorators=list_options))
content.add_command(show_command(decorators=lookup_options))
content.add_command(create_command(decorators=create_options))


@content.command()
@click.option("--relative-path", required=True)
@click.option("--file", type=click.File("rb"), required=True)
# upload takes a file-argument and creates the entity from it.
# upload currently only works for advisory/package,
# see https://github.com/pulp/pulp_rpm/issues/2534
@content.command(
allowed_with_contexts=(
PulpRpmPackageContext,
PulpRpmAdvisoryContext,
)
)
@chunk_size_option
@pulp_option(
"--relative-path",
required=True,
help=_("Relative path within a distribution of the entity"),
allowed_with_contexts=(PulpRpmPackageContext,),
)
@pulp_option(
"--file",
type=click.File("rb"),
required=True,
help=_("An RPM binary"),
allowed_with_contexts=(PulpRpmPackageContext,),
)
@pulp_option(
"--file",
type=click.File("rb"),
required=True,
help=_("An advisory JSON file."),
allowed_with_contexts=(PulpRpmAdvisoryContext,),
)
@pass_entity_context
@pass_pulp_context
def upload(
pulp_ctx: PulpContext,
entity_ctx: PulpRpmPackageContext,
relative_path: str,
file: IO[bytes],
chunk_size: int,
entity_ctx: Union[
PulpRpmPackageContext,
PulpRpmAdvisoryContext,
],
**kwargs: Any,
) -> None:
"""Create an rpm package content unit by uploading a file"""
artifact_href = PulpArtifactContext(pulp_ctx).upload(file, chunk_size)
content = {"relative_path": relative_path, "artifact": artifact_href}
result = entity_ctx.create(body=content)
"""Create a content unit by uploading a file"""
file = kwargs.pop("file")
body = {}
if isinstance(entity_ctx, PulpRpmPackageContext):
chunk_size = kwargs.pop("chunk_size")
artifact_ctx = PulpArtifactContext(pulp_ctx)
artifact_href = artifact_ctx.upload(file, chunk_size)
body["artifact"] = artifact_href
body.update(kwargs)
result = entity_ctx.create(body=body)
elif isinstance(entity_ctx, PulpRpmAdvisoryContext):
result = entity_ctx.create(body=body, uploads={"file": file.read()})
else:
raise NotImplementedError()
pulp_ctx.output_result(result)
63 changes: 63 additions & 0 deletions pulpcore/cli/rpm/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,69 @@ class PulpRpmPackageContext(PulpContentContext):
ID_PREFIX = "content_rpm_packages"


class PulpRpmAdvisoryContext(PulpContentContext):
ENTITY = "rpm advisory"
ENTITIES = "rpm advisories"
HREF = "rpm_update_record_href"
ID_PREFIX = "content_rpm_advisories"


class PulpRpmDistributionTreeContext(PulpContentContext):
ENTITY = "rpm distribution tree"
ENTITIES = "rpm distribution trees"
HREF = "rpm_distribution_tree_href"
ID_PREFIX = "content_rpm_distribution_trees"


class PulpRpmModulemdDefaultsContext(PulpContentContext):
ENTITY = "rpm modulemd defaults"
ENTITIES = "rpm modulemd defaults"
HREF = "rpm_modulemd_defaults_href"
ID_PREFIX = "content_rpm_modulemd_defaults"


class PulpRpmModulemdContext(PulpContentContext):
ENTITY = "rpm modulemd"
ENTITIES = "rpm modulemds"
HREF = "rpm_modulemd_href"
ID_PREFIX = "content_rpm_modulemds"


class PulpRpmPackageCategoryContext(PulpContentContext):
ENTITY = "rpm package category"
ENTITIES = "rpm package categories"
HREF = "rpm_package_category_href"
ID_PREFIX = "content_rpm_packagecategories"


class PulpRpmPackageEnvironmentContext(PulpContentContext):
ENTITY = "rpm package environment"
ENTITIES = "rpm package environments"
HREF = "rpm_package_environment_href"
ID_PREFIX = "content_rpm_packageenvironments"


class PulpRpmPackageGroupContext(PulpContentContext):
ENTITY = "rpm package group"
ENTITIES = "rpm package groups"
HREF = "rpm_package_group_href"
ID_PREFIX = "content_rpm_packagegroups"


class PulpRpmPackageLangpacksContext(PulpContentContext):
ENTITY = "rpm package langpack"
ENTITIES = "rpm package langpacks"
HREF = "rpm_package_langpacks_href"
ID_PREFIX = "content_rpm_packagelangpacks"


class PulpRpmRepoMetadataFileContext(PulpContentContext):
ENTITY = "rpm repo metadata file"
ENTITIES = "rpm repo metadata files"
HREF = "rpm_repo_metadata_file_href"
ID_PREFIX = "content_rpm_repo_metadata_files"


class PulpRpmPublicationContext(PulpEntityContext):
ENTITY = _("rpm publication")
ENTITIES = _("rpm publications")
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def pulp_cli_vars(pulp_cli_vars):
"CONTAINER_REMOTE_URL": "https://quay.io",
"CONTAINER_IMAGE": "libpod/alpine",
"RPM_REMOTE_URL": urljoin(PULP_FIXTURES_URL, "/rpm-unsigned"),
"RPM_WEAK_DEPS_URL": urljoin(PULP_FIXTURES_URL, "/rpm-richnweak-deps"),
"RPM_MODULES_REMOTE_URL": urljoin(PULP_FIXTURES_URL, "/rpm-with-modules"),
"ANSIBLE_COLLECTION_REMOTE_URL": "https://galaxy.ansible.com/",
"ANSIBLE_ROLE_REMOTE_URL": "https://galaxy.ansible.com/api/v1/roles/?namespace__name=elastic", # noqa
"PYTHON_REMOTE_URL": PULP_FIXTURES_URL + "/python-pypi/",
Expand Down
2 changes: 1 addition & 1 deletion tests/scripts/pulp_rpm/test_acs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cleanup() {
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
pulp orphan cleanup || true
pulp orphan cleanup --protection-time 0 || true
}
trap cleanup EXIT

Expand Down
33 changes: 33 additions & 0 deletions tests/scripts/pulp_rpm/test_advisory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"issued": "2020-03-08 20:04:01",
"id": "CEBA-2022-124387",
"type": "Bug Fix Advisory",
"release": "1",
"version": "1",
"pkglist": [
{
"packages": [
{
"arch": "noarch",
"epoch": "0",
"filename": "bear-4.1-1.noarch.rpm",
"name": "bear",
"reboot_suggested": false,
"relogin_suggested": false,
"restart_suggested": false,
"release": "1",
"src": "http://www.fedoraproject.org",
"sum": "",
"sum_type": "",
"version": "4.1"
}
]
}
],
"severity": "",
"description": "Not available",
"reboot_suggested": false,
"updated": "2022-03-08 20:04:01",
"solution": "Not available",
"fromstr": "[email protected]"
}
Loading

0 comments on commit 8a74968

Please sign in to comment.