Skip to content

Commit

Permalink
add runtimejob handling additional apis (#1197)
Browse files Browse the repository at this point in the history
* add runtimejob handling additional apis

Signed-off-by: Akihiko Kuroda <[email protected]>
  • Loading branch information
akihikokuroda authored Jan 29, 2024
1 parent 348d299 commit 3af60ac
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
39 changes: 39 additions & 0 deletions gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,45 @@ def stop(self, request, pk=None): # pylint: disable=invalid-name,unused-argumen
)
return Response({"message": message})

@action(methods=["POST"], detail=True)
def add_runtimejob(
self, request, pk=None
): # pylint: disable=invalid-name,unused-argument
"""Add RuntimeJob to job"""
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.job.add_runtimejob", context=ctx):
if not request.data.get("runtime_job"):
return Response(
{
"message": "Got empty `runtime_job` field. Please, specify `runtime_job`."
},
status=status.HTTP_400_BAD_REQUEST,
)
job = self.get_object()
runtimejob = RuntimeJob(
job=job,
runtime_job=request.data.get("runtime_job"),
)
runtimejob.save()
message = "RuntimeJob is added."
return Response({"message": message})

@action(methods=["GET"], detail=True)
def list_runtimejob(
self, request, pk=None
): # pylint: disable=invalid-name,unused-argument
"""Add RuntimeJpb to job"""
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.job.stop", context=ctx):
job = self.get_object()
runtimejobs = RuntimeJob.objects.filter(job=job)
ids = []
for runtimejob in runtimejobs:
ids.append(runtimejob.runtime_job)
return Response(json.dumps(ids))


class FilesViewSet(viewsets.ViewSet):
"""ViewSet for file operations handling.
Expand Down
49 changes: 49 additions & 0 deletions gateway/tests/api/test_v1_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework import status
from rest_framework.test import APITestCase
from api.models import Job, JobConfig
import json


class TestProgramApi(APITestCase):
Expand Down Expand Up @@ -156,3 +157,51 @@ def test_runtime_job(self):
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)
self.assertEqual(programs_response.json().get("count"), 1)

def test_add_runtimejob(self):
"""Tests run existing authorized."""
auth = reverse("rest_login")
response = self.client.post(
auth, {"username": "test_user", "password": "123"}, format="json"
)
token = response.data.get("access")
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)

programs_response = self.client.post(
"/api/v1/jobs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec83/add_runtimejob/",
data={
"runtime_job": "runtime_job_4",
},
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)

programs_response = self.client.get(
"/api/v1/runtime_jobs/runtime_job_4/",
format="json",
)
self.assertEqual(programs_response.status_code, status.HTTP_200_OK)
self.assertEqual(
programs_response.json()["job"]["id"],
"1a7947f9-6ae8-4e3d-ac1e-e7d608deec83",
)

def test_list_runtimejob(self):
auth = reverse("rest_login")
response = self.client.post(
auth, {"username": "test_user", "password": "123"}, format="json"
)
token = response.data.get("access")
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + token)

programs_response = self.client.get(
"/api/v1/jobs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec83/list_runtimejob/",
format="json",
)
self.assertEqual(programs_response.json(), '["runtime_job_1", "runtime_job_2"]')

programs_response = self.client.get(
"/api/v1/jobs/1a7947f9-6ae8-4e3d-ac1e-e7d608deec82/list_runtimejob/",
format="json",
)
self.assertEqual(programs_response.json(), '["runtime_job_3"]')
2 changes: 1 addition & 1 deletion gateway/tests/fixtures/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
},
{
"model": "api.runtimejob",
"pk": "runtime_job_4",
"pk": "runtime_job_5",
"fields": {
"job": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec84",
"runtime_job": "runtime_job_5"
Expand Down

0 comments on commit 3af60ac

Please sign in to comment.