Add vfc-vnflcm get_jobs auto-swagger
[vfc/gvnfm/vnflcm.git] / lcm / lcm / jobs / views.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 import traceback
17
18 from drf_yasg.utils import swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22
23 from lcm.jobs.job_get import GetJobInfoService
24 from lcm.jobs.serializers import JobQueryRespSerializer
25 from lcm.pub.exceptions import NFLCMException
26 from lcm.pub.utils.values import ignore_case_get
27
28 logger = logging.getLogger(__name__)
29
30
31 class JobView(APIView):
32     @swagger_auto_schema(
33         request_body=None,
34         responses={
35             status.HTTP_200_OK: JobQueryRespSerializer(),
36             status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
37         }
38     )
39     def get(self, request, job_id):
40         try:
41             response_id = ignore_case_get(request.META, 'responseId')
42             ret = GetJobInfoService(job_id, response_id).do_biz()
43             resp_serializer = JobQueryRespSerializer(data=ret)
44             if not resp_serializer.is_valid():
45                 raise NFLCMException(resp_serializer.errors)
46             return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
47         except Exception as e:
48             logger.error(traceback.format_exc())
49             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)