diff options
author | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-01-30 16:15:22 +0800 |
---|---|---|
committer | ying.yunlong <ying.yunlong@zte.com.cn> | 2018-01-30 16:15:22 +0800 |
commit | 7d53102e9d32052a22140a62c70e5713876c2df9 (patch) | |
tree | e65a9e9209616fc9ec632dae7c97ff7c3c4ac41b /res | |
parent | db19cf9655cd08338164d859bc2900277e0cc697 (diff) |
Add vfc-vnfres getVnfs auto-swagger
Change-Id: I2ad74696337c68d1373377976e365a45393de855
Issue-ID: VFC-679
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
Diffstat (limited to 'res')
-rw-r--r-- | res/res/resources/serializers.py | 4 | ||||
-rw-r--r-- | res/res/resources/urls.py | 4 | ||||
-rw-r--r-- | res/res/resources/views.py | 46 |
3 files changed, 34 insertions, 20 deletions
diff --git a/res/res/resources/serializers.py b/res/res/resources/serializers.py index 31fb2f4..c32a55b 100644 --- a/res/res/resources/serializers.py +++ b/res/res/resources/serializers.py @@ -156,6 +156,10 @@ class VnfInfoSerializer(serializers.Serializer): instantiatedVnfInfo = instantiatedVnfInfoSerializer(help_text="instantiatedVnfInfo", required=True) +class VnfsInfoSerializer(serializers.Serializer): + resp_data = VnfInfoSerializer(help_text="the response data", many=True) + + class VmInfoSerializer(serializers.Serializer): resp_data = VmResponseSerializer(help_text="the response data", many=True) diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py index fa50f82..d7f8297 100644 --- a/res/res/resources/urls.py +++ b/res/res/resources/urls.py @@ -18,8 +18,8 @@ from rest_framework.urlpatterns import format_suffix_patterns from res.resources import views urlpatterns = [ - url(r'^api/vnfres/v1/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', views.getVnfs.as_view(), name='get_vnf'), - url(r'^api/vnfres/v1/vnfs$', views.get_vnfs, name='get_vnfs'), + url(r'^api/vnfres/v1/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', views.getVnf.as_view(), name='get_vnf'), + url(r'^api/vnfres/v1/vnfs$', views.getVnfs.as_view(), name='get_vnfs'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.getVms.as_view(), name='get_vms'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/flavors$', views.getFlavors.as_view(), name='get_flavors'), url(r'^api/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.getNetworks.as_view(), name='get_networks'), diff --git a/res/res/resources/views.py b/res/res/resources/views.py index dc0026a..b0085a8 100644 --- a/res/res/resources/views.py +++ b/res/res/resources/views.py @@ -17,7 +17,6 @@ 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 from rest_framework.views import APIView @@ -26,12 +25,12 @@ 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.resources.serializers import VolumeInfoSerializer, CpsInfoSerializer, SubnetInfoSerializer, \ - NetworkInfoSerializer, FlavorInfoSerializer, VmInfoSerializer, VnfInfoSerializer + NetworkInfoSerializer, FlavorInfoSerializer, VmInfoSerializer, VnfInfoSerializer, VnfsInfoSerializer logger = logging.getLogger(__name__) -class getVnfs(APIView): +class getVnf(APIView): @swagger_auto_schema( responses={ status.HTTP_200_OK: VnfInfoSerializer(), @@ -165,21 +164,32 @@ def fill_resp_data(vnf): return resp_data -@api_view(http_method_names=['GET']) -def get_vnfs(request): - logger.debug("Query all the vnfs[%s]", fun_name()) - try: - vnf_insts = NfInstModel.objects.all() - if not vnf_insts: - return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND) - arr = [] - for vnf_inst in vnf_insts: - arr.append(fill_resp_data(vnf_inst)) - 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 Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +class getVnfs(APIView): + @swagger_auto_schema( + responses={ + status.HTTP_200_OK: VnfsInfoSerializer(), + status.HTTP_404_NOT_FOUND: 'Vnfs does not exist', + status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'}) + def get(self, request): + logger.debug("Query all the vnfs[%s]", fun_name()) + try: + vnf_insts = NfInstModel.objects.all() + if not vnf_insts: + return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND) + arr = [] + for vnf_inst in vnf_insts: + arr.append(fill_resp_data(vnf_inst)) + + vnfsInfoSerializer = VnfsInfoSerializer(data={'resp_data': arr}) + isValid = vnfsInfoSerializer.is_valid() + if not isValid: + raise Exception(vnfsInfoSerializer.errors) + + return Response(data=vnfsInfoSerializer.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 Vnfs'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) class getVms(APIView): |