Skip to content

Commit

Permalink
Update the import endpoint to return a task group
Browse files Browse the repository at this point in the history
fixes #9382
  • Loading branch information
David Davis committed Sep 21, 2021
1 parent 99b06a4 commit 6771a8d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES/9382.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated the pulp import creation endpoint to return a task group instead of a task.
6 changes: 1 addition & 5 deletions pulpcore/app/tasks/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,13 @@ def validate_and_assemble(toc_filename):

log.info(_("Importing {}.").format(path))
current_task = Task.current()
task_group = TaskGroup.current()
importer = PulpImporter.objects.get(pk=importer_pk)
the_import = PulpImport.objects.create(
importer=importer, task=current_task, params={"path": path}
)
CreatedResource.objects.create(content_object=the_import)

task_group = TaskGroup.objects.create(description=f"Import of {path}")
Task.objects.filter(pk=current_task.pk).update(task_group=task_group)
current_task.refresh_from_db()
CreatedResource.objects.create(content_object=task_group)

with tempfile.TemporaryDirectory() as temp_dir:
with tarfile.open(path, "r:gz") as tar:
tar.extractall(path=temp_dir)
Expand Down
15 changes: 10 additions & 5 deletions pulpcore/app/viewsets/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
Importer,
PulpImport,
PulpImporter,
TaskGroup,
)
from pulpcore.app.response import OperationPostponedResponse
from pulpcore.app.response import TaskGroupOperationResponse
from pulpcore.app.serializers import (
AsyncOperationResponseSerializer,
ImportSerializer,
ImporterSerializer,
PulpImporterSerializer,
PulpImportSerializer,
TaskGroupOperationResponseSerializer,
)
from pulpcore.app.tasks import pulp_import
from pulpcore.app.viewsets import (
Expand Down Expand Up @@ -91,7 +92,7 @@ class PulpImportViewSet(ImportViewSet):
@extend_schema(
request=PulpImportSerializer,
description="Trigger an asynchronous task to import a Pulp export.",
responses={202: AsyncOperationResponseSerializer},
responses={202: TaskGroupOperationResponseSerializer},
)
def create(self, request, importer_pk):
"""Import a Pulp export into Pulp."""
Expand All @@ -102,11 +103,15 @@ def create(self, request, importer_pk):

serializer = PulpImportSerializer(data=request.data, context={"request": request})
serializer.is_valid(raise_exception=True)

path = serializer.validated_data.get("path")
toc = serializer.validated_data.get("toc")
task = dispatch(
task_group = TaskGroup.objects.create(description=f"Import of {path}")

dispatch(
pulp_import,
exclusive_resources=[importer],
task_group=task_group,
kwargs={"importer_pk": importer.pk, "path": path, "toc": toc},
)
return OperationPostponedResponse(task, request)
return TaskGroupOperationResponse(task_group, request)
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ def _perform_import(self, importer, chunked=False, an_export=None):
else:
filenames = [f for f in list(an_export.output_file_info.keys()) if f.endswith("tar.gz")]
import_response = self.imports_api.create(importer.pulp_href, {"path": filenames[0]})
monitor_task(import_response.task)
task = self.client.get(import_response.task)
resources = task["created_resources"]
task_group_href = resources[1]
task_group = monitor_task_group(task_group_href)
task_group = monitor_task_group(import_response.task_group)

return task_group

Expand Down

0 comments on commit 6771a8d

Please sign in to comment.