summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorying.yunlong <ying.yunlong@zte.com.cn>2018-01-22 16:37:44 +0800
committerying.yunlong <ying.yunlong@zte.com.cn>2018-01-22 18:33:46 +0800
commitac8f3283863de4f7137c6919d5815e3ec045aea1 (patch)
tree0ebfbbe8ab1926511aac6709dbe4296698da1505
parent36e8652d05c309e6ae6f6c8b7609fdfa8614f3ab (diff)
Add vfc-vnfmgr addvnf auto-swagger
Change-Id: Id10b8658adeb0ae1334b01b2b8c9995fe54c15c7 Issue-ID: VFC-670 Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r--mgr/mgr/vnfreg/serializers.py31
-rw-r--r--mgr/mgr/vnfreg/urls.py2
-rw-r--r--mgr/mgr/vnfreg/views.py62
3 files changed, 76 insertions, 19 deletions
diff --git a/mgr/mgr/vnfreg/serializers.py b/mgr/mgr/vnfreg/serializers.py
new file mode 100644
index 0000000..4b473f7
--- /dev/null
+++ b/mgr/mgr/vnfreg/serializers.py
@@ -0,0 +1,31 @@
+# Copyright 2018 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from rest_framework import serializers
+
+
+class VnfInfoSerializer(serializers.Serializer):
+ vnfInstId = serializers.CharField(help_text="the instance id of vnf", required=True)
+ ip = serializers.CharField(help_text="the ip of vnf", required=True)
+ port = serializers.CharField(help_text="the port of vnf", required=True)
+ username = serializers.CharField(help_text="the username of vnf", required=True)
+ password = serializers.CharField(help_text="the password of vnf", required=True)
+
+
+class ResponseSerializer(serializers.Serializer):
+ vnfInstId = serializers.CharField(help_text="the instance id of vnf", required=True)
+
+
+class ErrorSerializer(serializers.Serializer):
+ error = serializers.CharField(help_text="error message", required=True)
diff --git a/mgr/mgr/vnfreg/urls.py b/mgr/mgr/vnfreg/urls.py
index 9933b68..6203875 100644
--- a/mgr/mgr/vnfreg/urls.py
+++ b/mgr/mgr/vnfreg/urls.py
@@ -17,7 +17,7 @@ from mgr.vnfreg import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
- url(r'^api/vnfmgr/v1/vnfs$', views.add_vnf, name='add_vnf'),
+ url(r'^api/vnfmgr/v1/vnfs$', views.vnfmgr_addvnf.as_view(), name='add_vnf'),
url(r'^api/vnfmgr/v1/vnfs/(?P<vnfInstId>[0-9a-zA-Z\-\_]+)$', views.access_vnf, name='access_vnf'),
url(r'^api/vnfmgr/v1/configuration$', views.vnf_config, name='vnf_config'),
]
diff --git a/mgr/mgr/vnfreg/views.py b/mgr/mgr/vnfreg/views.py
index 23a6398..209bc8b 100644
--- a/mgr/mgr/vnfreg/views.py
+++ b/mgr/mgr/vnfreg/views.py
@@ -16,36 +16,62 @@ import logging
import json
import traceback
+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 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
logger = logging.getLogger(__name__)
-@api_view(http_method_names=['POST'])
-def add_vnf(request, *args, **kwargs):
- logger.info("Enter %s, data is %s", fun_name(), request.data)
- vnf_inst_id = ignore_case_get(request.data, "vnfInstId")
- try:
- if VnfRegModel.objects.filter(id=vnf_inst_id):
- raise Exception("Vnf(%s) already exists." % vnf_inst_id)
- VnfRegModel(
- id=vnf_inst_id,
- 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")).save()
- 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)
- return Response(data={"vnfInstId": vnf_inst_id}, status=status.HTTP_201_CREATED)
+def handler_exception(e):
+ logger.error(e.message)
+ logger.error(traceback.format_exc())
+ errorSerializer = ErrorSerializer(data={'error': e.message})
+ errorSerializer.is_valid()
+ return errorSerializer.data
+
+
+class vnfmgr_addvnf(APIView):
+ @swagger_auto_schema(request_body=VnfInfoSerializer(),
+ responses={
+ 201: ResponseSerializer(),
+ 500: ErrorSerializer()})
+ def post(self, request):
+ logger.info("Enter %s, data is %s", fun_name(), request.data)
+ requestSerializer = VnfInfoSerializer(data=request.data)
+ request_isValid = requestSerializer.is_valid()
+ try:
+ if not request_isValid:
+ raise Exception(requestSerializer.errors)
+
+ requestData = requestSerializer.data
+ vnf_inst_id = ignore_case_get(requestData, "vnfInstId")
+ if VnfRegModel.objects.filter(id=vnf_inst_id):
+ raise Exception("Vnf(%s) already exists." % vnf_inst_id)
+ VnfRegModel(
+ id=vnf_inst_id,
+ ip=ignore_case_get(requestData, "ip"),
+ port=ignore_case_get(requestData, "port"),
+ username=ignore_case_get(requestData, "username"),
+ password=ignore_case_get(requestData, "password")).save()
+
+ responseSerializer = ResponseSerializer(data={"vnfInstId": vnf_inst_id})
+ isValid = responseSerializer.is_valid()
+ if not isValid:
+ raise Exception(responseSerializer.errors)
+ except Exception as e:
+ errorData = handler_exception(e)
+ return Response(data=errorData, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+ return Response(data=responseSerializer.data, status=status.HTTP_201_CREATED)
@api_view(http_method_names=['GET', 'PUT', 'DELETE'])