1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from drf_yasg import openapi
18 from rest_framework.response import Response
19 from rest_framework.views import APIView
20 from rest_framework import status
21 from drf_yasg.utils import swagger_auto_schema
23 from lcm.jobs.enum import JOB_ERROR_CODE
24 from lcm.jobs.job_get import GetJobInfoService
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.jobs.api_model import JobUpdReq, JobUpdResp
27 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
28 from lcm.jobs.serializers import JobQueryRespSerializer
29 from lcm.pub.exceptions import BadRequestException, NSLCMException
31 logger = logging.getLogger(__name__)
34 def view_safe_call_with_log(logger):
35 def view_safe_call(func):
36 def wrapper(*args, **kwargs):
38 return func(*args, **kwargs)
39 except BadRequestException as e:
40 logger.error(e.args[0])
41 return make_error_resp(
43 status=status.HTTP_400_BAD_REQUEST
45 except NSLCMException as e:
46 logger.error(e.args[0])
47 return make_error_resp(
49 status=status.HTTP_500_INTERNAL_SERVER_ERROR
51 except Exception as e:
52 logger.error(e.args[0])
53 logger.error(traceback.format_exc())
54 return make_error_resp(
55 detail='Unexpected exception',
56 status=status.HTTP_500_INTERNAL_SERVER_ERROR
62 def make_error_resp(status, detail):
71 class JobView(APIView):
73 input_job_id = openapi.Parameter(
77 type=openapi.TYPE_STRING)
78 input_response_id = openapi.Parameter(
81 description="job response id",
82 type=openapi.TYPE_STRING)
85 operation_description="Query job",
86 manual_parameters=[input_job_id, input_response_id],
88 status.HTTP_200_OK: JobQueryRespSerializer(),
89 status.HTTP_500_INTERNAL_SERVER_ERROR: "HTTP_500_INTERNAL_SERVER_ERROR"
92 def get(self, request, job_id):
94 logger.debug("Enter JobView::get, job_id: %s, request= %s ", job_id, request.data)
95 response_id = int(request.GET.get('responseId', 0))
96 ret = GetJobInfoService(job_id, response_id).do_biz()
97 resp_serializer = JobQueryRespSerializer(data=ret)
98 if not resp_serializer.is_valid():
99 raise NSLCMException(resp_serializer.errors)
100 logger.debug("Leave JobView::get, response=%s", ret)
101 return Response(data=ret, status=status.HTTP_200_OK)
102 except Exception as e:
103 logger.error(traceback.format_exc())
104 return Response(data={'error': e.args[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
106 @swagger_auto_schema(
107 operation_description="Update job",
108 manual_parameters=[input_job_id],
109 request_body=JobUpdReqSerializer(),
111 status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
114 @view_safe_call_with_log(logger=logger)
115 def post(self, request, job_id):
117 logger.debug("Enter JobView:post, job_id=%s, request=%s", job_id, request.data)
118 req_serializer = JobUpdReqSerializer(data=request.data)
119 if not req_serializer.is_valid():
120 raise BadRequestException(req_serializer.errors)
122 jobs = JobUtil.query_job_status(job_id)
124 raise BadRequestException("Job(%s) does not exist." % job_id)
126 if jobs[-1].errcode != JOB_ERROR_CODE.ERROR:
127 job_up_req = JobUpdReq(**request.data)
128 desc = job_up_req.desc
129 no_err_list = ('true', 'active', '0')
130 err_code = JOB_ERROR_CODE.NO_ERROR if job_up_req.errcode in no_err_list else JOB_ERROR_CODE.ERROR
131 logger.debug("errcode=%s", err_code)
132 JobUtil.add_job_status(job_id, job_up_req.progress, desc, error_code=err_code)
133 job_update_resp = JobUpdResp('ok')
134 resp_serializer = JobUpdRespSerializer(job_update_resp)
135 logger.debug("Leave JobView::post, response=%s", job_update_resp)
136 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
137 except BadRequestException as e:
138 job_update_resp = JobUpdResp('error', e.args[0])
139 resp_serializer = JobUpdRespSerializer(job_update_resp)
140 return Response(data=resp_serializer.data, status=status.HTTP_400_BAD_REQUEST)
141 except Exception as e:
142 job_update_resp = JobUpdResp('error', e.args[0])
143 resp_serializer = JobUpdRespSerializer(job_update_resp)
144 return Response(data=resp_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)