Modify get vms 78/100278/2
authorhewei-cmss <hewei@cmss.chinamobile.com>
Tue, 14 Jan 2020 11:04:39 +0000 (19:04 +0800)
committerwei he <hewei@cmss.chinamobile.com>
Wed, 15 Jan 2020 01:01:29 +0000 (01:01 +0000)
Issue-ID: VFC-1599

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

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

diff --git a/res/res/biz/vms_get.py b/res/res/biz/vms_get.py
new file mode 100644 (file)
index 0000000..091a278
--- /dev/null
@@ -0,0 +1,56 @@
+# 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 VmInstModel\r
+from res.resources.serializers import VmInfoSerializer\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetVmsService(BaseService):\r
+\r
+    def __init__(self):\r
+        super(GetVmsService, self).__init__()\r
+\r
+    def get_vms(self, vnf_instance_id):\r
+        return self.query_resources(\r
+            res_type="Vms",\r
+            logger=logger,\r
+            resources=VmInstModel.objects.filter(instid=vnf_instance_id),\r
+            cvt_fun=self.fill_vms_data,\r
+            res_serializer=VmInfoSerializer\r
+        )\r
+\r
+    def fill_vms_data(self, vm):\r
+        vms_data = {\r
+            "vmid": vm.vmid,\r
+            "vimid": vm.vimid,\r
+            "resouceid": vm.resouceid,\r
+            "insttype": vm.insttype,\r
+            "instid": vm.instid,\r
+            "vmname": vm.vmname,\r
+            "operationalstate": vm.operationalstate,\r
+            "tenant": vm.tenant,\r
+            "is_predefined": vm.is_predefined,\r
+            "security_groups": vm.security_groups,\r
+            "flavor_id": vm.flavor_id,\r
+            "availability_zone": vm.availability_zone,\r
+            "server_group": vm.server_group,\r
+            "volume_array": vm.volume_array,\r
+            "metadata": vm.metadata,\r
+            "nic_array": vm.nic_array\r
+        }\r
+        return vms_data\r
index f0906d4..dff2d94 100644 (file)
@@ -16,13 +16,14 @@ from django.conf.urls import url
 
 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
 from res.resources.views import views
 from res.resources.health_check_views import HealthCheckView
 
 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<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.getVms.as_view(), name='get_vms'),
+    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<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'),
diff --git a/res/res/resources/views/get_vms_view.py b/res/res/resources/views/get_vms_view.py
new file mode 100644 (file)
index 0000000..a58334a
--- /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 VmInfoSerializer\r
+from res.resources.views.base_view import view_safe_call_with_log\r
+from res.biz.vms_get import GetVmsService\r
+logger = logging.getLogger(__name__)\r
+\r
+\r
+class GetVmsView(APIView):\r
+    @swagger_auto_schema(\r
+        responses={\r
+            status.HTTP_200_OK: VmInfoSerializer(),\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 GetVmsService().get_vms(vnf_instance_id)\r
index a0a5897..c863f7d 100644 (file)
@@ -22,7 +22,6 @@ 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 VmInstModel\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
@@ -31,7 +30,6 @@ from res.resources.serializers import CpsInfoSerializer
 from res.resources.serializers import SubnetInfoSerializer\r
 from res.resources.serializers import NetworkInfoSerializer\r
 from res.resources.serializers import FlavorInfoSerializer\r
-from res.resources.serializers import VmInfoSerializer\r
 from res.resources.views.base_view import view_safe_call_with_log\r
 \r
 logger = logging.getLogger(__name__)\r
@@ -54,46 +52,6 @@ def query_resources(res_type, logger, resources, cvt_fun, res_serializer):
     )\r
 \r
 \r
-class getVms(APIView):\r
-    @swagger_auto_schema(\r
-        responses={\r
-            status.HTTP_200_OK: VmInfoSerializer(),\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="Vms",\r
-            logger=logger,\r
-            resources=VmInstModel.objects.filter(instid=vnfInstanceId),\r
-            cvt_fun=fill_vms_data,\r
-            res_serializer=VmInfoSerializer\r
-        )\r
-\r
-\r
-def fill_vms_data(vm):\r
-    vms_data = {\r
-        "vmid": vm.vmid,\r
-        "vimid": vm.vimid,\r
-        "resouceid": vm.resouceid,\r
-        "insttype": vm.insttype,\r
-        "instid": vm.instid,\r
-        "vmname": vm.vmname,\r
-        "operationalstate": vm.operationalstate,\r
-        "tenant": vm.tenant,\r
-        "is_predefined": vm.is_predefined,\r
-        "security_groups": vm.security_groups,\r
-        "flavor_id": vm.flavor_id,\r
-        "availability_zone": vm.availability_zone,\r
-        "server_group": vm.server_group,\r
-        "volume_array": vm.volume_array,\r
-        "metadata": vm.metadata,\r
-        "nic_array": vm.nic_array\r
-    }\r
-    return vms_data\r
-\r
-\r
 class getFlavors(APIView):\r
     @swagger_auto_schema(\r
         responses={\r