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 rest_framework.response import Response
18 from rest_framework.views import APIView
19 from rest_framework import status
20 from drf_yasg.utils import swagger_auto_schema
22 from lcm.jobs.job_get import GetJobInfoService
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
26 from lcm.jobs.serializers import JobQueryRespSerializer
27 from lcm.pub.exceptions import NSLCMException
29 logger = logging.getLogger(__name__)
32 class JobView(APIView):
36 status.HTTP_200_OK: JobQueryRespSerializer(),
37 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
40 def get(self, request, job_id):
42 response_id = ignore_case_get(request.META, 'responseId')
43 ret = GetJobInfoService(job_id, response_id).do_biz()
44 resp_serializer = JobQueryRespSerializer(data=ret)
45 if not resp_serializer.is_valid():
46 raise NSLCMException(resp_serializer.errors)
47 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
48 except Exception as e:
49 logger.error(traceback.format_exc())
50 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
53 request_body=JobUpdReqSerializer(),
55 status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
58 def post(self, request, job_id):
60 logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
62 req_serializer = JobUpdReqSerializer(data=request.data)
63 if not req_serializer.is_valid():
64 raise NSLCMException(req_serializer.errors)
66 jobs = JobUtil.query_job_status(job_id)
68 raise NSLCMException("Job(%s) does not exist.")
70 if jobs[-1].errcode != '255':
71 progress = request.data.get('progress')
72 desc = request.data.get('desc', '%s' % progress)
73 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
74 logger.debug("errcode=%s", errcode)
75 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
77 resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
78 if not resp_serializer.is_valid():
79 raise NSLCMException(req_serializer.errors)
81 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
82 except Exception as e:
83 resp_serializer = JobUpdRespSerializer(data={
86 if not resp_serializer.is_valid():
87 logger.error(resp_serializer.errors)
88 return Response(data={
90 'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
91 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)