Modify get flavors 65/100465/1
authorhewei-cmss <hewei@cmss.chinamobile.com>
Mon, 20 Jan 2020 04:27:37 +0000 (12:27 +0800)
committerhewei-cmss <hewei@cmss.chinamobile.com>
Mon, 20 Jan 2020 04:27:37 +0000 (12:27 +0800)
Issue-ID: VFC-1599

Signed-off-by: hewei-cmss <hewei@cmss.chinamobile.com>
Change-Id: I71f1e5a2316053c60b02075f8fce1fa8acccf7f3

res/res/biz/flavors_get.py [new file with mode: 0644]
res/res/resources/urls.py
res/res/resources/views/get_flavors_view.py [new file with mode: 0644]
res/res/resources/views/views.py

diff --git a/res/res/biz/flavors_get.py b/res/res/biz/flavors_get.py
new file mode 100644 (file)
index 0000000..5ae41e2
--- /dev/null
@@ -0,0 +1,51 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+#         http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+import logging\r
+\r
+from res.biz.base import BaseService\r
+from res.pub.database.models import FlavourInstModel\r
+from res.resources.serializers import FlavorInfoSerializer\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetFlavorsService(BaseService):\r
+\r
+    def __init__(self):\r
+        super(GetFlavorsService, self).__init__()\r
+\r
+    def get_flavors(self, vnf_instance_id):\r
+        return self.query_resources(\r
+            res_type="Flavors",\r
+            logger=logger,\r
+            resources=FlavourInstModel.objects.filter(\r
+                instid=vnf_instance_id),\r
+            cvt_fun=self.fill_flavours_data,\r
+            res_serializer=FlavorInfoSerializer\r
+        )\r
+\r
+    def fill_flavours_data(self, flavor):\r
+        flavours_data = {\r
+            "flavourid": flavor.flavourid,\r
+            "name": flavor.name,\r
+            "vcpu": flavor.vcpu,\r
+            "memory": flavor.memory,\r
+            "extraspecs": flavor.extraspecs,\r
+            "instid": flavor.instid,\r
+            "tenant": flavor.tenant,\r
+            "vimid": flavor.vimid,\r
+            "resouceid": flavor.resouceid,\r
+            "create_time": flavor.create_time\r
+        }\r
+        return flavours_data\r
index dff2d94..2088caf 100644 (file)
@@ -14,6 +14,7 @@
 
 from django.conf.urls import url
 
+from res.resources.views.get_flavors_view import GetFlavorsView
 from res.resources.views.get_vnfs_view import GetVnfView
 from res.resources.views.get_vnfs_view import GetVnfsView
 from res.resources.views.get_vms_view import GetVmsView
@@ -24,7 +25,7 @@ urlpatterns = [
     url(r'^api/vnfres/v1/vnfs/(?P<vnf_instance_id>[0-9a-zA-Z\-\_]+)$', GetVnfView.as_view(), name='get_vnf'),
     url(r'^api/vnfres/v1/vnfs$', GetVnfsView.as_view(), name='get_vnfs'),
     url(r'^api/vnfres/v1/(?P<vnf_instance_id>[0-9a-zA-Z\-\_]+)/vms$', GetVmsView.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<vnf_instance_id>[0-9a-zA-Z\-\_]+)/flavors$', GetFlavorsView.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/get_flavors_view.py b/res/res/resources/views/get_flavors_view.py
new file mode 100644 (file)
index 0000000..583ea04
--- /dev/null
@@ -0,0 +1,34 @@
+# Copyright @ 2020 China Mobile (SuZhou) Software Technology Co.,Ltd.\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License");\r
+# you may not use this file except in compliance with the License.\r
+# You may obtain a copy of the License at\r
+#\r
+#         http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+# See the License for the specific language governing permissions and\r
+# limitations under the License.\r
+import logging\r
+\r
+from rest_framework import status\r
+from rest_framework.views import APIView\r
+from drf_yasg.utils import swagger_auto_schema\r
+from res.resources.serializers import FlavorInfoSerializer\r
+from res.resources.views.base_view import view_safe_call_with_log\r
+from res.biz.flavors_get import GetFlavorsService\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetFlavorsView(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: FlavorInfoSerializer(),\r
+            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
+        }\r
+    )\r
+    @view_safe_call_with_log(logger=logger)\r
+    def get(self, request, vnf_instance_id):\r
+        return GetFlavorsService().get_flavors(vnf_instance_id)\r
index c863f7d..cb5e6b5 100644 (file)
@@ -22,14 +22,12 @@ from rest_framework.views import APIView
 from res.pub.exceptions import VNFRESException\r
 from res.pub.database.models import StorageInstModel\r
 from res.pub.database.models import NetworkInstModel\r
-from res.pub.database.models import FlavourInstModel\r
 from res.pub.database.models import SubNetworkInstModel\r
 from res.pub.database.models import CPInstModel\r
 from res.resources.serializers import VolumeInfoSerializer\r
 from res.resources.serializers import CpsInfoSerializer\r
 from res.resources.serializers import SubnetInfoSerializer\r
 from res.resources.serializers import NetworkInfoSerializer\r
-from res.resources.serializers import FlavorInfoSerializer\r
 from res.resources.views.base_view import view_safe_call_with_log\r
 \r
 logger = logging.getLogger(__name__)\r
@@ -52,40 +50,6 @@ def query_resources(res_type, logger, resources, cvt_fun, res_serializer):
     )\r
 \r
 \r
-class getFlavors(APIView):\r
-    @swagger_auto_schema(\r
-        responses={\r
-            status.HTTP_200_OK: FlavorInfoSerializer(),\r
-            status.HTTP_500_INTERNAL_SERVER_ERROR: 'internal error'\r
-        }\r
-    )\r
-    @view_safe_call_with_log(logger=logger)\r
-    def get(self, request, vnfInstanceId):\r
-        return query_resources(\r
-            res_type="Flavors",\r
-            logger=logger,\r
-            resources=FlavourInstModel.objects.filter(instid=vnfInstanceId),\r
-            cvt_fun=fill_flavours_data,\r
-            res_serializer=FlavorInfoSerializer\r
-        )\r
-\r
-\r
-def fill_flavours_data(f):\r
-    flavours_data = {\r
-        "flavourid": f.flavourid,\r
-        "name": f.name,\r
-        "vcpu": f.vcpu,\r
-        "memory": f.memory,\r
-        "extraspecs": f.extraspecs,\r
-        "instid": f.instid,\r
-        "tenant": f.tenant,\r
-        "vimid": f.vimid,\r
-        "resouceid": f.resouceid,\r
-        "create_time": f.create_time\r
-    }\r
-    return flavours_data\r
-\r
-\r
 class getNetworks(APIView):\r
     @swagger_auto_schema(\r
         responses={\r