Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NoArtifactContentViewSet #5088

Merged
merged 1 commit into from Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/plugin_api/5086.feature
@@ -0,0 +1 @@
Added NoArtifactContentViewSet for content creation that does not require a file to be uploaded.
1 change: 1 addition & 0 deletions pulpcore/plugin/viewsets/__init__.py
Expand Up @@ -44,6 +44,7 @@
from pulpcore.filters import HyperlinkRelatedFilter

from .content import (
NoArtifactContentViewSet,
NoArtifactContentUploadViewSet,
SingleArtifactContentUploadViewSet,
)
29 changes: 29 additions & 0 deletions pulpcore/plugin/viewsets/content.py
Expand Up @@ -29,6 +29,35 @@ def get_deferred_context(self, request):
return {}


class NoArtifactContentViewSet(DefaultDeferredContextMixin, ContentViewSet):
"""A ViewSet for content creation that does not require a file to be uploaded."""

@extend_schema(
description="Trigger an asynchronous task to create content,"
"optionally create new repository version.",
responses={202: AsyncOperationResponseSerializer},
)
def create(self, request):
"""Create a content unit."""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)

exclusive_resources = [
item for item in (serializer.validated_data.get(key) for key in ("repository",)) if item
]

task = dispatch(
tasks.base.general_create,
exclusive_resources=exclusive_resources,
args=(self.queryset.model._meta.app_label, serializer.__class__.__name__),
kwargs={
"data": {k: v for k, v in request.data.items()},
"context": self.get_deferred_context(request),
},
)
return OperationPostponedResponse(task, request)


class NoArtifactContentUploadViewSet(DefaultDeferredContextMixin, ContentViewSet):
"""A ViewSet for uploads that do not require to store an uploaded content as an Artifact."""

Expand Down
Expand Up @@ -125,6 +125,11 @@ ViewSet, the associated serializer should also subclass `NoArtifactContentUpload
that by using the aforesaid Serializer and ViewSet, Pulp still loads content of an uploaded file to
a temporary file. But the file is going to be removed by the end of a created task.

If creating the content does not require any files to be uploaded at all, you can use
`NoArtifactContentViewSet` along with `NoArtifactContentSerializer`. This is useful if creating the
content in question requires only a set of required API parameters, but should still use the same
"add to repository" and/or `retrieve` workflows.

If any additional context needs to be passed from the ViewSet to the creation task, the
`get_deferred_context` method of the ViewSet might be overwritten. It's return value will then be
available as `self.context` in the Serializer.
Expand Down