diff options
author | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-01-23 13:33:23 +0800 |
---|---|---|
committer | yunlong ying <ying.yunlong@zte.com.cn> | 2018-01-23 06:35:52 +0000 |
commit | e2fbd9ef65d63fdbba92d27c868761f4dbbb733a (patch) | |
tree | 8c7ae556e454b1654ac1d80f7833f59510108a95 /mgr | |
parent | e6afb9a8a792bdab4055413540a586d92eb6c924 (diff) |
Add auto-swagger for access vnf
Change-Id: I62894e88b03f64ec48d463de2d3968f0da9481c0
Issue-ID: VFC-670
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
Diffstat (limited to 'mgr')
-rw-r--r-- | mgr/mgr/vnfreg/serializers.py | 4 | ||||
-rw-r--r-- | mgr/mgr/vnfreg/views.py | 47 |
2 files changed, 42 insertions, 9 deletions
diff --git a/mgr/mgr/vnfreg/serializers.py b/mgr/mgr/vnfreg/serializers.py index 4b473f7..c47edcd 100644 --- a/mgr/mgr/vnfreg/serializers.py +++ b/mgr/mgr/vnfreg/serializers.py @@ -29,3 +29,7 @@ class ResponseSerializer(serializers.Serializer): class ErrorSerializer(serializers.Serializer): error = serializers.CharField(help_text="error message", required=True) + + +class NoneSerializer(serializers.Serializer): + pass diff --git a/mgr/mgr/vnfreg/views.py b/mgr/mgr/vnfreg/views.py index 209bc8b..86e1c7f 100644 --- a/mgr/mgr/vnfreg/views.py +++ b/mgr/mgr/vnfreg/views.py @@ -16,6 +16,7 @@ import logging import json import traceback +from drf_yasg import openapi from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.decorators import api_view @@ -26,7 +27,7 @@ from mgr.pub.utils.values import ignore_case_get from mgr.pub.utils.syscomm import fun_name from mgr.pub.database.models import VnfRegModel from mgr.pub.utils import restcall -from mgr.vnfreg.serializers import ErrorSerializer, VnfInfoSerializer, ResponseSerializer +from mgr.vnfreg.serializers import ErrorSerializer, VnfInfoSerializer, ResponseSerializer, NoneSerializer logger = logging.getLogger(__name__) @@ -74,8 +75,29 @@ class vnfmgr_addvnf(APIView): return Response(data=responseSerializer.data, status=status.HTTP_201_CREATED) +@swagger_auto_schema(method='put', + request_body=VnfInfoSerializer(), + responses={ + 202: NoneSerializer(), + 500: ErrorSerializer()}) +@swagger_auto_schema(method='delete', + responses={ + 204: NoneSerializer(), + 500: ErrorSerializer()}) +@swagger_auto_schema(methods=['get'], + manual_parameters=[ + openapi.Parameter('test', + openapi.IN_QUERY, + "test manual param", + type=openapi.TYPE_BOOLEAN + ), ], + responses={ + 200: openapi.Response('response description', VnfInfoSerializer()), + 500: ErrorSerializer()}) @api_view(http_method_names=['GET', 'PUT', 'DELETE']) def access_vnf(request, *args, **kwargs): + requestSerializer = VnfInfoSerializer(data=request.data) + request_isValid = requestSerializer.is_valid() vnf_inst_id = ignore_case_get(kwargs, "vnfInstId") logger.info("Enter %s, method is %s, ", fun_name(), request.method) logger.info("vnfInstId is %s, data is %s", vnf_inst_id, request.data) @@ -85,19 +107,27 @@ def access_vnf(request, *args, **kwargs): err_msg = "Vnf(%s) does not exist." % vnf_inst_id return Response(data={'error': err_msg}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': - ret = { + resp = { "vnfInstId": vnf_inst_id, "ip": vnf[0].ip, "port": vnf[0].port, "username": vnf[0].username, "password": vnf[0].password } + responseSerializer = VnfInfoSerializer(data=resp) + if not responseSerializer.is_valid(): + raise Exception(responseSerializer.errors) + ret = responseSerializer.data normal_status = status.HTTP_200_OK elif request.method == 'PUT': - ip = ignore_case_get(request.data, "ip") - port = ignore_case_get(request.data, "port") - username = ignore_case_get(request.data, "username") - password = ignore_case_get(request.data, "password") + if not request_isValid: + raise Exception(requestSerializer.errors) + + requestData = requestSerializer.data + ip = ignore_case_get(requestData, "ip") + port = ignore_case_get(requestData, "port") + username = ignore_case_get(requestData, "username") + password = ignore_case_get(requestData, "password") if ip: vnf[0].ip = ip if port: @@ -114,9 +144,8 @@ def access_vnf(request, *args, **kwargs): ret = {} normal_status = status.HTTP_204_NO_CONTENT except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + errorData = handler_exception(e) + return Response(data=errorData, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(data=ret, status=normal_status) |