From 2880b4458c99f74ce320b82b15b4fd4de4f16d72 Mon Sep 17 00:00:00 2001 From: Gerrod Ubben Date: Wed, 6 Dec 2023 11:40:42 -0500 Subject: [PATCH] Switch ansible collection upload to use the Pulp V3 api fixes: #844 --- CHANGES/844.feature | 1 + pulp-glue/pulp_glue/ansible/context.py | 11 ++++++++++- pulpcore/cli/ansible/content.py | 22 +++++++++------------- 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 CHANGES/844.feature diff --git a/CHANGES/844.feature b/CHANGES/844.feature new file mode 100644 index 000000000..84b1614bf --- /dev/null +++ b/CHANGES/844.feature @@ -0,0 +1 @@ +Ansible Collection upload now uses the Pulp V3 API and uploading directly to a repository with `--repository`. diff --git a/pulp-glue/pulp_glue/ansible/context.py b/pulp-glue/pulp_glue/ansible/context.py index b1255d11b..195a38c69 100644 --- a/pulp-glue/pulp_glue/ansible/context.py +++ b/pulp-glue/pulp_glue/ansible/context.py @@ -24,9 +24,18 @@ class PulpAnsibleCollectionVersionContext(PulpContentContext): ID_PREFIX = "content_ansible_collection_versions" UPLOAD_ID: ClassVar[str] = "upload_collection" NEEDS_PLUGINS = [PluginRequirement("ansible", specifier=">=0.7.0")] + CAPABILITIES = {"upload": [PluginRequirement("ansible", specifier=">=0.16.0")]} def upload(self, file: IO[bytes], **kwargs: Any) -> Any: # type:ignore - return self.call("upload", body={"file": file}) + repository: Optional[PulpRepositoryContext] = kwargs.pop("repository", None) + if self.capable("upload"): + chunk_size: int = kwargs.pop("chunk_size", 1000000) + return super().upload(file, chunk_size, repository, **kwargs) + else: + result = self.call("upload", body={"file": file}) + if repository: + repository.modify(add_content_units=[self.pulp_href]) + return result class PulpAnsibleRoleContext(PulpContentContext): diff --git a/pulpcore/cli/ansible/content.py b/pulpcore/cli/ansible/content.py index 0525948e1..b3c612164 100644 --- a/pulpcore/cli/ansible/content.py +++ b/pulpcore/cli/ansible/content.py @@ -1,4 +1,4 @@ -from typing import IO, Any, Callable, Optional +from typing import IO, Any, Callable import click from pulp_glue.ansible.context import ( @@ -7,7 +7,7 @@ PulpAnsibleRepositoryContext, PulpAnsibleRoleContext, ) -from pulp_glue.common.context import PulpEntityContext, PulpRepositoryContext +from pulp_glue.common.context import PulpContentContext, PulpRepositoryContext from pulp_glue.common.i18n import get_translation from pulp_glue.core.context import PulpArtifactContext @@ -37,7 +37,7 @@ def _content_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any: if value: - entity_ctx = ctx.find_object(PulpEntityContext) + entity_ctx = ctx.find_object(PulpContentContext) assert entity_ctx is not None entity_ctx.entity = value return value @@ -85,7 +85,7 @@ def _new_callback(*args: Any, **kwargs: Any) -> Any: "ansible:ansible": PulpAnsibleRepositoryContext, }, href_pattern=PulpRepositoryContext.HREF_PATTERN, - allowed_with_contexts=signature_context, + allowed_with_contexts=collection_context + signature_context, help=_( "Repository to upload into in the form '[[:]:]' or by href." ), @@ -228,7 +228,7 @@ def content(ctx: click.Context, pulp_ctx: PulpCLIContext, content_type: str) -> help=_("Chunk size to break up {entity} into. Defaults to 1MB"), default="1MB", callback=parse_size_callback, - allowed_with_contexts=role_context, + allowed_with_contexts=content_context, ) @pulp_option( "--name", help=_("Name of {entity}"), allowed_with_contexts=role_context, required=True @@ -255,7 +255,7 @@ def content(ctx: click.Context, pulp_ctx: PulpCLIContext, content_type: str) -> @pass_pulp_context def upload( pulp_ctx: PulpCLIContext, - content_ctx: PulpEntityContext, + content_ctx: PulpContentContext, file: IO[bytes], **kwargs: Any, ) -> None: @@ -267,13 +267,9 @@ def upload( result = content_ctx.create(body=body) pulp_ctx.output_result(result) elif isinstance(content_ctx, PulpAnsibleCollectionVersionSignatureContext): - body = {"signed_collection": kwargs["collection"], "file": file} - repository: Optional[PulpRepositoryContext] = kwargs["repository"] - if repository: - body["repository"] = repository.pulp_href - pulp_ctx.output_result(content_ctx.create(body=body)) + kwargs["signed_collection"] = kwargs.pop("collection") + pulp_ctx.output_result(content_ctx.create(body=kwargs)) elif isinstance(content_ctx, PulpAnsibleCollectionVersionContext): - result = content_ctx.upload(file=file) - pulp_ctx.output_result(result) + pulp_ctx.output_result(content_ctx.upload(file=file, **kwargs)) else: raise NotImplementedError()