diff options
author | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-02-06 18:55:16 +0800 |
---|---|---|
committer | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-02-06 18:55:16 +0800 |
commit | bbca42c0dc51dbffaa06ce376aef15fa7b7c850d (patch) | |
tree | efbae1763b498b98b8bfb93aef7376a92666f270 | |
parent | fefc5d626f39a11df049cbea557fad735ae54660 (diff) |
Add vfc-ztevnfmdriver heal auto-swagger
Change-Id: If42870aae9fa914dc57034847f1e1cc0cf4e8984
Issue-ID: VFC-736
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r-- | zte/vmanager/driver/interfaces/serializers.py | 13 | ||||
-rw-r--r-- | zte/vmanager/driver/interfaces/urls.py | 2 | ||||
-rw-r--r-- | zte/vmanager/driver/interfaces/views.py | 93 |
3 files changed, 65 insertions, 43 deletions
diff --git a/zte/vmanager/driver/interfaces/serializers.py b/zte/vmanager/driver/interfaces/serializers.py index abf0f4de..70a6541a 100644 --- a/zte/vmanager/driver/interfaces/serializers.py +++ b/zte/vmanager/driver/interfaces/serializers.py @@ -65,3 +65,16 @@ class HealReqSerializer(serializers.Serializer): required=False, allow_null=True) affectedvm = AffectedvmSerializer(help_text='affectedvm', many=True) + + +class HealRespSerializer(serializers.Serializer): + jobid = serializers.CharField( + help_text="jobid", + max_length=255, + required=True, + allow_null=True) + nfInstanceId = serializers.CharField( + help_text="nfInstanceId", + max_length=255, + required=True, + allow_null=False) diff --git a/zte/vmanager/driver/interfaces/urls.py b/zte/vmanager/driver/interfaces/urls.py index 5f972b47..07df352b 100644 --- a/zte/vmanager/driver/interfaces/urls.py +++ b/zte/vmanager/driver/interfaces/urls.py @@ -29,6 +29,6 @@ urlpatterns = [ url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/scale$', views.scale, name='scale'), url(r'^api/ztevnfmdriver/v1/(?P<vnfmid>[0-9a-zA-Z\-\_]+)/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/heal$', - views.heal, name='heal'), + views.Heal.as_view(), name='heal'), url(r'^samples/$', views.samples, name='samples') ] diff --git a/zte/vmanager/driver/interfaces/views.py b/zte/vmanager/driver/interfaces/views.py index 27b57c7a..cd51c694 100644 --- a/zte/vmanager/driver/interfaces/views.py +++ b/zte/vmanager/driver/interfaces/views.py @@ -18,9 +18,13 @@ import logging import traceback import os +from drf_yasg.utils import swagger_auto_schema +from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response +from rest_framework.views import APIView +from driver.interfaces.serializers import HealReqSerializer, HealRespSerializer from driver.pub.utils import restcall from driver.pub.utils.restcall import req_by_msb from driver.pub.config.config import VNF_FTP @@ -433,48 +437,53 @@ def scale(request, *args, **kwargs): return Response(data=resp_data, status=ret[2]) -@api_view(http_method_names=['POST']) -def heal(request, *args, **kwargs): - logger.info("====heal_vnf===") - try: - logger.info("request.data = %s", request.data) - logger.info("requested_url = %s", request.get_full_path()) - vnfm_id = ignorcase_get(kwargs, "vnfmid") - nf_instance_id = ignorcase_get(kwargs, "vnfInstanceId") - ret = get_vnfminfo_from_nslcm(vnfm_id) - if ret[0] != 0: - return Response(data={'error': ret[1]}, status=ret[2]) - vnfm_info = json.JSONDecoder().decode(ret[1]) - data = {} - data['action'] = ignorcase_get(request.data, 'action') - affectedvm = ignorcase_get(request.data, 'affectedvm') - data['affectedvm'] = [] - if isinstance(affectedvm, list): - data['affectedvm'] = affectedvm - else: - data['affectedvm'].append(affectedvm) - data['lifecycleoperation'] = 'operate' - data['isgrace'] = 'force' - - logger.info("data = %s", data) - ret = restcall.call_req( - base_url=ignorcase_get(vnfm_info, "url"), - user=ignorcase_get(vnfm_info, "userName"), - passwd=ignorcase_get(vnfm_info, "password"), - auth_type=restcall.rest_no_auth, - resource='/api/v1/nf_m_i/nfs/{vnfInstanceID}/vms/operation'.format(vnfInstanceID=nf_instance_id), - method='post', - content=json.JSONEncoder().encode(data)) - logger.info("ret=%s", ret) - if ret[0] != 0: - return Response(data={'error': 'heal error'}, status=ret[2]) - resp_data = json.JSONDecoder().decode(ret[1]) - logger.info("resp_data=%s", resp_data) - except Exception as e: - logger.error("Error occurred when healing VNF,error:%s", e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'heal expection'}, status='500') - return Response(data=resp_data, status=ret[2]) +class Heal(APIView): + @swagger_auto_schema( + request_body=HealReqSerializer(), + responses={ + status.HTTP_202_ACCEPTED: HealRespSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } + ) + def post(self, request, vnfmid, vnfInstanceId): + logger.info("====heal_vnf===") + try: + logger.info("request.data = %s", request.data) + logger.info("requested_url = %s", request.get_full_path()) + ret = get_vnfminfo_from_nslcm(vnfmid) + if ret[0] != 0: + return Response(data={'error': ret[1]}, status=ret[2]) + vnfm_info = json.JSONDecoder().decode(ret[1]) + data = {} + data['action'] = ignorcase_get(request.data, 'action') + affectedvm = ignorcase_get(request.data, 'affectedvm') + data['affectedvm'] = [] + if isinstance(affectedvm, list): + data['affectedvm'] = affectedvm + else: + data['affectedvm'].append(affectedvm) + data['lifecycleoperation'] = 'operate' + data['isgrace'] = 'force' + + logger.info("data = %s", data) + ret = restcall.call_req( + base_url=ignorcase_get(vnfm_info, "url"), + user=ignorcase_get(vnfm_info, "userName"), + passwd=ignorcase_get(vnfm_info, "password"), + auth_type=restcall.rest_no_auth, + resource='/api/v1/nf_m_i/nfs/{vnfInstanceID}/vms/operation'.format(vnfInstanceID=vnfInstanceId), + method='post', + content=json.JSONEncoder().encode(data)) + logger.info("ret=%s", ret) + if ret[0] != 0: + raise Exception('heal error') + resp_data = json.JSONDecoder().decode(ret[1]) + logger.info("resp_data=%s", resp_data) + return Response(data=resp_data, status=status.HTTP_202_ACCEPTED) + except Exception as e: + logger.error("Error occurred when healing VNF,error:%s", e.message) + logger.error(traceback.format_exc()) + return Response(data={'error': 'heal expection'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def get_vdus(nf_model, aspect_id): |