diff options
-rw-r--r-- | res/res/resources/serializers.py | 6 | ||||
-rw-r--r-- | res/res/resources/urls.py | 2 | ||||
-rw-r--r-- | res/res/resources/views.py | 44 |
3 files changed, 31 insertions, 21 deletions
diff --git a/res/res/resources/serializers.py b/res/res/resources/serializers.py index 6dd3d46..9f3c8bb 100644 --- a/res/res/resources/serializers.py +++ b/res/res/resources/serializers.py @@ -19,13 +19,13 @@ class FlavorResponseSerializer(serializers.Serializer): flavourid = serializers.CharField(help_text="flavourid", required=True) vimid = serializers.CharField(help_text="the vim id", required=True) resouceid = serializers.CharField(help_text="the resouce id", required=True) - tenant = serializers.IntegerField(help_text="tenant", required=True) + tenant = serializers.IntegerField(help_text="tenant", required=True, allow_null=True) instid = serializers.CharField(help_text="the inst id", required=True) name = serializers.CharField(help_text="name", required=True) extraspecs = serializers.CharField(help_text="extraspecs", required=True) - create_time = serializers.CharField(help_text="create_time", required=True) + create_time = serializers.CharField(help_text="create_time", required=True, allow_null=True) memory = serializers.IntegerField(help_text="memory", required=True) - vcpu = serializers.CharField(help_text="vcpu", required=True) + vcpu = serializers.IntegerField(help_text="vcpu", required=True) class FlavorInfoSerializer(serializers.Serializer): diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py index 4f3328e..0973e09 100644 --- a/res/res/resources/urls.py +++ b/res/res/resources/urls.py @@ -21,7 +21,7 @@ urlpatterns = [ url(r'^api/vnfres/v1/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', views.get_vnf, name='get_vnf'), 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\-\_]+)/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'), 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'), diff --git a/res/res/resources/views.py b/res/res/resources/views.py index e07c730..dbd2054 100644 --- a/res/res/resources/views.py +++ b/res/res/resources/views.py @@ -27,7 +27,7 @@ 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, \ - NetworkInfoSerializer + NetworkInfoSerializer, FlavorInfoSerializer logger = logging.getLogger(__name__) @@ -213,22 +213,32 @@ def fill_vms_data(vm): return vms_data -@api_view(http_method_names=['GET']) -def get_flavors(request, *args, **kwargs): - logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name()) - try: - vnf_inst_id = ignore_case_get(kwargs, "vnfInstanceId") - flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id) - if not flavours: - return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND) - arr = [] - for flavour in flavours: - arr.append(fill_flavours_data(flavour)) - 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 flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +class getFlavors(APIView): + @swagger_auto_schema( + responses={ + status.HTTP_200_OK: FlavorInfoSerializer(), + status.HTTP_404_NOT_FOUND: 'Flavours does not exist', + status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'}) + def get(self, request, vnfInstanceId): + logger.debug("Query all the flavors by vnfInstanceId[%s]", fun_name()) + try: + flavours = FlavourInstModel.objects.filter(instid=vnfInstanceId) + if not flavours: + return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND) + arr = [] + for flavour in flavours: + arr.append(fill_flavours_data(flavour)) + + flavorInfoSerializer = FlavorInfoSerializer(data={'resp_data': arr}) + isValid = flavorInfoSerializer.is_valid() + if not isValid: + raise Exception(flavorInfoSerializer.errors) + + return Response(data=flavorInfoSerializer.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 flavours'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def fill_flavours_data(f): |