Skip to content

Commit

Permalink
Adds machinery to return task groups from viewsets
Browse files Browse the repository at this point in the history
Adds a ``TaskGroupResponse`` and corresponding
``TaskGroupResponseSerializer`` allowing viewsets to return a task group
back to users directly.

closes #9380
  • Loading branch information
bmbouter committed Sep 13, 2021
1 parent 69e16fa commit 1792ddf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES/plugin_api/9380.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Added ``pulpcore.plugin.viewset.TaskGroupResponse`` which can be used to return a reference to a
task group created in a viewset. Added ``pulpcore.plugin.serializers.TaskGroupResponseSerializer``
which can be used to indicate the serializer response format of viewsets that will use
``TaskGroupResponse`` similar to how ``AsyncOperationResponseSerializer`` is used.
23 changes: 23 additions & 0 deletions pulpcore/app/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ def __init__(self, task, request):
"""
resp = {"task": reverse("tasks-detail", args=[task.pk], request=None)}
super().__init__(data=resp, status=202)


class TaskGroupResponse(Response):
"""
An HTTP response class for returning 202 and a task group.
This response object should be used by views that create a task group and dispatch one or more
tasks for that group. When JSON is requested, the response will look like the following::
{
"task_group": "/pulp/api/v3/task-groups/735633bc-eb41-4737-b436-c7c6914f34b1/"
}
"""

def __init__(self, task_group, request):
"""
Args:
task_group (pulpcore.plugin.models.TaskGroup): A
:class:`~pulpcore.plugin.models.TaskGroup` object used to generate the response.
request (rest_framework.request.Request): Request used to generate the pulp_href urls
"""
resp = {"task_group": reverse("task-groups-detail", args=[task_group.pk], request=None)}
super().__init__(data=resp, status=202)
1 change: 1 addition & 0 deletions pulpcore/app/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NestedRelatedField,
RelatedField,
RelatedResourceField,
TaskGroupResponseSerializer,
ValidateFieldsMixin,
validate_unknown_fields,
)
Expand Down
16 changes: 15 additions & 1 deletion pulpcore/app/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
NestedHyperlinkedRelatedField,
)

from pulpcore.app.models import Label, Task
from pulpcore.app.models import Label, Task, TaskGroup
from pulpcore.app.util import (
get_view_name_for_model,
get_viewset_for_model,
Expand Down Expand Up @@ -366,3 +366,17 @@ class AsyncOperationResponseSerializer(serializers.Serializer):
view_name="tasks-detail",
allow_null=False,
)


class TaskGroupResponseSerializer(serializers.Serializer):
"""
Serializer for asynchronous operations that return a task group.
"""

task_group = RelatedField(
required=True,
help_text=_("The href of the task group."),
queryset=TaskGroup.objects,
view_name="task-groups-detail",
allow_null=False,
)
1 change: 1 addition & 0 deletions pulpcore/plugin/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
RepositoryVersionRelatedField,
SingleArtifactContentSerializer,
SingleContentArtifactField,
TaskGroupResponseSerializer,
ValidateFieldsMixin,
validate_unknown_fields,
)
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/plugin/viewsets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Allow plugin viewsets to return 202s
from pulpcore.app.response import OperationPostponedResponse # noqa
from pulpcore.app.response import OperationPostponedResponse, TaskGroupResponse # noqa

# Import Viewsets in platform that are potentially useful to plugin writers
from pulpcore.app.viewsets import ( # noqa
Expand Down

0 comments on commit 1792ddf

Please sign in to comment.