diff options
Diffstat (limited to 'res')
-rw-r--r-- | res/res/resources/urls.py | 2 | ||||
-rw-r--r-- | res/res/resources/views.py | 45 |
2 files changed, 29 insertions, 18 deletions
diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py index 8698690..4f3328e 100644 --- a/res/res/resources/urls.py +++ b/res/res/resources/urls.py @@ -22,7 +22,7 @@ urlpatterns = [ url(r'^api/vnfres/v1/vnfs$', views.get_vnfs, name='get_vnfs'), 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\-\_]+)/networks$', views.getNetworks.as_view(), name='get_networks'), 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'), diff --git a/res/res/resources/views.py b/res/res/resources/views.py index 13ba7c4..e07c730 100644 --- a/res/res/resources/views.py +++ b/res/res/resources/views.py @@ -26,7 +26,8 @@ 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, SubnetInfoSerializer +from res.resources.serializers import VolumeInfoSerializer, CpsInfoSerializer, SubnetInfoSerializer, \ + NetworkInfoSerializer logger = logging.getLogger(__name__) @@ -246,22 +247,32 @@ def fill_flavours_data(f): return flavours_data -@api_view(http_method_names=['GET']) -def get_networks(request, *args, **kwargs): - logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name()) - try: - vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId") - networks = NetworkInstModel.objects.filter(instid=vnf_inst_id) - if not networks: - return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND) - arr = [] - for network in networks: - arr.append(fill_networks_data(network)) - 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 networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +class getNetworks(APIView): + @swagger_auto_schema( + responses={ + status.HTTP_200_OK: NetworkInfoSerializer(), + status.HTTP_404_NOT_FOUND: 'Networks does not exist', + status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'}) + def get(self, request, vnfInstanceId): + logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name()) + try: + networks = NetworkInstModel.objects.filter(instid=vnfInstanceId) + if not networks: + return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND) + arr = [] + for network in networks: + arr.append(fill_networks_data(network)) + + networkInfoSerializer = NetworkInfoSerializer(data={'resp_data': arr}) + isValid = networkInfoSerializer.is_valid() + if not isValid: + raise Exception(networkInfoSerializer.errors) + + return Response(data=networkInfoSerializer.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 networks'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def fill_networks_data(network): |