update vnf instance name
[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 import traceback
16
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
22
23 from lcm.jobs.job_get import GetJobInfoService
24 from lcm.pub.utils.jobutil import JobUtil
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer
27 from lcm.jobs.serializers import JobQueryRespSerializer
28 from lcm.pub.exceptions import NSLCMException
29
30 logger = logging.getLogger(__name__)
31
32
33 class JobView(APIView):
34     @swagger_auto_schema(
35         manual_parameters=[
36             openapi.Parameter('responseId',
37                               openapi.IN_QUERY,
38                               "responseId",
39                               type=openapi.TYPE_INTEGER
40                               ),
41         ],
42         responses={
43             status.HTTP_200_OK: JobQueryRespSerializer(),
44             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
45         }
46     )
47     def get(self, request, job_id):
48         try:
49             response_id = ignore_case_get(request.META, 'responseId')
50             ret = GetJobInfoService(job_id, response_id).do_biz()
51             # resp_serializer = JobQueryRespSerializer(data=ret)
52             # if not resp_serializer.is_valid():
53             # raise NSLCMException(resp_serializer.errors)
54             # return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
55             return Response(data=ret, status=status.HTTP_200_OK)
56         except Exception as e:
57             logger.error(traceback.format_exc())
58             return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
59
60     @swagger_auto_schema(
61         request_body=JobUpdReqSerializer(),
62         responses={
63             status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
64         }
65     )
66     def post(self, request, job_id):
67         try:
68             logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
69
70             req_serializer = JobUpdReqSerializer(data=request.data)
71             if not req_serializer.is_valid():
72                 raise NSLCMException(req_serializer.errors)
73
74             jobs = JobUtil.query_job_status(job_id)
75             if not jobs:
76                 raise NSLCMException("Job(%s) does not exist.")
77
78             if jobs[-1].errcode != '255':
79                 progress = request.data.get('progress')
80                 desc = request.data.get('desc', '%s' % progress)
81                 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
82                 logger.debug("errcode=%s", errcode)
83                 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
84
85             resp_serializer = JobUpdRespSerializer(data={'result': 'ok'})
86             if not resp_serializer.is_valid():
87                 raise NSLCMException(req_serializer.errors)
88
89             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)
90         except Exception as e:
91             resp_serializer = JobUpdRespSerializer(data={
92                 'result': 'error',
93                 'msg': e.message})
94             if not resp_serializer.is_valid():
95                 logger.error(resp_serializer.errors)
96                 return Response(data={
97                     'result': 'error',
98                     'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED)
99             return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED)