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.
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
18 from rest_framework import status
19 from drf_yasg.utils import swagger_auto_schema
21 from lcm.jobs.job_get import GetJobInfoService
22 from lcm.pub.utils.jobutil import JobUtil
23 from lcm.pub.utils.values import ignore_case_get
24 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
25 from lcm.pub.exceptions import NSLCMException
27 logger = logging.getLogger(__name__)
30 class JobView(APIView):
31 def get(self, request, job_id):
32 response_id = ignore_case_get(request.META, 'responseId')
33 ret = GetJobInfoService(job_id, response_id).do_biz()
34 return Response(data=ret)
37 request_body=JobUpdReqSerializer(),
39 status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
42 def post(self, request, job_id):
44 logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
46 req_serializer = JobUpdReqSerializer(data=request.data)
47 if not req_serializer.is_valid():
48 raise NSLCMException(req_serializer.errors)
50 jobs = JobUtil.query_job_status(job_id)
52 raise NSLCMException("Job(%s) does not exist.")
54 if jobs[-1].errcode != '255':
55 progress = request.data.get('progress')
56 desc = request.data.get('desc', '%s' % progress)
57 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
58 logger.debug("errcode=%s", errcode)
59 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
61 resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
62 if not resp_serializer.is_valid():
63 raise NSLCMException(req_serializer.errors)
65 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
66 except Exception as e:
67 resp_serializer = JobUpdRespSerializer(data={
70 if not resp_serializer.is_valid():
71 logger.error(resp_serializer.errors)
72 return Response(data={
74 'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
75 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)