summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorying.yunlong <ying.yunlong@zte.com.cn>2018-01-24 17:17:24 +0800
committerying.yunlong <ying.yunlong@zte.com.cn>2018-01-24 17:17:24 +0800
commit1f3ae91a97e741c2e1ec51bf4532b4db8a1f23f3 (patch)
treebfce2fa84862f6b4e3b7626e8cc276ff19907384
parentcf17ef9fdfe4a7847ab3200dfe85accdfd41a292 (diff)
Add vfc-vnfres getVolumes auto-swagger
Change-Id: I67884bbcfe455e28a8f562c963f963cb9b7134ad Issue-ID: VFC-679 Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r--res/res/resources/serializers.py4
-rw-r--r--res/res/resources/urls.py2
-rw-r--r--res/res/resources/views.py43
3 files changed, 32 insertions, 17 deletions
diff --git a/res/res/resources/serializers.py b/res/res/resources/serializers.py
index bce2939..4d8229f 100644
--- a/res/res/resources/serializers.py
+++ b/res/res/resources/serializers.py
@@ -15,6 +15,10 @@
from rest_framework import serializers
+class NoneSerializer(serializers.Serializer):
+ pass
+
+
class VolumeResponseSerializer(serializers.Serializer):
storageid = serializers.CharField(help_text="the storage id", required=True)
vimid = serializers.CharField(help_text="the vim id", required=True)
diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py
index 32b6f2a..0a53881 100644
--- a/res/res/resources/urls.py
+++ b/res/res/resources/urls.py
@@ -26,7 +26,7 @@ urlpatterns = [
url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.get_networks, name='get_networks'),
url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/subnets$', views.get_subnets, name='get_subnets'),
url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/cps$', views.get_cps, name='get_cps'),
- url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/volumes$', views.get_volumes, name='get_volumes'),
+ url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/volumes$', views.getVolumes.as_view(), name='get_volumes'),
url(r'^api/vnfres/v1/swagger.json$', SwaggerJsonView.as_view()),
]
diff --git a/res/res/resources/views.py b/res/res/resources/views.py
index f442931..e7fc4f6 100644
--- a/res/res/resources/views.py
+++ b/res/res/resources/views.py
@@ -17,6 +17,7 @@ import logging
import os
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
@@ -27,6 +28,7 @@ from res.pub.utils.values import ignore_case_get
from res.pub.utils.syscomm import fun_name
from res.pub.database.models import NfInstModel, StorageInstModel, NetworkInstModel, VLInstModel, \
VNFCInstModel, VmInstModel, FlavourInstModel, SubNetworkInstModel, CPInstModel
+from res.resources.serializers import VolumeInfoSerializer, NoneSerializer
logger = logging.getLogger(__name__)
@@ -339,22 +341,31 @@ def fill_cps_data(cp):
return cps_data
-@api_view(http_method_names=['GET'])
-def get_volumes(request, *args, **kwargs):
- logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
- try:
- vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
- volumes = StorageInstModel.objects.filter(instid=vnf_inst_id)
- if not volumes:
- return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
- arr = []
- for v in volumes:
- arr.append(fill_volumes_data(v))
- return Response(data={'resp_data': arr}, status=status.HTTP_200_OK)
- except Exception as e:
- logger.error(e.message)
- logger.error(traceback.format_exc())
- return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+class getVolumes(APIView):
+ @swagger_auto_schema(request_body=NoneSerializer(),
+ responses={
+ status.HTTP_200_OK: VolumeInfoSerializer(),
+ status.HTTP_404_NOT_FOUND: 'Volumes does not exist',
+ status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
+ def get(self, request, vnfInstanceId):
+ logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
+ try:
+ volumes = StorageInstModel.objects.filter(instid=vnfInstanceId)
+ if not volumes:
+ return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
+ arr = []
+ for v in volumes:
+ arr.append(fill_volumes_data(v))
+ volumeSerializer = VolumeInfoSerializer(data={'resp_data': arr})
+ isValid = volumeSerializer.is_valid()
+ if not isValid:
+ raise Exception(volumeSerializer.errors)
+
+ return Response(data=volumeSerializer.data, status=status.HTTP_200_OK)
+ except Exception as e:
+ logger.error(e.message)
+ logger.error(traceback.format_exc())
+ return Response(data={'error': 'Failed to get volumes'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def fill_volumes_data(v):