From 1308091491b3efebb3ecb511a6bafaedfc34b40d Mon Sep 17 00:00:00 2001 From: Haibin Huang Date: Tue, 12 Mar 2019 00:19:17 +0800 Subject: Increase ocata cover rate Delete resource code from ocata and use newton_base code. Change-Id: I3f66fa63232fdd9b113e0b61ff9d0a8aa520f96f Issue-ID: MULTICLOUD-501 Signed-off-by: Haibin Huang --- ocata/ocata/resource/tests/test_events.py | 350 ----------------- ocata/ocata/resource/tests/tests_infra_workload.py | 4 +- ocata/ocata/resource/views/__init__.py | 13 - ocata/ocata/resource/views/capacity.py | 144 ------- ocata/ocata/resource/views/events.py | 99 ----- ocata/ocata/resource/views/infra_workload.py | 420 --------------------- ocata/ocata/urls.py | 8 +- 7 files changed, 4 insertions(+), 1034 deletions(-) delete mode 100644 ocata/ocata/resource/tests/test_events.py delete mode 100644 ocata/ocata/resource/views/__init__.py delete mode 100644 ocata/ocata/resource/views/capacity.py delete mode 100644 ocata/ocata/resource/views/events.py delete mode 100644 ocata/ocata/resource/views/infra_workload.py (limited to 'ocata') diff --git a/ocata/ocata/resource/tests/test_events.py b/ocata/ocata/resource/tests/test_events.py deleted file mode 100644 index 238559fa..00000000 --- a/ocata/ocata/resource/tests/test_events.py +++ /dev/null @@ -1,350 +0,0 @@ -# Copyright (c) 2017-2018 Wind River Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import mock - -from rest_framework import status - -from newton_base.tests import mock_info -from newton_base.tests import test_base -from newton_base.util import VimDriverUtils - -MOCK_GET_SERVERS_DETAIL_RESPONSE = { - "servers": [ - { - "accessIPv4": "", - "OS-EXT-SRV-ATTR:instance_name": "instance-0000000a", - "OS-SRV-USG:terminated_at": "", - "accessIPv6": "", - "config_drive": "", - "OS-DCF:diskConfig": "AUTO", - "updated": "2018-03-27T02:17:12Z", - "metadata": {}, - "id": "12f5b1d0-fe5c-469f-a7d4-b62a91134bf8", - "flavor": { - "id": "60edb520-5826-4ae7-9e07-709b19ba6f39", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/60edb520-5826-4ae7-9e07-709b19ba6f39" - } - ] - }, - "links": [ - { - "rel": "self", - "href": "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/12f5b1d0-fe5c-469f-a7d4-b62a91134bf8" - }, - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/12f5b1d0-fe5c-469f-a7d4-b62a91134bf8" - } - ], - "OS-EXT-SRV-ATTR:host": "compute-0", - "OS-EXT-AZ:availability_zone": "nova", - "name": "test1", - "wrs-res:pci_devices": "", - "hostId": "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577", - "user_id": "777155411f3042c9b7e3194188d6f85d", - "status": "PAUSED", - "OS-EXT-STS:power_state": 3, - "OS-EXT-SRV-ATTR:hypervisor_hostname": "compute-0", - "tenant_id": "ad979139d5ea4a84b21b3620c0e4761e", - "OS-SRV-USG:launched_at": "2018-03-27T02:16:40.000000", - "OS-EXT-STS:vm_state": "paused", - "wrs-if:nics": [ - { - "nic1": { - "mac_address": "fa:16:3e:5f:1a:76", - "network": "mgmt", - "port_id": "6c225c23-abe3-42a8-8909-83471503d5d4", - "vif_model": "virtio", - "vif_pci_address": "", - "mtu": 9216 - } - }, - { - "nic2": { - "mac_address": "fa:16:3e:7c:7b:d7", - "network": "data0", - "port_id": "cbea2fec-c9b8-48ec-a964-0e3e255841bc", - "vif_model": "virtio", - "vif_pci_address": "", - "mtu": 9216 - } - } - ], - "wrs-sg:server_group": "", - "OS-EXT-STS:task_state": "", - "wrs-res:topology": "node:0, 1024MB, pgsize:2M, 1s,1c,2t, vcpus:0,1, pcpus:5,21, siblings:{0,1}, pol:ded, thr:pre\nnode:1, 1024MB, pgsize:2M, 1s,1c,2t, vcpus:2,3, pcpus:8,24, siblings:{2,3}, pol:ded, thr:pre", - "wrs-res:vcpus": [4, 4, 4], - "key_name": "", - "image": { - "id": "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb" - } - ] - }, - "created": "2018-03-27T02:16:32Z", - "addresses": { - "data0": [ - { - "OS-EXT-IPS:type": "fixed", - "version": 4, - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:7b:d7", - "addr": "192.168.2.8" - } - ], - "mgmt": [ - { - "OS-EXT-IPS:type": "fixed", - "version": 4, - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:5f:1a:76", - "addr": "192.168.1.6" - } - ] - }, - "os-extended-volumes:volumes_attached": [] - }, - { - "accessIPv4": "", - "OS-EXT-SRV-ATTR:instance_name": "instance-00000009", - "OS-SRV-USG:terminated_at": "", - "accessIPv6": "", - "config_drive": "", - "OS-DCF:diskConfig": "AUTO", - "updated": "2018-03-27T02:12:21Z", - "metadata": {}, - "id": "3f1b0375-a1db-4d94-b336-f32c82c0d7ec", - "flavor": { - "id": "0d3b1381-1626-4f6b-869b-4a4d5d42085e", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/0d3b1381-1626-4f6b-869b-4a4d5d42085e" - } - ] - }, - "links": [ - { - "rel": "self", - "href": "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/3f1b0375-a1db-4d94-b336-f32c82c0d7ec" - }, - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/3f1b0375-a1db-4d94-b336-f32c82c0d7ec" - } - ], - "OS-EXT-SRV-ATTR:host": "compute-0", - "OS-EXT-AZ:availability_zone": "nova", - "name": "test2", - "wrs-res:pci_devices": "", - "hostId": "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577", - "user_id": "777155411f3042c9b7e3194188d6f85d", - "status": "ACTIVE", - "OS-EXT-STS:power_state": 1, - "OS-EXT-SRV-ATTR:hypervisor_hostname": "compute-0", - "tenant_id": "ad979139d5ea4a84b21b3620c0e4761e", - "OS-SRV-USG:launched_at": "2018-03-27T02:12:21.000000", - "OS-EXT-STS:vm_state": "active", - "wrs-if:nics": [ - { - "nic1": { - "mac_address": "fa:16:3e:54:f8:a6", - "network": "mgmt", - "port_id": "30e2f51c-4473-4650-9ae9-a35e5d7ad452", - "vif_model": "avp", - "vif_pci_address": "", - "mtu": 9216 - } - } - ], - "wrs-sg:server_group": "", - "OS-EXT-STS:task_state": "", - "wrs-res:topology": "node:0, 4096MB, pgsize:2M, 1s,3c,1t, vcpus:0-2, pcpus:4,20,7, pol:ded, thr:pre", - "progress": 0, - "wrs-res:vcpus": [3, 3, 3], - "key_name": "", - "image": { - "id": "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb" - } - ] - }, - "created": "2018-03-27T02:10:26Z", - "addresses": { - "mgmt": [ - { - "OS-EXT-IPS:type": "fixed", - "version": 4, - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:54:f8:a6", - "addr": "192.168.1.11" - } - ] - }, - "os-extended-volumes:volumes_attached": [] - }, - { - "accessIPv4": "", - "OS-EXT-SRV-ATTR:instance_name": "instance-00000008", - "OS-SRV-USG:terminated_at": "", - "accessIPv6": "", - "config_drive": "", - "OS-DCF:diskConfig": "AUTO", - "updated": "2018-03-27T02:12:15Z", - "metadata": {}, - "id": "1b6f6671-b680-42cd-89e9-fc4ddd5d2e02", - "flavor": { - "id": "0d3b1381-1626-4f6b-869b-4a4d5d42085e", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/flavors/0d3b1381-1626-4f6b-869b-4a4d5d42085e" - } - ] - }, - "links": [ - { - "rel": "self", - "href": "http://192.168.100.100:8774/v2.1/ad979139d5ea4a84b21b3620c0e4761e/servers/1b6f6671-b680-42cd-89e9-fc4ddd5d2e02" - }, - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/servers/1b6f6671-b680-42cd-89e9-fc4ddd5d2e02" - } - ], - "OS-EXT-SRV-ATTR:host": "compute-0", - "OS-EXT-AZ:availability_zone": "nova", - "name": "test3", - "wrs-res:pci_devices": "", - "hostId": "b3479a460f5effda10c6fdb860e824be631026c1d09f551479180577", - "user_id": "777155411f3042c9b7e3194188d6f85d", - "status": "ACTIVE", - "OS-EXT-STS:power_state": 1, - "OS-EXT-SRV-ATTR:hypervisor_hostname": "compute-0", - "tenant_id": "ad979139d5ea4a84b21b3620c0e4761e", - "OS-SRV-USG:launched_at": "2018-03-27T02:12:15.000000", - "OS-EXT-STS:vm_state": "active", - "wrs-if:nics": [ - { - "nic1": { - "mac_address": "fa:16:3e:4e:9b:75", - "network": "mgmt", - "port_id": "72d13987-1d94-4a64-aa1a-973869ae1cad", - "vif_model": "avp", - "vif_pci_address": "", - "mtu": 9216 - } - } - ], - "wrs-sg:server_group": "", - "OS-EXT-STS:task_state": "", - "wrs-res:topology": "node:0, 4096MB, pgsize:2M, 1s,3c,1t, vcpus:0-2, pcpus:19,3,22, pol:ded, thr:pre", - "progress": 0, - "wrs-res:vcpus": [3, 3, 3], - "key_name": "", - "image": { - "id": "7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb", - "links": [ - { - "rel": "bookmark", - "href": "http://192.168.100.100:8774/ad979139d5ea4a84b21b3620c0e4761e/images/7ba636ef-5dfd-4e67-ad32-cd23ee74e1eb" - } - ] - }, - "created": "2018-03-27T02:10:01Z", - "addresses": { - "mgmt": [ - { - "OS-EXT-IPS:type": "fixed", - "version": 4, - "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:4e:9b:75", - "addr": "192.168.1.8" - } - ] - }, - "os-extended-volumes:volumes_attached": [] - } - ] -} - -SUCCESS_VMSTATE_RESPONSE = { - 'result': [ - { - 'name': 'test1', - 'power_state': 3, - 'id': '12f5b1d0-fe5c-469f-a7d4-b62a91134bf8', - 'state': 'paused', - 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e', - 'host': 'compute-0', - 'availability_zone': 'nova', - 'launched_at': '2018-03-27T02:16:40.000000' - }, - { - 'name': 'test2', - 'power_state': 1, - 'id': '3f1b0375-a1db-4d94-b336-f32c82c0d7ec', - 'state': 'active', - 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e', - 'host': 'compute-0', - 'availability_zone': 'nova', - 'launched_at': '2018-03-27T02:12:21.000000' - }, - { - 'name': 'test3', - 'power_state': 1, - 'id': '1b6f6671-b680-42cd-89e9-fc4ddd5d2e02', - 'state': 'active', - 'tenant_id': 'ad979139d5ea4a84b21b3620c0e4761e', - 'host': 'compute-0', - 'availability_zone': 'nova', - 'launched_at': '2018-03-27T02:12:15.000000' - } - ] -} - - -class TestEvents(test_base.TestRequest): - def setUp(self): - super(TestEvents, self).setUp() - - def _get_mock_response(self, return_value=None): - mock_response = mock.Mock(spec=test_base.MockResponse) - mock_response.status_code = status.HTTP_200_OK - mock_response.json.return_value = return_value - return mock_response - - @mock.patch.object(VimDriverUtils, 'get_session') - @mock.patch.object(VimDriverUtils, 'get_vim_info') - def test_events_check_success(self, mock_get_vim_info, mock_get_session): - mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO - mock_get_session.return_value = test_base.get_mock_session( - ["get"], { - "side_effect": [ - self._get_mock_response(MOCK_GET_SERVERS_DETAIL_RESPONSE), - ] - }) - - response = self.client.post( - "/api/multicloud-ocata/v0/windriver-hudson-dc_RegionOne/events_check", - HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) - - self.assertEquals(status.HTTP_200_OK, response.status_code) - self.assertEqual(SUCCESS_VMSTATE_RESPONSE, response.data) diff --git a/ocata/ocata/resource/tests/tests_infra_workload.py b/ocata/ocata/resource/tests/tests_infra_workload.py index c608865b..52a76e58 100644 --- a/ocata/ocata/resource/tests/tests_infra_workload.py +++ b/ocata/ocata/resource/tests/tests_infra_workload.py @@ -18,8 +18,8 @@ import unittest from rest_framework import status from common.msapi.helper import Helper as helper -from ocata.resource.views.infra_workload import InfraWorkload -from ocata.resource.views.infra_workload import APIv1InfraWorkload +from newton_base.resource.infra_workload import InfraWorkload +from newton_base.resource.infra_workload import APIv1InfraWorkload MOCK_TOKEN_RESPONSE = { "access": { diff --git a/ocata/ocata/resource/views/__init__.py b/ocata/ocata/resource/views/__init__.py deleted file mode 100644 index ae1ce9db..00000000 --- a/ocata/ocata/resource/views/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright (c) 2017-2018 Wind River Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/ocata/ocata/resource/views/capacity.py b/ocata/ocata/resource/views/capacity.py deleted file mode 100644 index e76c93a3..00000000 --- a/ocata/ocata/resource/views/capacity.py +++ /dev/null @@ -1,144 +0,0 @@ -# Copyright (c) 2018 Intel Corporation. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import logging -import traceback - -from common.exceptions import VimDriverNewtonException -from newton_base.util import VimDriverUtils - -from keystoneauth1.exceptions import HttpError -from rest_framework import status -from rest_framework.response import Response -from rest_framework.views import APIView -from common.msapi import extsys - - -logger = logging.getLogger(__name__) - - -class CapacityCheck(APIView): - - def __init__(self): - self._logger = logger - - def post(self, request, vimid=""): - self._logger.info("CapacityCheck--post::vimid, data> %s, %s" % (vimid, request.data)) - self._logger.debug("CapacityCheck--post::META> %s" % request.META) - - hasEnoughResource = False - try: - resource_demand = request.data - - tenant_name = None - vim = VimDriverUtils.get_vim_info(vimid) - sess = VimDriverUtils.get_session(vim, tenant_name) - - # get token: - cloud_owner, regionid = extsys.decode_vim_id(vimid) - interface = 'public' - service = {'service_type': 'compute', - 'interface': interface, - 'region_name': vim['openstack_region_id'] - if vim.get('openstack_region_id') - else vim['cloud_region_id']} - - # get limit for this tenant - req_resouce = "/limits" - resp = sess.get(req_resouce, endpoint_filter=service) - content = resp.json() - compute_limits = content['limits']['absolute'] - - # get total resource of this cloud region - try: - req_resouce = "/os-hypervisors/statistics" - self._logger.info("check os-hypervisors statistics> URI:%s" % req_resouce) - resp = sess.get(req_resouce, endpoint_filter=service) - self._logger.info("check os-hypervisors statistics> status:%s" % resp.status_code) - content = resp.json() - hypervisor_statistics = content['hypervisor_statistics'] - self._logger.debug("check os-hypervisors statistics> resp data:%s" % content) - except HttpError as e: - if e.http_status == status.HTTP_403_FORBIDDEN: - # Due to non administrator account cannot get hypervisor data, - # so construct enough resource data - conVCPUS = int(resource_demand['vCPU']) - conFreeRamMB = int(resource_demand['Memory']) - conFreeDiskGB = int(resource_demand['Storage']) - self._logger.info("Non administator forbidden to access hypervisor statistics data") - hypervisor_statistics = {'vcpus_used': 0, 'vcpus': conVCPUS, 'free_ram_mb': conFreeRamMB, 'free_disk_gb': conFreeDiskGB} - else: - # non forbiden exeption will be redirected - raise e - - # get storage limit for this tenant - service['service_type'] = 'volumev2' - req_resouce = "/limits" - resp = sess.get(req_resouce, endpoint_filter=service) - content = resp.json() - storage_limits = content['limits']['absolute'] - - # compute actual available resource for this tenant - remainVCPU = compute_limits['maxTotalCores'] - compute_limits['totalCoresUsed'] - remainHypervisorVCPU = hypervisor_statistics['vcpus'] - hypervisor_statistics['vcpus_used'] - - if (remainVCPU > remainHypervisorVCPU): - remainVCPU = remainHypervisorVCPU - - remainMEM = compute_limits['maxTotalRAMSize'] - compute_limits['totalRAMUsed'] - remainHypervisorMEM = hypervisor_statistics['free_ram_mb'] - if remainMEM > remainHypervisorMEM: - remainMEM = remainHypervisorMEM - - remainStorage = storage_limits['maxTotalVolumeGigabytes'] - storage_limits['totalGigabytesUsed'] - remainHypervisorStorage = hypervisor_statistics['free_disk_gb'] - if (remainStorage > remainHypervisorStorage): - remainStorage = remainHypervisorStorage - - # compare resource demanded with available - if (int(resource_demand['vCPU']) > remainVCPU): - hasEnoughResource = False - elif (int(resource_demand['Memory']) > remainMEM): - hasEnoughResource = False - elif (int(resource_demand['Storage']) > remainStorage): - hasEnoughResource = False - else: - hasEnoughResource = True - - return Response(data={'result': hasEnoughResource}, status=status.HTTP_200_OK) - except VimDriverNewtonException as e: - return Response(data={'result': hasEnoughResource, 'error': e.content}, status=e.status_code) - except HttpError as e: - self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) - resp = e.response.json() - resp.update({'result': hasEnoughResource}) - return Response(data=e.response.json(), status=e.http_status) - except Exception as e: - self._logger.error(traceback.format_exc()) - return Response(data={'result': hasEnoughResource, 'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - -class APIv1CapacityCheck(CapacityCheck): - - def __init__(self): - super(APIv1CapacityCheck, self).__init__() - # self._logger = logger - - def post(self, request, cloud_owner="", cloud_region_id=""): - self._logger.info("vimid, data> %s,%s, %s" % (cloud_owner, cloud_region_id, request.data)) - self._logger.debug("META> %s" % request.META) - - vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id) - return super(APIv1CapacityCheck, self).post(request, vimid) diff --git a/ocata/ocata/resource/views/events.py b/ocata/ocata/resource/views/events.py deleted file mode 100644 index 6b48f2d5..00000000 --- a/ocata/ocata/resource/views/events.py +++ /dev/null @@ -1,99 +0,0 @@ -# Copyright (c) 2017-2018 Wind River Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import logging -import traceback - -from common.exceptions import VimDriverNewtonException -from newton_base.util import VimDriverUtils - -from keystoneauth1.exceptions import HttpError -from rest_framework import status -from rest_framework.response import Response -from rest_framework.views import APIView -from common.msapi import extsys - - -logger = logging.getLogger(__name__) - - -class EventsCheck(APIView): - - def __init__(self): - self._logger = logger - - def post(self, request, vimid=""): - self._logger.info("vimid, data> %s, %s" % (vimid, request.data)) - self._logger.debug("META> %s" % request.META) - - try: - tenant_name = None - vim = VimDriverUtils.get_vim_info(vimid) - sess = VimDriverUtils.get_session(vim, tenant_name) - - # get token: - cloud_owner, regionid = extsys.decode_vim_id(vimid) - interface = 'public' - service = { - 'service_type': 'compute', - 'interface': interface, - 'region_name': vim['openstack_region_id'] - if vim.get('openstack_region_id') - else vim['cloud_region_id'] - } - - # get servers detail info - req_resouce = "/servers/detail" - self._logger.info("check servers detail> URI:%s" % req_resouce) - resp = sess.get(req_resouce, endpoint_filter=service) - self._logger.info("check servers detail> status:%s" % resp.status_code) - content = resp.json() - self._logger.debug("check servers detail> resp data:%s" % content) - - # extract server status info - if len(content['servers']): - servers = content['servers'] - resp_vmstate = [] - for num in range(0, len(servers)): - vmstate = { - 'name': servers[num]['name'], - 'state': servers[num]['OS-EXT-STS:vm_state'], - 'power_state': servers[num]['OS-EXT-STS:power_state'], - 'launched_at': servers[num]['OS-SRV-USG:launched_at'], - 'id': servers[num]['id'], - 'host': servers[num]['OS-EXT-SRV-ATTR:host'], - 'availability_zone': servers[num]['OS-EXT-AZ:availability_zone'], - 'tenant_id': servers[num]['tenant_id'] - } - - resp_vmstate.append(vmstate) - - self._logger.info("RESP with data> result:%s" % resp_vmstate) - return Response(data={'result': resp_vmstate}, status=status.HTTP_200_OK) - - except VimDriverNewtonException as e: - self._logger.error("Plugin exception> status:%s,error:%s" % - (e.status_code, e.content)) - return Response(data={'result': resp_vmstate, 'error': e.content}, status=e.status_code) - - except HttpError as e: - self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) - resp = e.response.json() - resp.update({'result': resp_vmstate}) - return Response(data=e.response.json(), status=e.http_status) - - except Exception as e: - self._logger.error(traceback.format_exc()) - return Response(data={'result': resp_vmstate, 'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/ocata/ocata/resource/views/infra_workload.py b/ocata/ocata/resource/views/infra_workload.py deleted file mode 100644 index ab464da8..00000000 --- a/ocata/ocata/resource/views/infra_workload.py +++ /dev/null @@ -1,420 +0,0 @@ -# Copyright (c) 2018 Intel Corporation. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import json -import logging -import traceback - -from django.conf import settings -from keystoneauth1.exceptions import HttpError -from rest_framework import status -from rest_framework.response import Response -from rest_framework.views import APIView - -from common.msapi import extsys -from common.msapi.helper import Helper as helper -from common.utils import restcall -from common.exceptions import VimDriverNewtonException - -logger = logging.getLogger(__name__) - - -class InfraWorkload(APIView): - - def __init__(self): - self._logger = logger - - def post(self, request, vimid="", requri=""): - self._logger.info("vimid, requri: %s, %s" % (vimid, requri)) - self._logger.info("data: %s" % request.data) - self._logger.debug("META: %s" % request.META) - - try: - data = request.data - oof_directive = data.get("oof_directives", {}) - template_type = data.get("template_type", None) - template_data = data.get("template_data", {}) - - resp_template = None - if template_type and "heat" == template_type.lower(): - # update heat parameters from oof_directive - parameters = template_data.get("parameters", {}) - - for directive in oof_directive.get("directives", []): - if directive["type"] == "vnfc": - for directive2 in directive.get("directives", []): - if directive2["type"] in ["flavor_directives", "sriovNICNetwork_directives"]: - for attr in directive2.get("attributes", []): - label_name = attr["attribute_name"] - label_value = attr["attribute_value"] - if label_name in parameters: - template_data["parameters"][label_name] = label_value - else: - self._logger.warn("There is no parameter exist: %s" % label_name) - - # update parameters - template_data["parameters"] = parameters - - # reset to make sure "files" are empty - template_data["file"] = {} - - # authenticate - cloud_owner, regionid = extsys.decode_vim_id(vimid) - # should go via multicloud proxy so that the selflink is updated by multicloud - retcode, v2_token_resp_json, os_status = helper.MultiCloudIdentityHelper( - settings.MULTICLOUD_API_V1_PREFIX, - cloud_owner, regionid, "/v2.0/tokens") - if retcode > 0 or not v2_token_resp_json: - logger.error("authenticate fails:%s,%s, %s" % - (cloud_owner, regionid, v2_token_resp_json)) - return - - service_type = "orchestration" - resource_uri = "/stacks" - self._logger.info("retrieve stack resources, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, - regionid, - v2_token_resp_json, - service_type, - resource_uri, - template_data, - "POST") - stack1 = content.get('stack', None) if retcode == 0 and content else None - resp_template = { - "template_type": template_type, - "workload_id": stack1["id"] if stack1 else "", - "template_response": content - } - self._logger.info("RESP with data> result:%s" % resp_template) - - return Response(data=resp_template, status=os_status) - - else: - msg = "The template type %s is not supported" % (template_type) - self._logger.warn(msg) - return Response(data={"error": msg}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - except VimDriverNewtonException as e: - self._logger.error("Plugin exception> status:%s,error:%s" - % (e.status_code, e.content)) - return Response(data={'error': e.content}, status=e.status_code) - except HttpError as e: - self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) - return Response(data=e.response.json(), status=e.http_status) - except Exception as e: - self._logger.error(traceback.format_exc()) - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def get(self, request, vimid="", requri=""): - self._logger.info("vimid, requri: %s, %s" % (vimid, requri)) - self._logger.debug("META: %s" % request.META) - - try: - # assume the workload_type is heat - template_type = "heat" - stack_id = requri - cloud_owner, regionid = extsys.decode_vim_id(vimid) - # should go via multicloud proxy so that the selflink is updated by multicloud - retcode, v2_token_resp_json, os_status = helper.MultiCloudIdentityHelper( - settings.MULTICLOUD_API_V1_PREFIX, - cloud_owner, regionid, "/v2.0/tokens") - if retcode > 0 or not v2_token_resp_json: - logger.error("authenticate fails:%s, %s, %s" % (cloud_owner, regionid, v2_token_resp_json)) - return - - # get stack status - service_type = "orchestration" - resource_uri = "/stacks?id=%s" % stack_id if stack_id else "/stacks" - self._logger.info("retrieve stack resources, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, - service_type, resource_uri, None, "GET") - stacks = content.get('stacks', []) if retcode == 0 and content else [] - stack_status = stacks[0]["stack_status"] if len(stacks) > 0 else "" - - resp_template = { - "template_type": template_type, - "workload_id": stack_id, - "workload_status": stack_status - } - - if retcode > 0: - resp_template['workload_response'] = content - - if ('CREATE_COMPLETE' == stack_status): - self.heatbridge_update(request, vimid, stack_id) - - self._logger.info("RESP with data> result:%s" % resp_template) - return Response(data=resp_template, status=status.HTTP_200_OK) - except VimDriverNewtonException as e: - self._logger.error("Plugin exception> status:%s,error:%s" - % (e.status_code, e.content)) - return Response(data={'error': e.content}, status=e.status_code) - except HttpError as e: - self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) - return Response(data=e.response.json(), status=e.http_status) - except Exception as e: - self._logger.error(traceback.format_exc()) - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def heatbridge_update(self, request, vimid, stack_id): - ''' - update heat resource to AAI for the specified cloud region and tenant - The resources includes: vserver, vserver/l-interface, - :param request: - :param vimid: - :param stack_id: - :return: - ''' - - cloud_owner, regionid = extsys.decode_vim_id(vimid) - # should go via multicloud proxy so that the selflink is updated by multicloud - retcode, v2_token_resp_json, os_status = helper.MultiCloudIdentityHelper(settings.MULTICLOUD_API_V1_PREFIX, - cloud_owner, regionid, "/v2.0/tokens") - if retcode > 0: - logger.error("authenticate fails:%s, %s, %s" % (cloud_owner, regionid, v2_token_resp_json)) - return None - tenant_id = v2_token_resp_json["access"]["token"]["tenant"]["id"] - # tenant_name = v2_token_resp_json["access"]["token"]["tenant"]["name"] - - # common prefix - aai_cloud_region = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s" \ - % (cloud_owner, regionid, tenant_id) - - # get stack resource - service_type = "orchestration" - resource_uri = "/stacks/%s/resources" % (stack_id) - self._logger.info("retrieve stack resources, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, service_type, resource_uri, None, "GET") - resources = content.get('resources', []) if retcode == 0 and content else [] - - # find and update resources - transactions = [] - for resource in resources: - if resource.get('resource_status', None) != "CREATE_COMPLETE": - continue - if resource.get('resource_type', None) == 'OS::Nova::Server': - # retrieve vserver details - service_type = "compute" - resource_uri = "/servers/%s" % (resource['physical_resource_id']) - self._logger.info("retrieve vserver detail, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, service_type, - resource_uri, None, "GET") - self._logger.debug(" resp data:%s" % content) - vserver_detail = content.get('server', None) if retcode == 0 and content else None - if vserver_detail: - # compose inventory entry for vserver - vserver_link = "" - for link in vserver_detail['links']: - if link['rel'] == 'self': - vserver_link = link['href'] - break - pass - - # note: relationship-list to flavor/image is not be update yet - # note: volumes is not updated yet - # note: relationship-list to vnf will be handled somewhere else - aai_resource = { - 'body': { - 'vserver-name': vserver_detail['name'], - 'vserver-name2': vserver_detail['name'], - "vserver-id": vserver_detail['id'], - "vserver-selflink": vserver_link, - "prov-status": vserver_detail['status'] - }, - "uri": aai_cloud_region + "/vservers/vserver/%s" - % (vserver_detail['id']) - } - - try: - # then update the resource - retcode, content, status_code = \ - restcall.req_to_aai(aai_resource['uri'], "PUT", content=aai_resource['body']) - - if retcode == 0 and content: - content = json.JSONDecoder().decode(content) - self._logger.debug("AAI update %s response: %s" % (aai_resource['uri'], content)) - except Exception as e: - self._logger.error(traceback.format_exc(e)) - pass - - aai_resource_transactions = {"put": [aai_resource]} - transactions.append(aai_resource_transactions) - # self._logger.debug("aai_resource :%s" % aai_resource_transactions) - pass - - for resource in resources: - if resource.get('resource_status', None) != "CREATE_COMPLETE": - continue - if resource.get('resource_type', None) == 'OS::Neutron::Port': - # retrieve vserver details - service_type = "network" - resource_uri = "/v2.0/ports/%s" % (resource['physical_resource_id']) - self._logger.info("retrieve vserver detail, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, - regionid, - v2_token_resp_json, - service_type, - resource_uri, - None, - "GET") - self._logger.debug(" resp data:%s" % content) - - vport_detail = content.get('port', None) if retcode == 0 and content else None - if vport_detail: - # compose inventory entry for vport - # note: l3-interface-ipv4-address-list, l3-interface-ipv6-address-list are not updated yet - # note: network-name is not update yet since the detail coming with network-id - aai_resource = { - "body": { - "interface-name": vport_detail['name'], - "interface-id": vport_detail['id'], - "macaddr": vport_detail['mac_address'] - }, - 'uri': aai_cloud_region + "/vservers/vserver/%s/l-interfaces/l-interface/%s" - % (vport_detail['device_id'], vport_detail['name']) - } - try: - # then update the resource - retcode, content, status_code = \ - restcall.req_to_aai(aai_resource['uri'], "PUT", content=aai_resource['body']) - - if retcode == 0 and content: - content = json.JSONDecoder().decode(content) - self._logger.debug("AAI update %s response: %s" % (aai_resource['uri'], content)) - except Exception as e: - self._logger.error(traceback.format_exc(e)) - pass - - aai_resource_transactions = {"put": [aai_resource]} - transactions.append(aai_resource_transactions) - # self._logger.debug("aai_resource :%s" % aai_resource_transactions) - - pass - - aai_transactions = {"transactions": transactions} - self._logger.debug("aai_transactions :%s" % aai_transactions) - - return aai_transactions - - def delete(self, request, vimid="", requri=""): - self._logger.info("vimid, requri: %s, %s" % (vimid, requri)) - self._logger.debug("META: %s" % request.META) - - try: - if requri == "": - raise VimDriverNewtonException( - message="workload_id is not specified", - content="workload_id must be specified to delete the workload", - status_code=400) - - # assume the workload_type is heat - template_type = "heat" - stack_id = requri - cloud_owner, regionid = extsys.decode_vim_id(vimid) - # should go via multicloud proxy so that the selflink is updated by multicloud - retcode, v2_token_resp_json, os_status = helper.MultiCloudIdentityHelper( - settings.MULTICLOUD_API_V1_PREFIX, - cloud_owner, regionid, "/v2.0/tokens") - if retcode > 0 or not v2_token_resp_json: - logger.error("authenticate fails:%s, %s, %s" % (cloud_owner, regionid, v2_token_resp_json)) - return - # tenant_id = v2_token_resp_json["access"]["token"]["tenant"]["id"] - # tenant_name = v2_token_resp_json["access"]["token"]["tenant"]["name"] - - # get stack status - service_type = "orchestration" - resource_uri = "/stacks?id=%s" % stack_id if stack_id else "/stacks" - self._logger.info("retrieve stack resources, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, - service_type, resource_uri, None, "GET") - stacks = content.get('stacks', []) if retcode == 0 and content else [] - # assume there is at most 1 stack returned since it was filtered by id - stack1 = stacks[0] if stacks else None - stack_status = "" - - if stack1 and 'CREATE_COMPLETE' == stack1['stack_status']: - # delete the stack - resource_uri = "/stacks/%s/%s" % (stack1['stack_name'], stack1['id']) - self._logger.info("delete stack, URI:%s" % resource_uri) - retcode, content, os_status = helper.MultiCloudServiceHelper(cloud_owner, regionid, v2_token_resp_json, - service_type, resource_uri, None, "DELETE") - if retcode == 0: - stack_status = "DELETE_IN_PROCESS" - # and update AAI inventory by heatbridge-delete - self.heatbridge_delete(request, vimid, stack1['id']) - - resp_template = { - "template_type": template_type, - "workload_id": stack_id, - "workload_status": stack_status - } - - if retcode > 0: - resp_template["workload_response"] = content - - self._logger.info("RESP with data> result:%s" % resp_template) - return Response(status=os_status) - except VimDriverNewtonException as e: - self._logger.error("Plugin exception> status:%s,error:%s" - % (e.status_code, e.content)) - return Response(data={'error': e.content}, status=e.status_code) - except HttpError as e: - self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) - return Response(data=e.response.json(), status=e.http_status) - except Exception as e: - self._logger.error(traceback.format_exc()) - return Response(data={'error': str(e)}, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def heatbridge_delete(self, request, stack_id, vimid): - ''' - remove heat resource from AAI for the specified cloud region and tenant - The resources includes: vserver, vserver/l-interface, - :param request: - :param stack_id: - :param vimid: - :param tenant_id: - :return: - ''' - pass - - -class APIv1InfraWorkload(InfraWorkload): - - def __init__(self): - super(APIv1InfraWorkload, self).__init__() - # self._logger = logger - - def post(self, request, cloud_owner="", cloud_region_id="", requri=""): - # self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data)) - # self._logger.debug("META: %s" % request.META) - - vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id) - return super(APIv1InfraWorkload, self).post(request, vimid, requri) - - def get(self, request, cloud_owner="", cloud_region_id="", requri=""): - # self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data)) - # self._logger.debug("META: %s" % request.META) - - vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id) - return super(APIv1InfraWorkload, self).get(request, vimid, requri) - - def delete(self, request, cloud_owner="", cloud_region_id="", requri=""): - # self._logger.info("cloud owner, cloud region id, data: %s,%s, %s" % (cloud_owner, cloud_region_id, request.data)) - # self._logger.debug("META: %s" % request.META) - - vimid = extsys.encode_vim_id(cloud_owner, cloud_region_id) - return super(APIv1InfraWorkload, self).delete(request, vimid, requri) diff --git a/ocata/ocata/urls.py b/ocata/ocata/urls.py index 3e825c84..2f3ae73d 100644 --- a/ocata/ocata/urls.py +++ b/ocata/ocata/urls.py @@ -16,9 +16,8 @@ from django.conf.urls import include, url from ocata.registration.views import registration from newton_base.openoapi import tenants -from ocata.resource.views import capacity -from ocata.resource.views import events -from ocata.resource.views import infra_workload +from newton_base.resource import capacity +from newton_base.resource import infra_workload urlpatterns = [ url(r'^', include('ocata.swagger.urls')), @@ -42,9 +41,6 @@ urlpatterns = [ # CapacityCheck url(r'^api/multicloud-ocata/v0/(?P[0-9a-zA-Z_-]+)/capacity_check/?$', capacity.CapacityCheck.as_view()), - # events - url(r'^api/multicloud-ocata/v0/(?P[0-9a-zA-Z_-]+)/events_check/?$', - events.EventsCheck.as_view()), # API upgrading url(r'^api/multicloud-ocata/v1/(?P[0-9a-zA-Z_-]+)/(?P[0-9a-zA-Z_-]+)/registry$', -- cgit 1.2.3-korg