Add vfc-vnfres getVolumes auto-swagger 13/29013/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Wed, 24 Jan 2018 09:17:24 +0000 (17:17 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Wed, 24 Jan 2018 09:17:24 +0000 (17:17 +0800)
Change-Id: I67884bbcfe455e28a8f562c963f963cb9b7134ad
Issue-ID: VFC-679
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
res/res/resources/serializers.py
res/res/resources/urls.py
res/res/resources/views.py

index bce2939..4d8229f 100644 (file)
 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)
index 32b6f2a..0a53881 100644 (file)
@@ -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()),
 ]
 
index f442931..e7fc4f6 100644 (file)
@@ -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):