diff --git a/gateway/api/views.py b/gateway/api/views.py index 7edfe2b49..191263c93 100644 --- a/gateway/api/views.py +++ b/gateway/api/views.py @@ -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. diff --git a/gateway/tests/api/test_v1_program.py b/gateway/tests/api/test_v1_program.py index 4d94ea906..af0296951 100644 --- a/gateway/tests/api/test_v1_program.py +++ b/gateway/tests/api/test_v1_program.py @@ -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): @@ -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"]') diff --git a/gateway/tests/fixtures/fixtures.json b/gateway/tests/fixtures/fixtures.json index 0cb4a2f07..7823fcfa4 100644 --- a/gateway/tests/fixtures/fixtures.json +++ b/gateway/tests/fixtures/fixtures.json @@ -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"