Skip to content

Commit

Permalink
Switch ansible collection upload to use the Pulp V3 api
Browse files Browse the repository at this point in the history
fixes: pulp#844
  • Loading branch information
gerrod3 committed Dec 6, 2023
1 parent c3cec8d commit c0d34c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/844.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ansible Collection upload now uses the Pulp V3 API and uploading directly to a repository with `--repository`.
8 changes: 7 additions & 1 deletion pulp-glue/pulp_glue/ansible/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ 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})
if self.capable("upload"):
chunk_size: int = kwargs.pop("chunk_size", 1000000)
repository: Optional[PulpRepositoryContext] = kwargs.pop("repository", None)
return super().upload(file, chunk_size, repository, **kwargs)
else:
return self.call("upload", body={"file": file})


class PulpAnsibleRoleContext(PulpContentContext):
Expand Down
22 changes: 9 additions & 13 deletions pulpcore/cli/ansible/content.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '[[<plugin>:]<resource_type>:]<name>' or by href."
),
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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()

0 comments on commit c0d34c7

Please sign in to comment.