aboutsummaryrefslogtreecommitdiffstats
path: root/catalog/jobs
diff options
context:
space:
mode:
authordyh <dengyuanhong@chinamobile.com>2019-09-04 09:52:48 +0800
committerdyh <dengyuanhong@chinamobile.com>2019-09-04 16:09:26 +0800
commita32c2b20207885d895bd96204cc166fca14db97b (patch)
tree1edd33368158dc5f057a0a9475dced3df6c3b24c /catalog/jobs
parent431a5a35a8e0a26d21c663167303696db8a7a2a6 (diff)
update for change to etsicatalog
Change-Id: Idc2a6950960a324964500a8c4701be422de2b782 Issue-ID: MODELING-216 Signed-off-by: dyh <dengyuanhong@chinamobile.com>
Diffstat (limited to 'catalog/jobs')
-rw-r--r--catalog/jobs/__init__.py13
-rw-r--r--catalog/jobs/job_get.py46
-rw-r--r--catalog/jobs/tests/__init__.py13
-rw-r--r--catalog/jobs/tests/tests.py40
-rw-r--r--catalog/jobs/urls.py20
-rw-r--r--catalog/jobs/views.py124
6 files changed, 256 insertions, 0 deletions
diff --git a/catalog/jobs/__init__.py b/catalog/jobs/__init__.py
new file mode 100644
index 0000000..c7b6818
--- /dev/null
+++ b/catalog/jobs/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/catalog/jobs/job_get.py b/catalog/jobs/job_get.py
new file mode 100644
index 0000000..32ee243
--- /dev/null
+++ b/catalog/jobs/job_get.py
@@ -0,0 +1,46 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import logging
+
+from catalog.pub.utils.jobutil import JobUtil
+
+logger = logging.getLogger(__name__)
+
+
+class GetJobInfoService(object):
+ def __init__(self, job_id, response_id=0):
+ self.job_id = job_id
+ self.response_id = response_id if response_id else 0
+
+ def do_biz(self):
+ logger.debug("[getjob]job_id=%s, response_id=%s", self.job_id, self.response_id)
+ jobs = JobUtil.query_job_status(self.job_id, self.response_id)
+ if not jobs:
+ return {"jobId": self.job_id}
+ ret = {
+ "jobId": self.job_id,
+ "responseDescriptor": {
+ "status": jobs[0].status,
+ "progress": jobs[0].progress,
+ "statusDescription": jobs[0].descp,
+ "errorCode": jobs[0].errcode,
+ "responseId": jobs[0].indexid,
+ "responseHistoryList": [
+ {
+ "status": job.status,
+ "progress": job.progress,
+ "statusDescription": job.descp,
+ "errorCode": job.errcode,
+ "responseId": job.indexid} for job in jobs[1:]]}}
+ return ret
diff --git a/catalog/jobs/tests/__init__.py b/catalog/jobs/tests/__init__.py
new file mode 100644
index 0000000..c7b6818
--- /dev/null
+++ b/catalog/jobs/tests/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/catalog/jobs/tests/tests.py b/catalog/jobs/tests/tests.py
new file mode 100644
index 0000000..460c854
--- /dev/null
+++ b/catalog/jobs/tests/tests.py
@@ -0,0 +1,40 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from django.test import TestCase, Client
+from rest_framework import status
+
+from catalog.pub.database.models import JobModel, JobStatusModel
+
+
+class JobsViewTest(TestCase):
+ def setUp(self):
+ self.job_id = 'test_job_id'
+ self.client = Client()
+
+ def tearDown(self):
+ JobModel.objects.all().delete()
+
+ def test_job_normal(self):
+ JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
+ JobStatusModel(indexid=1, jobid=self.job_id, status='inst', errcode='0', progress=20, descp='inst').save()
+ response = self.client.get("/api/catalog/v1/jobs/%s" % self.job_id)
+ self.assertEqual(status.HTTP_200_OK, response.status_code)
+
+ def test_job_when_jobid_not_exist(self):
+ job_id = 'test_new_job_id'
+ JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
+ JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save()
+ response = self.client.get("/api/catalog/v1/jobs/%s" % job_id)
+ self.assertIn('jobId', response.data)
+ self.assertNotIn('responseDescriptor', response.data)
diff --git a/catalog/jobs/urls.py b/catalog/jobs/urls.py
new file mode 100644
index 0000000..ea1fcd6
--- /dev/null
+++ b/catalog/jobs/urls.py
@@ -0,0 +1,20 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from django.conf.urls import url
+
+from catalog.jobs.views import JobView
+
+urlpatterns = [
+ url(r'^api/catalog/v1/jobs/(?P<job_id>[0-9a-zA-Z_-]+)$', JobView.as_view()),
+]
diff --git a/catalog/jobs/views.py b/catalog/jobs/views.py
new file mode 100644
index 0000000..123af00
--- /dev/null
+++ b/catalog/jobs/views.py
@@ -0,0 +1,124 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import logging
+
+from drf_yasg import openapi
+from drf_yasg.utils import swagger_auto_schema
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from catalog.jobs.job_get import GetJobInfoService
+from catalog.packages.serializers.catalog_serializers import GetJobResponseSerializer
+from catalog.packages.serializers.catalog_serializers import PostJobRequestSerializer
+from catalog.packages.serializers.catalog_serializers import PostJobResponseResultSerializer
+from catalog.pub.utils.jobutil import JobUtil
+from catalog.pub.utils.values import ignore_case_get
+
+logger = logging.getLogger(__name__)
+
+
+class JobView(APIView):
+
+ input_job_id = openapi.Parameter(
+ 'job_id',
+ openapi.IN_QUERY,
+ description="job id",
+ type=openapi.TYPE_STRING)
+ input_response_id = openapi.Parameter(
+ 'responseId',
+ openapi.IN_QUERY,
+ description="response id",
+ type=openapi.TYPE_STRING)
+
+ @swagger_auto_schema(
+ operation_description="Get job status",
+ manual_parameters=[input_job_id, input_response_id],
+ responses={
+ status.HTTP_200_OK: GetJobResponseSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ })
+ def get(self, request, job_id):
+ response_id = ignore_case_get(request.META, 'responseId')
+ ret = GetJobInfoService(job_id, response_id).do_biz()
+ response_serializer = GetJobResponseSerializer(data=ret)
+ validataion_error = self.handleValidatonError(
+ response_serializer, False)
+ if validataion_error:
+ return validataion_error
+
+ return Response(
+ data=response_serializer.data,
+ status=status.HTTP_200_OK)
+
+ @swagger_auto_schema(
+ request_body=PostJobRequestSerializer(),
+ operation_description="Update job status",
+ manual_parameters=[input_job_id],
+ responses={
+ status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ }
+ )
+ def post(self, request, job_id):
+ job_result_ok = {'result': 'ok'}
+
+ logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
+ jobs = JobUtil.query_job_status(job_id)
+ if len(jobs) > 0 and jobs[-1].errcode == '255':
+ return Response(data=job_result_ok)
+
+ request_serializer = PostJobRequestSerializer(data=request.data)
+ validataion_error = self.handleValidatonError(
+ request_serializer, True)
+ if not validataion_error:
+ return validataion_error
+
+ requestData = request_serializer.data
+ progress = ignore_case_get(requestData, "progress")
+ desc = ignore_case_get(requestData, "desc", '%s' % progress)
+ errcode = '0' if ignore_case_get(
+ requestData, 'errcode') in (
+ 'true', 'active') else '255'
+ logger.debug("errcode=%s", errcode)
+ JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
+
+ response_serializer = PostJobResponseResultSerializer(
+ data=job_result_ok)
+ validataion_error = self.handleValidatonError(
+ response_serializer, False)
+ if validataion_error:
+ return validataion_error
+
+ return Response(
+ data=response_serializer.data,
+ status=status.HTTP_202_ACCEPTED)
+
+ def handleValidatonError(self, base_serializer, is_request):
+ response = None
+
+ if not base_serializer.is_valid():
+ errormessage = base_serializer.errors
+ logger.error(errormessage)
+
+ if is_request:
+ message = 'Invalid request'
+ else:
+ message = 'Invalid response'
+ logger.error(message)
+
+ Response(
+ data={'result': message, 'msg': errormessage},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ return response