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..7933e8ddc 100644 --- a/pulp-glue/pulp_glue/ansible/context.py +++ b/pulp-glue/pulp_glue/ansible/context.py @@ -24,9 +24,19 @@ 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}) + self.pulp_href = result["created_resources"][0] + if repository: + repository.modify(add_content=[self.pulp_href]) + return self.entity class PulpAnsibleRoleContext(PulpContentContext): diff --git a/pulpcore/cli/ansible/content.py b/pulpcore/cli/ansible/content.py index 0525948e1..0480c8b9a 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,10 @@ 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["file"] = file + 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() diff --git a/tests/scripts/pulp_ansible/test_content.sh b/tests/scripts/pulp_ansible/test_content.sh index 95fa14852..ce1d3c379 100755 --- a/tests/scripts/pulp_ansible/test_content.sh +++ b/tests/scripts/pulp_ansible/test_content.sh @@ -8,6 +8,7 @@ pulp debug has-plugin --name "ansible" || exit 23 cleanup() { pulp ansible repository destroy --name "cli_test_ansible_repository" || true pulp ansible repository destroy --name "cli_test_ansible_repository_verify" || true + pulp ansible repository destroy --name "cli_test_ansible_repository_upload" || true } trap cleanup EXIT @@ -25,13 +26,16 @@ fi wget "https://galaxy.ansible.com/download/ansible-posix-1.3.0.tar.gz" sha256=$(sha256sum ansible-posix-1.3.0.tar.gz | cut -d' ' -f1) -expect_succ pulp ansible content upload --file "ansible-posix-1.3.0.tar.gz" +expect_succ pulp ansible repository create --name "cli_test_ansible_repository_upload" +expect_succ pulp ansible content upload --file "ansible-posix-1.3.0.tar.gz" --repository "cli_test_ansible_repository_upload" expect_succ pulp artifact list --sha256 "$sha256" test "$(echo "$OUTPUT" | jq -r length)" -eq "1" expect_succ pulp ansible content list --name "posix" --namespace "ansible" --version "1.3.0" test "$(echo "$OUTPUT" | jq -r length)" -eq "1" content_href="$(echo "$OUTPUT" | jq -r .[0].pulp_href)" expect_succ pulp ansible content show --href "$content_href" +expect_succ pulp ansible repository content list --repository "cli_test_ansible_repository_upload" --version 1 +test "$(echo "$OUTPUT" | jq -r length)" -eq "1" # Test ansible role upload wget "https://github.com/ansible/ansible-kubernetes-modules/archive/v0.0.1.tar.gz"