Add vfc-vnfres get_Cps auto-swagger 81/29081/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Thu, 25 Jan 2018 01:47:39 +0000 (09:47 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Thu, 25 Jan 2018 01:47:39 +0000 (09:47 +0800)
Change-Id: I450351c2c47a7faf93ca09e2ca4d759174b7941a
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 0a53881..5bcfb9a 100644 (file)
@@ -25,7 +25,7 @@ urlpatterns = [
     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\-\_]+)/cps$', views.get_cps, name='get_cps'),
+    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'),
     url(r'^api/vnfres/v1/swagger.json$', SwaggerJsonView.as_view()),
 ]
index e7fc4f6..049f584 100644 (file)
@@ -28,7 +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
+from res.resources.serializers import VolumeInfoSerializer, NoneSerializer, CpsInfoSerializer
 
 logger = logging.getLogger(__name__)
 
@@ -310,22 +310,31 @@ def fill_subnets_data(subnet):
     return subnets_data
 
 
-@api_view(http_method_names=['GET'])
-def get_cps(request, *args, **kwargs):
-    logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
-    try:
-        vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId")
-        cps = CPInstModel.objects.filter(ownerid=vnf_inst_id)
-        if not cps:
-            return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
-        arr = []
-        for cp in cps:
-            arr.append(fill_cps_data(cp))
-        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 cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+class getCps(APIView):
+    @swagger_auto_schema(request_body=NoneSerializer(),
+                         responses={
+                             status.HTTP_200_OK: CpsInfoSerializer(),
+                             status.HTTP_404_NOT_FOUND: 'Cps does not exist',
+                             status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'})
+    def get(self, request, vnfInstanceId):
+        logger.debug("Query all the cps by vnfInstanceId[%s]", fun_name())
+        try:
+            cps = CPInstModel.objects.filter(ownerid=vnfInstanceId)
+            if not cps:
+                return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
+            arr = []
+            for cp in cps:
+                arr.append(fill_cps_data(cp))
+            cpInfoSerializer = CpsInfoSerializer(data={'resp_data': arr})
+            isValid = cpInfoSerializer.is_valid()
+            if not isValid:
+                raise Exception(cpInfoSerializer.errors)
+
+            return Response(data=cpInfoSerializer.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 cps'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
 
 def fill_cps_data(cp):