support fcaps 29/38329/1
authorzhangab <zhanganbing@chinamobile.com>
Mon, 26 Mar 2018 02:00:22 +0000 (10:00 +0800)
committerzhangab <zhanganbing@chinamobile.com>
Mon, 26 Mar 2018 02:18:18 +0000 (10:18 +0800)
Change-Id: Id7ef66b74a6569da3ad73f215f9dca863fea5f7a
Issue-ID: MULTICLOUD-201
Signed-off-by: zhangab <zhanganbing@chinamobile.com>
share/newton_base/openoapi/alarm.py [new file with mode: 0755]
share/newton_base/openoapi/hypervisor.py [new file with mode: 0755]
share/newton_base/openoapi/sample.py [new file with mode: 0755]
share/newton_base/openoapi/vmlist.py [new file with mode: 0755]

diff --git a/share/newton_base/openoapi/alarm.py b/share/newton_base/openoapi/alarm.py
new file mode 100755 (executable)
index 0000000..3cbcf9a
--- /dev/null
@@ -0,0 +1,92 @@
+# Copyright 2018 CMCC Technologies 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
+import threading\r
+import traceback\r
+from keystoneauth1.exceptions import HttpError\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from common.exceptions import VimDriverNewtonException\r
+from newton_base.util import VimDriverUtils\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+running_threads = {}\r
+running_thread_lock = threading.Lock()\r
+\r
+\r
+class Alarms(APIView):\r
+    service = {'service_type': 'metering',\r
+               'interface': 'public'}\r
+    keys_mapping = [\r
+        ("project_id", "tenantId"),\r
+        ("disk_format", "alarmType"),\r
+        ("container_format", "containerFormat")\r
+    ]\r
+\r
+    def get(self, request, vimid="", tenantid="", alarmid=""):\r
+        logger.debug("alarms--get::> %s" % request.data)\r
+        try:\r
+            # prepare request resource to vim instance\r
+            query = VimDriverUtils.get_query_part(request)\r
+            content, status_code = self.get_alarms(query, vimid, tenantid, alarmid)\r
+            return Response(data=content, status=status_code)\r
+        except VimDriverNewtonException as e:\r
+            return Response(data={'error': e.content}, status=e.status_code)\r
+        except HttpError as e:\r
+            logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))\r
+            return Response(data=e.response.json(), status=e.http_status)\r
+        except Exception as e:\r
+            logger.error(traceback.format_exc())\r
+            return Response(data={'error': str(e)},\r
+                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+\r
+    def get_alarms(self, query="", vimid="", tenantid="", alarmid=""):\r
+        logger.debug("alarms--get_alarms::> %s" % alarmid)\r
+\r
+        # prepare request resource to vim instance\r
+        req_resouce = "/v2/alarms"\r
+        if alarmid:\r
+            req_resouce += "/%s" % alarmid\r
+        elif query:\r
+            req_resouce += "?%s" % query\r
+\r
+        vim = VimDriverUtils.get_vim_info(vimid)\r
+        vim["domain"] = "Default"\r
+        sess = VimDriverUtils.get_session(vim, tenantid)\r
+        resp = sess.get(req_resouce, endpoint_filter=self.service)\r
+        content = resp.json()\r
+        vim_dict = {\r
+            "vimName": vim["name"],\r
+            "vimId": vim["vimId"],\r
+            "tenantId": tenantid,\r
+        }\r
+\r
+        '''if not alarmid:\r
+            # convert the key naming in alarms\r
+            for alarm in content["alarms"]:\r
+                VimDriverUtils.replace_key_by_mapping(alarm,\r
+                                                      self.keys_mapping)\r
+        else:\r
+            # convert the key naming in the alarm specified by id\r
+            #alarm = content.pop("alarm", None)\r
+            VimDriverUtils.replace_key_by_mapping(content,\r
+                                                  self.keys_mapping)\r
+            #content.update(alarm)'''\r
+\r
+        return content, resp.status_code\r
+\r
+    \r
diff --git a/share/newton_base/openoapi/hypervisor.py b/share/newton_base/openoapi/hypervisor.py
new file mode 100755 (executable)
index 0000000..147fbc4
--- /dev/null
@@ -0,0 +1,70 @@
+# Copyright 2018 CMCC Technologies 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
+\r
+import logging\r
+import threading\r
+import traceback\r
+from keystoneauth1.exceptions import HttpError\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from common.exceptions import VimDriverNewtonException\r
+from newton_base.util import VimDriverUtils\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+running_threads = {}\r
+running_thread_lock = threading.Lock()\r
+\r
+class Hypervisors(APIView):\r
+    service = {'service_type': 'compute',\r
+               'interface': 'public'}\r
+    keys_mapping = [\r
+        ("project_id", "tenantId"),\r
+        ("disk_format", "serverType"),\r
+        ("container_format", "containerFormat")\r
+    ]\r
+\r
+       \r
+    def get(self, request, vimid="", tenantid="", hypervisorid=""):\r
+        logger.debug("hypervisors--get::> %s" % request.data)\r
+        try:\r
+            query = VimDriverUtils.get_query_part(request)\r
+            content, status_code = self.get_hypervisors(query, vimid, tenantid, hypervisorid)\r
+            return Response(data=content, status=status_code)\r
+        except VimDriverNewtonException as e:\r
+            return Response(data={'error': e.content}, status=e.status_code)\r
+        except HttpError as e:\r
+            logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))\r
+            return Response(data=e.response.json(), status=e.http_status)\r
+        except Exception as e:\r
+            logger.error(traceback.format_exc())\r
+            return Response(data={'error': str(e)},\r
+                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+\r
+                                                       \r
+    def get_hypervisors(self, query="", vimid="", tenantid="", hypervisorid=""):\r
+        logger.debug("hypervisors--get::> %s" % hypervisorid)\r
+\r
+        req_resource = "/os-hypervisors"\r
+\r
+        vim = VimDriverUtils.get_vim_info(vimid)\r
+        vim["domain"] = "Default"\r
+        sess = VimDriverUtils.get_session(vim, tenantid)\r
+        resp = sess.get(req_resource, endpoint_filter = self.service)\r
+        content = resp.json()\r
+\r
+        return content, resp.status_code\r
+\r
diff --git a/share/newton_base/openoapi/sample.py b/share/newton_base/openoapi/sample.py
new file mode 100755 (executable)
index 0000000..81455d0
--- /dev/null
@@ -0,0 +1,92 @@
+# Copyright 2018 CMCC Technologies 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
+import threading\r
+import traceback\r
+from keystoneauth1.exceptions import HttpError\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from common.exceptions import VimDriverNewtonException\r
+from newton_base.util import VimDriverUtils\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+running_threads = {}\r
+running_thread_lock = threading.Lock()\r
+\r
+\r
+class Samples(APIView):\r
+    service = {'service_type': 'metering',\r
+               'interface': 'public'}\r
+    keys_mapping = [\r
+        ("project_id", "tenantId"),\r
+        ("disk_format", "sampleType"),\r
+        ("container_format", "containerFormat")\r
+    ]\r
+       \r
+\r
+    def get(self, request, vimid="", tenantid="", sampleid=""):\r
+        logger.debug("samples--get::> %s" % request.data)\r
+        try:\r
+            query = VimDriverUtils.get_query_part(request)\r
+            req_resouce = "/v2/samples"\r
+            req_resouce = req_resouce + "?" + query\r
+    \r
+            vim = VimDriverUtils.get_vim_info(vimid)\r
+           vim["url"] = "http://192.168.106.200:5000/v3"\r
+           vim["domain"] = "Default"\r
+            sess = VimDriverUtils.get_session(vim, tenantid)\r
+            resp = sess.get(req_resouce, endpoint_filter=self.service)\r
+            content = resp.json()\r
+            vim_dict = {\r
+                "vimName": vim["name"],\r
+                "vimId": vim["vimId"],\r
+                "tenantId": tenantid,\r
+            }\r
+            return Response(data=content, status=resp.status_code)\r
+        except VimDriverNewtonException as e:\r
+            return Response(data={'error2': e.content}, status=e.status_code)\r
+        except HttpError as e:\r
+            logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))\r
+            return Response(data=e.response.json(), status=e.http_status)\r
+        except Exception as e:\r
+            logger.error(traceback.format_exc())\r
+            return Response(data={'error4': str(e)},\r
+                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+                                                       \r
+\r
+    def get_samples(self, query="", vimid="", tenantid="", sampleid=""):\r
+        logger.debug("samples--get_samples::> %s" % sampleid)\r
+\r
+        req_resouce = "v2/samples"\r
+        if sampleid:\r
+            req_resouce += "/%s" % sampleid\r
+        elif query:\r
+            req_resouce += "?%s" % query\r
+\r
+        vim = VimDriverUtils.get_vim_info(vimid)\r
+        sess = VimDriverUtils.get_session(vim, tenantid)\r
+        resp = sess.get(req_resouce, endpoint_filter=self.service)\r
+        content = resp.json()\r
+        vim_dict = {\r
+            "vimName": vim["name"],\r
+            "vimId": vim["vimId"],\r
+            "tenantId": tenantid,\r
+        }\r
+        content.update(vim_dict)\r
+        return content, resp.status_code\r
+\r
+    \r
diff --git a/share/newton_base/openoapi/vmlist.py b/share/newton_base/openoapi/vmlist.py
new file mode 100755 (executable)
index 0000000..8d2e0c6
--- /dev/null
@@ -0,0 +1,74 @@
+# Copyright 2018 CMCC Technologies 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
+import threading\r
+import traceback\r
+from keystoneauth1.exceptions import HttpError\r
+from rest_framework import status\r
+from rest_framework.response import Response\r
+from rest_framework.views import APIView\r
+\r
+from common.exceptions import VimDriverNewtonException\r
+from newton_base.util import VimDriverUtils\r
+\r
+logger = logging.getLogger(__name__)\r
+\r
+running_threads = {}\r
+running_thread_lock = threading.Lock()\r
+\r
+\r
+class VMlist(APIView):\r
+    service = {'service_type': 'compute',\r
+               'interface': 'public'}\r
+    keys_mapping = [\r
+        ("project_id", "tenantId"),\r
+        ("disk_format", "serverType"),\r
+        ("container_format", "containerFormat")\r
+    ]\r
+\r
+    def get(self, request, vimid="", tenantid="", serverid=""):\r
+        logger.debug("servers--get::> %s" % request.data)\r
+        try:\r
+            query = VimDriverUtils.get_query_part(request)\r
+            content, status_code = self.get_servers(query, vimid, tenantid, serverid)\r
+            return Response(data=content, status=status_code)\r
+        except VimDriverNewtonException as e:\r
+            return Response(data={'error': e.content}, status=e.status_code)\r
+        except HttpError as e:\r
+            logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))\r
+            return Response(data=e.response.json(), status=e.http_status)\r
+        except Exception as e:\r
+            logger.error(traceback.format_exc())\r
+            return Response(data={'error': str(e)},\r
+                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)\r
+\r
+                                                       \r
+    def get_servers(self, query="", vimid="", tenantid="", serverid=""):\r
+        logger.debug("servers--get_servers::> %s" % serverid)\r
+\r
+        req_resouce = "/servers"\r
+        vim = VimDriverUtils.get_vim_info(vimid)\r
+        vim["domain"] = "Default"\r
+        sess = VimDriverUtils.get_session(vim, tenantid)\r
+        resp = sess.get(req_resouce, endpoint_filter=self.service)\r
+        content = resp.json()\r
+        vim_dict = {\r
+            "vimName": vim["name"],\r
+            "vimId": vim["vimId"],\r
+            "tenantId": tenantid,\r
+        }\r
+\r
+        return content, resp.status_code\r
+\r
+    \r