Add swagger auto generate of job
[vfc/nfvo/lcm.git] / lcm / jobs / views.py
1 # Copyright 2016 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 import logging
15
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
20
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
26
27 logger = logging.getLogger(__name__)
28
29
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)
35
36     @swagger_auto_schema(
37         request_body=JobUpdReqSerializer(),
38         responses={
39             status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
40         }
41     )
42     def post(self, request, job_id):
43         try:
44             logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
45
46             req_serializer = JobUpdReqSerializer(data=request.data)
47             if not req_serializer.is_valid():
48                 raise NSLCMException(req_serializer.errors)
49
50             jobs = JobUtil.query_job_status(job_id)
51             if not jobs:
52                 raise NSLCMException("Job(%s) does not exist.")
53
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)
60
61             resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
62             if not resp_serializer.is_valid():
63                 raise NSLCMException(req_serializer.errors)
64
65             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
66         except Exception as e:
67             resp_serializer = JobUpdRespSerializer(data={
68                 'result': 'error',
69                 'msg': e.message})
70             if not resp_serializer.is_valid():
71                 logger.error(resp_serializer.errors)
72                 return Response(data={
73                     'result': 'error',
74                     'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
75             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)