Add vfc-vnfres getSubnet auto-swagger 55/29355/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Sat, 27 Jan 2018 07:03:19 +0000 (15:03 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Sat, 27 Jan 2018 07:03:19 +0000 (15:03 +0800)
Change-Id: I415dc51b647cd41d64451d588efe2b2eabb78b60
Issue-ID: VFC-679
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
res/res/resources/urls.py
res/res/resources/views.py

index e282e86..8698690 100644 (file)
@@ -23,7 +23,7 @@ urlpatterns = [
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.get_vms, name='get_vms'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/flavors$', views.get_flavors, name='get_flavors'),
     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\-\_]+)/subnets$', views.getSubnets.as_view(), name='get_subnets'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/cps$', views.getCps.as_view(), name='get_cps'),
     url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/volumes$', views.getVolumes.as_view(), name='get_volumes'),
 ]
index dfecd87..13ba7c4 100644 (file)
@@ -26,7 +26,7 @@ from res.pub.database.models import NfInstModel, StorageInstModel, NetworkInstMo
 from res.pub.exceptions import VNFRESException
 from res.pub.utils.syscomm import fun_name
 from res.pub.utils.values import ignore_case_get
-from res.resources.serializers import VolumeInfoSerializer, CpsInfoSerializer
+from res.resources.serializers import VolumeInfoSerializer, CpsInfoSerializer, SubnetInfoSerializer
 
 logger = logging.getLogger(__name__)
 
@@ -276,22 +276,31 @@ def fill_networks_data(network):
     return networks_data
 
 
-@api_view(http_method_names=['GET'])
-def get_subnets(request, *args, **kwargs):
-    logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
-    try:
-        vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
-        subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
-        if not subnets:
-            return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
-        arr = []
-        for subnet in subnets:
-            arr.append(fill_subnets_data(subnet))
-        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 subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+class getSubnets(APIView):
+    @swagger_auto_schema(
+        responses={
+            status.HTTP_200_OK: SubnetInfoSerializer(),
+            status.HTTP_404_NOT_FOUND: 'Subnets does not exist',
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
+    def get(self, request, vnfInstanceId):
+        logger.debug("Query all the subnets by vnfInstanceId[%s]", fun_name())
+        try:
+            subnets = SubNetworkInstModel.objects.filter(instid=vnfInstanceId)
+            if not subnets:
+                return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
+            arr = []
+            for subnet in subnets:
+                arr.append(fill_subnets_data(subnet))
+            subnetInfoSerializer = SubnetInfoSerializer(data={'resp_data': arr})
+            isValid = subnetInfoSerializer.is_valid()
+            if not isValid:
+                raise Exception(subnetInfoSerializer.errors)
+
+            return Response(data=subnetInfoSerializer.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 subnets'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
 
 def fill_subnets_data(subnet):