From 4619f941557fc239f73bf02a2c6cc44bed1115f2 Mon Sep 17 00:00:00 2001 From: Nathan Freeman Date: Wed, 28 Aug 2024 13:20:04 -0500 Subject: [PATCH] Add task model serializers --- .../serializers/ApplicationTaskSerializer.py | 11 ++++++ .../backend/serializers/ContextSerializer.py | 19 +++++++++++ .../serializers/CredentialsSerializer.py | 16 +++++++++ .../serializers/DestinationSerializer.py | 16 +++++++++ .../serializers/ImageBuildTaskSerializer.py | 15 ++++++++ .../serializers/RequestTaskSerializer.py | 18 ++++++++++ .../serializers/TapisActorTaskSerializer.py | 13 +++++++ .../serializers/TapisJobTaskSerializer.py | 12 +++++++ .../src/backend/serializers/TaskSerializer.py | 34 ++++++++++++++++++- .../serializers/TemplateTaskSerializer.py | 11 ++++++ src/api/src/backend/serializers/__init__.py | 11 +++++- src/api/src/backend/views/Tasks.py | 13 +++++-- 12 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/api/src/backend/serializers/ApplicationTaskSerializer.py create mode 100644 src/api/src/backend/serializers/ContextSerializer.py create mode 100644 src/api/src/backend/serializers/CredentialsSerializer.py create mode 100644 src/api/src/backend/serializers/DestinationSerializer.py create mode 100644 src/api/src/backend/serializers/ImageBuildTaskSerializer.py create mode 100644 src/api/src/backend/serializers/RequestTaskSerializer.py create mode 100644 src/api/src/backend/serializers/TapisActorTaskSerializer.py create mode 100644 src/api/src/backend/serializers/TapisJobTaskSerializer.py create mode 100644 src/api/src/backend/serializers/TemplateTaskSerializer.py diff --git a/src/api/src/backend/serializers/ApplicationTaskSerializer.py b/src/api/src/backend/serializers/ApplicationTaskSerializer.py new file mode 100644 index 00000000..2ad78320 --- /dev/null +++ b/src/api/src/backend/serializers/ApplicationTaskSerializer.py @@ -0,0 +1,11 @@ +from backend.serializers import BaseTaskSerializer + + +class ApplicationTaskSerializer: + @staticmethod + def serialize(model, base=None): + task = base if base != None else BaseTaskSerializer.serialize(model) + + task["image"] = model.image + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/ContextSerializer.py b/src/api/src/backend/serializers/ContextSerializer.py new file mode 100644 index 00000000..6e45a3e3 --- /dev/null +++ b/src/api/src/backend/serializers/ContextSerializer.py @@ -0,0 +1,19 @@ +from backend.serializers.CredentialsSerializer import CredentialsSerializer +from backend.serializers.UUIDSerializer import UUIDSerializer + + +class ContextSerializer: + @staticmethod + def serialize(model): + context = {} + context["branch"] = model + context["credentials"] = CredentialsSerializer.serialize(model.credentials) + context["recipe_file_path"] = model.recipe_file_path + context["sub_path"] = model.sub_path + context["tag"] = model.tag + context["type"] = model.type + context["url"] = model.url + context["visibility"] = model.visibility + context["uuid"] = UUIDSerializer.serialize(model.uuid) + + return context \ No newline at end of file diff --git a/src/api/src/backend/serializers/CredentialsSerializer.py b/src/api/src/backend/serializers/CredentialsSerializer.py new file mode 100644 index 00000000..79aad317 --- /dev/null +++ b/src/api/src/backend/serializers/CredentialsSerializer.py @@ -0,0 +1,16 @@ +from backend.serializers.UUIDSerializer import UUIDSerializer + + +class CredentialsSerializer: + @staticmethod + def serialize(model): + + credential = {} + + credential["sk_id"] = model.sk_id + credential["owner"] = model.owner + credential["created_at"] = model.created_at + credential["uuid"] = UUIDSerializer.serialize(model.uuid) + + + return credential \ No newline at end of file diff --git a/src/api/src/backend/serializers/DestinationSerializer.py b/src/api/src/backend/serializers/DestinationSerializer.py new file mode 100644 index 00000000..a1574d35 --- /dev/null +++ b/src/api/src/backend/serializers/DestinationSerializer.py @@ -0,0 +1,16 @@ +from backend.serializers.CredentialsSerializer import CredentialsSerializer +from backend.serializers.UUIDSerializer import UUIDSerializer + + +class DestinationSerializer: + @staticmethod + def serialize(model): + destination = {} + destination["tag"] = model.tag, + destination["type"] = model.type, + destination["url"] = model.url, + destination["filename"] = model.filename, + destination["credentials"] = CredentialsSerializer.serialize(model.credentials) + destination["uuid"] = UUIDSerializer.serialize(model.uuid) + + return destination \ No newline at end of file diff --git a/src/api/src/backend/serializers/ImageBuildTaskSerializer.py b/src/api/src/backend/serializers/ImageBuildTaskSerializer.py new file mode 100644 index 00000000..ae2e144e --- /dev/null +++ b/src/api/src/backend/serializers/ImageBuildTaskSerializer.py @@ -0,0 +1,15 @@ +from backend.serializers import BaseTaskSerializer +from backend.serializers.ContextSerializer import ContextSerializer +from backend.serializers.DestinationSerializer import DestinationSerializer + + +class ImageBuildTaskSerializer: + @staticmethod + def serialize(model, base=None): + task = base if base != None else BaseTaskSerializer.serialize(model) + + task["builder"] = model.builder + task["context"] = ContextSerializer.serialize(model.context) + task["destination"] = DestinationSerializer(model.destination) + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/RequestTaskSerializer.py b/src/api/src/backend/serializers/RequestTaskSerializer.py new file mode 100644 index 00000000..bb687966 --- /dev/null +++ b/src/api/src/backend/serializers/RequestTaskSerializer.py @@ -0,0 +1,18 @@ +from backend.serializers import BaseTaskSerializer +from backend.serializers.CredentialsSerializer import CredentialsSerializer + + +class RequestTaskSerializer: + @staticmethod + def serialize(model, base=None): + task = base if base != None else BaseTaskSerializer.serialize(model) + + task["auth"] = CredentialsSerializer.serialize(model.auth) + task["data"] = model.data + task["headers"] = model.headers + task["http_method"] = model.http_method + task["protocol"] = model.protocol + task["query_params"] = model.query_params + task["url"] = model.url + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/TapisActorTaskSerializer.py b/src/api/src/backend/serializers/TapisActorTaskSerializer.py new file mode 100644 index 00000000..d0050ad5 --- /dev/null +++ b/src/api/src/backend/serializers/TapisActorTaskSerializer.py @@ -0,0 +1,13 @@ +from backend.serializers import BaseTaskSerializer + + +class TapisActorTaskSerializer: + @staticmethod + def serialize(model, base=None): + task = base if base != None else BaseTaskSerializer.serialize(model) + + task["tapis_actor_id"] = model.tapis_job_def + task["poll"] = model.poll + task["tapis_actor_message"] = model.tapis_actor_message + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/TapisJobTaskSerializer.py b/src/api/src/backend/serializers/TapisJobTaskSerializer.py new file mode 100644 index 00000000..78e5508d --- /dev/null +++ b/src/api/src/backend/serializers/TapisJobTaskSerializer.py @@ -0,0 +1,12 @@ +from backend.serializers import BaseTaskSerializer + + +class TapisJobTaskSerializer: + @staticmethod + def serialize(model, base=None): + task = base if base != None else BaseTaskSerializer.serialize(model) + + task["tapis_job_def"] = model.tapis_job_def + task["poll"] = model.poll + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/TaskSerializer.py b/src/api/src/backend/serializers/TaskSerializer.py index 43e6ee3d..5e7607ff 100644 --- a/src/api/src/backend/serializers/TaskSerializer.py +++ b/src/api/src/backend/serializers/TaskSerializer.py @@ -1,6 +1,20 @@ from backend.serializers.BaseTaskSerializer import BaseTaskSerializer from backend.serializers.FunctionTaskSerializer import FunctionTaskSerializer -from backend.models import TASK_TYPE_FUNCTION +from backend.serializers.ImageBuildTaskSerializer import ImageBuildTaskSerializer +from backend.serializers.TapisActorTaskSerializer import TapisActorTaskSerializer +from backend.serializers.TapisJobTaskSerializer import TapisJobTaskSerializer +from backend.serializers.ApplicationTaskSerializer import ApplicationTaskSerializer +from backend.serializers.TemplateTaskSerializer import TemplateTaskSerializer +from backend.serializers.RequestTaskSerializer import RequestTaskSerializer +from backend.models import ( + TASK_TYPE_FUNCTION, + TASK_TYPE_APPLICATION, + TASK_TYPE_TEMPLATE, + TASK_TYPE_TAPIS_ACTOR, + TASK_TYPE_IMAGE_BUILD, + TASK_TYPE_REQUEST, + TASK_TYPE_TAPIS_JOB +) class TaskSerializer: @staticmethod @@ -10,4 +24,22 @@ def serialize(model): if model.type == TASK_TYPE_FUNCTION: return FunctionTaskSerializer.serialize(model, base=base) + if model.type == TASK_TYPE_TEMPLATE: + return TemplateTaskSerializer.serialize(model, base=base) + + if model.type == TASK_TYPE_IMAGE_BUILD: + return ImageBuildTaskSerializer.serialize(model, base=base) + + if model.type == TASK_TYPE_TAPIS_JOB: + return TapisJobTaskSerializer.serialize(model, base=base) + + if model.type == TASK_TYPE_TAPIS_ACTOR: + return TapisActorTaskSerializer.serialize(model, base=base) + + if model.type == TASK_TYPE_REQUEST: + return RequestTaskSerializer.serialize(model, base=base) + + if model.type == TASK_TYPE_APPLICATION: + return ApplicationTaskSerializer.serialize(model, base=base) + raise NotImplementedError(f"Task Serializer does not have a method for serializing tasks of type '{model.type}'") \ No newline at end of file diff --git a/src/api/src/backend/serializers/TemplateTaskSerializer.py b/src/api/src/backend/serializers/TemplateTaskSerializer.py new file mode 100644 index 00000000..b19aeb3f --- /dev/null +++ b/src/api/src/backend/serializers/TemplateTaskSerializer.py @@ -0,0 +1,11 @@ +from backend.serializers import BaseTaskSerializer + + +class TemplateTaskSerializer: + @staticmethod + def serialize(model, base=None): + # Returning only the base task because the only property relavent to + # the template task (the `uses` property) is handled there. + task = base if base != None else BaseTaskSerializer.serialize(model) + + return task \ No newline at end of file diff --git a/src/api/src/backend/serializers/__init__.py b/src/api/src/backend/serializers/__init__.py index c9e0e722..16ae9984 100644 --- a/src/api/src/backend/serializers/__init__.py +++ b/src/api/src/backend/serializers/__init__.py @@ -4,4 +4,13 @@ from backend.serializers.BaseTaskSerializer import BaseTaskSerializer from backend.serializers.TaskSerializer import TaskSerializer from backend.serializers.TaskDTOSerializer import TaskDTOSerializer -from backend.serializers.FunctionTaskSerializer import FunctionTaskSerializer \ No newline at end of file +from backend.serializers.FunctionTaskSerializer import FunctionTaskSerializer +from backend.serializers.ImageBuildTaskSerializer import ImageBuildTaskSerializer +from backend.serializers.TapisActorTaskSerializer import TapisActorTaskSerializer +from backend.serializers.TapisJobTaskSerializer import TapisJobTaskSerializer +from backend.serializers.ApplicationTaskSerializer import ApplicationTaskSerializer +from backend.serializers.TemplateTaskSerializer import TemplateTaskSerializer +from backend.serializers.RequestTaskSerializer import RequestTaskSerializer +from backend.serializers.ContextSerializer import ContextSerializer +from backend.serializers.DestinationSerializer import DestinationSerializer +from backend.serializers.CredentialsSerializer import CredentialsSerializer \ No newline at end of file diff --git a/src/api/src/backend/views/Tasks.py b/src/api/src/backend/views/Tasks.py index 3fd6f56a..0c626150 100644 --- a/src/api/src/backend/views/Tasks.py +++ b/src/api/src/backend/views/Tasks.py @@ -7,6 +7,7 @@ from backend.views.http.responses import BaseResponse, ResourceURLResponse from backend.views.http.responses.errors import BadRequest, Forbidden, NotFound, MethodNotAllowed, ServerError from backend.views.http.responses.models import ModelListResponse, ModelResponse +from backend.views.http.responses import BaseResponse from backend.services.TaskService import service as task_service from backend.services.GroupService import service as group_service from backend.serializers import TaskSerializer, TaskDTOSerializer @@ -52,8 +53,16 @@ def get(self, request, group_id, pipeline_id, task_id=None): return ModelResponse(task) - def list(self, pipeline, *_, **__): - return ModelListResponse(Task.objects.filter(pipeline=pipeline)) + def list(self, pipeline, *_, **__): + task_models = Task.objects.filter(pipeline=pipeline) + tasks = [] + try: + for task_model in task_models: + tasks.append(TaskSerializer.serialize(task_model)) + except Exception as e: + return ServerError(f"{e}") + + return BaseResponse(result=tasks) def post(self, request, group_id, pipeline_id, *_, **__): # Validate the request body