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.job_get import GetJobInfoService
24 from lcm.pub.utils.jobutil import JobUtil
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):
34 input_job_id = openapi.Parameter(
38 type=openapi.TYPE_STRING)
39 input_response_id = openapi.Parameter(
42 description="response id",
43 type=openapi.TYPE_STRING)
46 operation_description="Query job",
47 manual_parameters=[input_job_id, input_response_id],
49 status.HTTP_200_OK: JobQueryRespSerializer(),
50 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
53 def get(self, request, job_id):
55 response_id = int(request.GET.get('responseId', 0))
56 ret = GetJobInfoService(job_id, response_id).do_biz()
57 resp_serializer = JobQueryRespSerializer(data=ret)
58 if not resp_serializer.is_valid():
59 raise NSLCMException(resp_serializer.errors)
60 return Response(data=ret, status=status.HTTP_200_OK)
61 except Exception as e:
62 logger.error(traceback.format_exc())
63 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
66 operation_description="Update job",
67 manual_parameters=[input_job_id],
68 request_body=JobUpdReqSerializer(),
70 status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
73 def post(self, request, job_id):
75 logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
77 req_serializer = JobUpdReqSerializer(data=request.data)
78 if not req_serializer.is_valid():
79 raise NSLCMException(req_serializer.errors)
81 jobs = JobUtil.query_job_status(job_id)
83 raise NSLCMException("Job(%s) does not exist.")
85 if jobs[-1].errcode != '255':
86 progress = request.data.get('progress')
87 desc = request.data.get('desc', '%s' % progress)
88 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
89 logger.debug("errcode=%s", errcode)
90 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
92 resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
93 if not resp_serializer.is_valid():
94 raise NSLCMException(req_serializer.errors)
96 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
97 except Exception as e:
98 resp_serializer = JobUpdRespSerializer(data={
101 if not resp_serializer.is_valid():
102 logger.error(resp_serializer.errors)
103 return Response(data={
105 'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
106 return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)