Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add runtimejob handling additional apis #1197

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that runtime_job got passed in request somehow to display user friendly error?

Something like

if not request.data.get("runtime_job"):
    return Response({"message": "Got empty `runtime_job` field. Please, specify `runtime_job`."}, code / status=400)

)
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
Loading