Skip to content

Commit

Permalink
Add task model serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
nathandf committed Aug 28, 2024
1 parent 2228788 commit 4619f94
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/api/src/backend/serializers/ApplicationTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions src/api/src/backend/serializers/ContextSerializer.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/api/src/backend/serializers/CredentialsSerializer.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/api/src/backend/serializers/DestinationSerializer.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/api/src/backend/serializers/ImageBuildTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions src/api/src/backend/serializers/RequestTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions src/api/src/backend/serializers/TapisActorTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/api/src/backend/serializers/TapisJobTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 33 additions & 1 deletion src/api/src/backend/serializers/TaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}'")
11 changes: 11 additions & 0 deletions src/api/src/backend/serializers/TemplateTaskSerializer.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion src/api/src/backend/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
13 changes: 11 additions & 2 deletions src/api/src/backend/views/Tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4619f94

Please sign in to comment.