From 43710aa9466768b43c24757c37095515eaafcb09 Mon Sep 17 00:00:00 2001 From: "ying.yunlong" Date: Sat, 27 Jan 2018 15:03:19 +0800 Subject: Add vfc-vnfres getSubnet auto-swagger Change-Id: I415dc51b647cd41d64451d588efe2b2eabb78b60 Issue-ID: VFC-679 Signed-off-by: ying.yunlong --- res/res/resources/urls.py | 2 +- res/res/resources/views.py | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) (limited to 'res') diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py index e282e86..8698690 100644 --- a/res/res/resources/urls.py +++ b/res/res/resources/urls.py @@ -23,7 +23,7 @@ urlpatterns = [ url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/vms$', views.get_vms, name='get_vms'), url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/flavors$', views.get_flavors, name='get_flavors'), url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/networks$', views.get_networks, name='get_networks'), - url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/subnets$', views.get_subnets, name='get_subnets'), + url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/subnets$', views.getSubnets.as_view(), name='get_subnets'), url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/cps$', views.getCps.as_view(), name='get_cps'), url(r'^api/vnfres/v1/(?P[0-9a-zA-Z\-\_]+)/volumes$', views.getVolumes.as_view(), name='get_volumes'), ] diff --git a/res/res/resources/views.py b/res/res/resources/views.py index dfecd87..13ba7c4 100644 --- a/res/res/resources/views.py +++ b/res/res/resources/views.py @@ -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): -- cgit 1.2.3-korg