Add swagger auto generate of job 91/28891/11
authorfujinhua <fu.jinhua@zte.com.cn>
Tue, 23 Jan 2018 09:18:58 +0000 (17:18 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Tue, 23 Jan 2018 11:16:17 +0000 (19:16 +0800)
Add job query swagger auto generate logic

Change-Id: I0ea6ffcaf8314f1503db587d938ad55869f3cef9
Issue-ID: VFC-673
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/jobs/serializers.py
lcm/jobs/tests/tests.py
lcm/jobs/views.py

index df70dee..9bc293e 100644 (file)
@@ -18,23 +18,23 @@ from rest_framework import serializers
 class JobHistorySerializer(serializers.Serializer):
     status = serializers.CharField(help_text="Status of job", required=True)
     progress = serializers.CharField(help_text="Progress of job", required=True)
-    statusDescription = serializers.CharField(help_text="Description of job", required=True)
-    errorCode = serializers.CharField(help_text="Error code of job", required=True)
+    statusDescription = serializers.CharField(help_text="Description of job", required=False)
+    errorCode = serializers.CharField(help_text="Error code of job", required=False)
     responseId = serializers.CharField(help_text="Response index of job", required=True)
 
 
 class JobDescriptorSerializer(serializers.Serializer):
     status = serializers.CharField(help_text="Status of job", required=True)
     progress = serializers.CharField(help_text="Progress of job", required=True)
-    statusDescription = serializers.CharField(help_text="Description of job", required=True)
-    errorCode = serializers.CharField(help_text="Error code of job", required=True)
+    statusDescription = serializers.CharField(help_text="Description of job", required=False)
+    errorCode = serializers.CharField(help_text="Error code of job", required=False)
     responseId = serializers.CharField(help_text="Response index of job", required=True)
     responseHistoryList = JobHistorySerializer(help_text="History of job", many=True)
 
 
 class JobQueryRespSerializer(serializers.Serializer):
     jobId = serializers.CharField(help_text="UUID of job", required=True)
-    responseDescriptor = JobDescriptorSerializer(help_text="Descriptor of job", required=True)
+    responseDescriptor = JobDescriptorSerializer(help_text="Descriptor of job", required=False)
 
 
 class JobUpdReqSerializer(serializers.Serializer):
index 7b97c72..84ae29f 100644 (file)
@@ -27,14 +27,17 @@ class JobsViewTest(TestCase):
 
     def test_job(self):
         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()
+        JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
         response = self.client.get("/api/nslcm/v1/jobs/%s" % self.job_id)
-        self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
+        self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
+        self.assertIn('jobId', response.data)
+        self.assertIn('responseDescriptor', response.data)
 
     def test_non_exiting_job(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()
+        JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
         response = self.client.get("/api/nslcm/v1/jobs/%s" % job_id)
+        self.assertEqual(status.HTTP_200_OK, response.status_code)
         self.assertIn('jobId', response.data)
         self.assertNotIn('responseDescriptor', response.data)
index 1ffcf9c..55093bd 100644 (file)
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import logging
+import traceback
 
 from rest_framework.response import Response
 from rest_framework.views import APIView
@@ -22,16 +23,31 @@ from lcm.jobs.job_get import GetJobInfoService
 from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.utils.values import ignore_case_get
 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
+from lcm.jobs.serializers import JobQueryRespSerializer
 from lcm.pub.exceptions import NSLCMException
 
 logger = logging.getLogger(__name__)
 
 
 class JobView(APIView):
+    @swagger_auto_schema(
+        request_body=None,
+        responses={
+            status.HTTP_200_OK: JobQueryRespSerializer(),
+            status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
+        }
+    )
     def get(self, request, job_id):
-        response_id = ignore_case_get(request.META, 'responseId')
-        ret = GetJobInfoService(job_id, response_id).do_biz()
-        return Response(data=ret)
+        try:
+            response_id = ignore_case_get(request.META, 'responseId')
+            ret = GetJobInfoService(job_id, response_id).do_biz()
+            resp_serializer = JobQueryRespSerializer(data=ret)
+            if not resp_serializer.is_valid():
+                raise NSLCMException(resp_serializer.errors)
+            return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
+        except Exception as e:
+            logger.error(traceback.format_exc())
+            return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
     @swagger_auto_schema(
         request_body=JobUpdReqSerializer(),