diff options
author | Victor Morales <victor.morales@intel.com> | 2017-09-28 10:50:03 -0700 |
---|---|---|
committer | Victor Morales <victor.morales@intel.com> | 2017-09-28 10:50:03 -0700 |
commit | 5952573e01f2681c45402bde24d3e1ab67d944b5 (patch) | |
tree | 5de8ad29df8077d7535031d696a8fd40dd95c931 | |
parent | 0aa903c0dc422a8660cfe64616a4d191318816e6 (diff) |
Create UTs for Registry APIView
This change creates unit tests for the Registry APIView class
and refactors its code to simplify it and reuse some functionality
Change-Id: I33a01e750e80d4acbae12bbd873484bbb423f1cf
Signed-off-by: Victor Morales <victor.morales@intel.com>
Issue-Id: MULTICLOUD-83
-rw-r--r-- | newton/newton/pub/tests/test_extsys.py | 11 | ||||
-rw-r--r-- | newton/newton/registration/tests/__init__.py | 0 | ||||
-rw-r--r-- | newton/newton/registration/tests/test_registration.py | 194 | ||||
-rw-r--r-- | newton/newton/registration/views/registration.py | 543 | ||||
-rw-r--r-- | newton/newton/requests/tests/test_base.py | 31 | ||||
-rw-r--r-- | newton/newton/requests/tests/test_server.py | 1 |
6 files changed, 388 insertions, 392 deletions
diff --git a/newton/newton/pub/tests/test_extsys.py b/newton/newton/pub/tests/test_extsys.py index 5ddbbcba..239ebe00 100644 --- a/newton/newton/pub/tests/test_extsys.py +++ b/newton/newton/pub/tests/test_extsys.py @@ -15,6 +15,7 @@ import json import mock +from rest_framework import status import six import unittest @@ -68,9 +69,13 @@ class TestEpaCaps(unittest.TestCase): def test_get_vim_by_id(self): values = [ - (1, "test_content", 500), # Failure first call - (0, json.dumps(MOCK_VIM_INFO), None), (1, "test_content", 500), # Failure second call - (0, json.dumps(MOCK_VIM_INFO), None), (0, json.dumps(MOCK_ESR_SYSTEM_INFO), None) # Success calls + (1, "test_content", + status.HTTP_500_INTERNAL_SERVER_ERROR), # Failure first call + (0, json.dumps(MOCK_VIM_INFO), None), + (1, "test_content", + status.HTTP_500_INTERNAL_SERVER_ERROR), # Failure second call + (0, json.dumps(MOCK_VIM_INFO), None), + (0, json.dumps(MOCK_ESR_SYSTEM_INFO), None) # Success calls ] restcall.req_to_aai = mock.Mock(side_effect=returnList(values)) diff --git a/newton/newton/registration/tests/__init__.py b/newton/newton/registration/tests/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/newton/newton/registration/tests/__init__.py diff --git a/newton/newton/registration/tests/test_registration.py b/newton/newton/registration/tests/test_registration.py new file mode 100644 index 00000000..d765eeb2 --- /dev/null +++ b/newton/newton/registration/tests/test_registration.py @@ -0,0 +1,194 @@ +# Copyright (c) 2017 Intel Corporation, 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.pub.utils import restcall +from newton.requests.tests import mock_info +from newton.requests.tests import test_base +from newton.requests.views.util import VimDriverUtils + +MOCK_GET_TENANT_RESPONSE = { + "projects":[ + {"id": "1", "name": "project"}, + {"id": "2", "name": "project2"}, + ] +} + +MOCK_GET_FLAVOR_RESPONSE = { + "flavors": [ + { + "id": "1", "name": "micro", "vcpus": 1, "ram": "1MB", + "disk": "1G", "OS-FLV-EXT-DATA:ephemeral": False, + "swap": True, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, "link": [{"href":1}] + }, + { + "id": "2", "name": "mini", "vcpus": 2, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True + }, + ] +} + +MOCK_GET_IMAGE_RESPONSE = { + "images": [ + { + "id": "1", "name": "cirros", "self": "test", + "os_distro": "CirrOS", "os_version": "0.3", + "application": "test", "application_vendor": "ONAP", + "application_version": 1, "architecture": "x86", + "schema": None + }, + { + "id": "2", "name": "cirros", "self": "test", + "os_distro": "CirrOS", "os_version": "0.3", + "application": "test", "application_vendor": "ONAP", + "application_version": 1, "architecture": "x86", + "schema": "req_resource" + }, + ] +} + +MOCK_GET_AZ_RESPONSE = { + "availabilityZoneInfo": [ + { + "zoneName": "production", + "zoneState": {"available": True}, + "hosts": { "hypervisor": "kvm" } + }, + { + "zoneName": "testing", + }, + ] +} + +MOCK_HYPERVISOR_RESPONSE = { + "hypervisors": [ + {"hypervisor_type": "kvm"} + ] +} + +MOCK_GET_SNAPSHOT_RESPONSE = { + "snapshots": [ + { + "id": 1, "name": "test", "metadata": + { + "architecture": "x86", "os-distro": "clearlinux", + "os-version": "276", "vendor": "intel", "version": 3, + "selflink": "test", "prev-snapshot-id": "test-id" + } + }, + {"id": 2, "name": "test2"} + ] +} + +MOCK_GET_HYPERVISOR_RESPONSE = { + "hypervisors": [ + { + "hypervisor_hostname": "testing", "state": "ACTIVE", + "id": 1, "local_gb": 256, "memory_mb": 1024, + "hypervisor_links": "link", "host_ip": "127.0.0.1", + "cpu_info": { + "topology": { + "cores": 8, "threads": 16, "sockets": 4 + } + } + }, + { + "hypervisor_hostname": "testing2", "state": "XXX", + "id": 1, "local_gb": 256, "memory_mb": 1024, + "hypervisor_links": "link", "host_ip": "127.0.0.1", + } + ] +} + +TEST_REGISTER_ENDPOINT_REQUEST = { + "defaultTenant": "project1" +} + +class TestFlavors(test_base.TestRequest): + + def setUp(self): + super(TestFlavors, self).setUp() + self.req_to_aai_backup = restcall.req_to_aai + + def tearDown(self): + super(TestFlavors, self).tearDown() + restcall.req_to_aai = self.req_to_aai_backup + + 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_register_endpoint_successfully( + self, mock_get_vim_info, mock_get_session): + restcall.req_to_aai = mock.Mock() + restcall.req_to_aai.return_value = (0, {}, status.HTTP_200_OK) + 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_TENANT_RESPONSE), + self._get_mock_response(MOCK_GET_FLAVOR_RESPONSE), + self._get_mock_response(MOCK_GET_IMAGE_RESPONSE), + self._get_mock_response(), + self._get_mock_response(MOCK_GET_AZ_RESPONSE), + self._get_mock_response(MOCK_HYPERVISOR_RESPONSE), + self._get_mock_response(MOCK_GET_SNAPSHOT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPERVISOR_RESPONSE) + ] + }) + + response = self.client.post(( + "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/" + "registry"), TEST_REGISTER_ENDPOINT_REQUEST, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_202_ACCEPTED, + response.status_code) + + @mock.patch.object(VimDriverUtils, 'delete_vim_info') + def test_unregister_endpoint_successfully( + self, mock_delete_vim_info): + mock_delete_vim_info.return_value = 0 + + response = self.client.delete(( + "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/" + "registry"), "{}", content_type="application/json", + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_202_ACCEPTED, + response.status_code) + + + @mock.patch.object(VimDriverUtils, 'delete_vim_info') + def test_fail_unregister_endpoint( + self, mock_delete_vim_info): + mock_delete_vim_info.return_value = 1 + + response = self.client.delete(( + "/api/multicloud-newton/v0/windriver-hudson-dc_RegionOne/" + "registry"), "{}", content_type="application/json", + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, + response.status_code) diff --git a/newton/newton/registration/views/registration.py b/newton/newton/registration/views/registration.py index 25231d7c..95769a66 100644 --- a/newton/newton/registration/views/registration.py +++ b/newton/newton/registration/views/registration.py @@ -11,6 +11,7 @@ # 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 json import traceback @@ -22,13 +23,12 @@ from rest_framework.views import APIView from newton.pub.config import config from newton.pub.exceptions import VimDriverNewtonException -from newton.requests.views.util import VimDriverUtils -from newton.pub.utils.restcall import req_to_aai from newton.pub.msapi import extsys +from newton.pub.utils import restcall +from newton.requests.views.util import VimDriverUtils logger = logging.getLogger(__name__) -DEBUG=True class Registry(APIView): @@ -36,100 +36,73 @@ class Registry(APIView): self.proxy_prefix = config.MULTICLOUD_PREFIX self._logger = logger - def update_tenant(self, cloud_owner, cloud_region_id, tenantinfo): - ''' - populate tenant into AAI - :param cloud_owner: - :param cloud_region_id: - :param tenantinfo: - tenant-id: string - tenant-name: string - :return: - ''' - - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s" - % (cloud_owner, cloud_region_id, tenantinfo['tenant-id']), "PUT", content=tenantinfo) - - self._logger.debug("update_tenant,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, tenantinfo['tenant-id'], retcode, content, status_code)) - return retcode - return 1 - - def discover_tenants(self, request, vimid="", session=None, viminfo=None): - req_resource = "/projects" - service = {'service_type': "identity", + def _get_list_resources( + self, resource_url, service_type, session, viminfo, + vimid, content_key): + service = {'service_type': service_type, 'interface': 'public', 'region_id': viminfo['cloud_region_id']} - - resp = session.get(req_resource, endpoint_filter=service) + resp = session.get(resource_url, endpoint_filter=service) content = resp.json() + self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) + % (vimid, resource_url, resp.status_code,content)) if resp.status_code != status.HTTP_200_OK: - return False # failed to discover resources + return # failed to discover resources + return content.get(content_key) + + def _update_resoure(self, cloud_owner, cloud_region_id, + resoure_id, resource_info, resource_type): + if cloud_owner and cloud_region_id: + retcode, content, status_code = \ + restcall.req_to_aai( + ("/cloud-infrastructure/cloud-regions/" + "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/" + "%(resource_type)s/%(resource_type)ss/%(resoure_id)s" + % { + "cloud_owner": cloud_owner, + "cloud_region_id": cloud_region_id, + "resoure_id": resoure_id, + "resource_info": resource_info, + "resource_type": resource_type, + }) + , "PUT", content=resource_info) + + self._logger.debug( + ("update_tenant,vimid:%(cloud_owner)s" + "_%(cloud_region_id)s req_to_aai: %(resoure_id)s, " + "return %(retcode)s, %(content)s, %(status_code)s") + % { + "cloud_owner": cloud_owner, + "cloud_region_id": cloud_region_id, + "resoure_id": resoure_id, + "retcode": retcode, + "content": content, + "status_code": status_code, + }) + return retcode + return 1 # unknown cloud owner,region_id + def _discover_tenants(self, vimid="", session=None, viminfo=None): # iterate all projects and populate them into AAI cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for tenant in content.get('projects'): + for tenant in self._get_list_resources( + "projects", "identity", session, viminfo, vimid, + "projects"): tenant_info = { 'tenant-id': tenant['id'], 'tenant-name': tenant['name'], } - self.update_tenant(cloud_owner, cloud_region_id, tenant_info) - pass - - - def update_flavor(self, cloud_owner, cloud_region_id, flavorinfo): - ''' - populate flavor into AAI - :param cloud_owner: - :param cloud_region_id: - :param flavorinfo: - flavor-id: string - flavor-name: string - flavor-vcpus: integer - flavor-ram: integer - flavor-disk: integer - flavor-ephemeral: integer - flavor-swap: string - flavor-is-public: boolean - flavor-selflink: string - flavor-disabled: boolean - - :return: - ''' - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/flavors/flavor/%s" - % (cloud_owner, cloud_region_id, flavorinfo['flavor-id']), "PUT", content=flavorinfo) - - self._logger.debug("update_flavor,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, flavorinfo['flavor-id'], retcode, content, status_code)) - return retcode - return 1 - - def discover_flavors(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/flavors/detail" - service = {'service_type': "compute", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) - - if resp.status_code != status.HTTP_200_OK: - return False # failed to discover resources + self._update_resoure( + cloud_owner, cloud_region_id, tenant['id'], + tenant_info, "tenant") + def _discover_flavors(self, vimid="", session=None, viminfo=None): cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for flavor in content.get('flavors'): + for flavor in self._get_list_resources( + "/flavors/detail", "compute", session, viminfo, vimid, + "flavors"): flavor_info = { 'flavor-id': flavor['id'], 'flavor-name': flavor['name'], @@ -143,87 +116,44 @@ class Registry(APIView): } if flavor.get('link') and len(flavor['link']) > 0: - flavor_info['flavor-selflink'] =flavor['links'][0]['href'], - - self.update_flavor(cloud_owner, cloud_region_id, flavor_info) - - pass - - def update_image_metadata(self, cloud_owner, cloud_region_id, image_id, metadatainfo): - ''' - populate image meta data - :param cloud_owner: - :param cloud_region_id: - :param image_id: - :param metadatainfo: - metaname: string - metaval: string - :return: - ''' - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai( - "/cloud-infrastructure/cloud-regions/cloud-region" - + "/%s/%s/images/image/%s/metadata/metadatum/%s" - % (cloud_owner, cloud_region_id, image_id, metadatainfo['metaname']), - "PUT", content=metadatainfo) - - self._logger.debug("update_image,vimid:%s_%s req_to_aai: %s/%s, return %s, %s, %s" - % (cloud_owner,cloud_region_id,image_id,metadatainfo['metaname'], - retcode, content, status_code)) - return retcode - return 1 - - def update_image(self, cloud_owner, cloud_region_id, imageinfo): - ''' - populate image into AAI - :param cloud_owner: - :param cloud_region_id: - :param imageinfo: - image-id: string - image-name: string - image-architecture: string - image-os-distro: string - image-os-version: string - application: string - application-vendor: string - application-version: string - image-selflink: string - - :return: - ''' - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/images/image/%s" - % (cloud_owner, cloud_region_id, imageinfo['image-id']), - "PUT", content=imageinfo) - - self._logger.debug("update_image,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, imageinfo['image-id'], - retcode, content, status_code)) - - return retcode - return 1 # unknown cloud owner,region_id - - def discover_images(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/v2/images" - service = {'service_type': "image", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) - - if resp.status_code != status.HTTP_200_OK: - return False # failed to discover resources + flavor_info['flavor-selflink'] = flavor['link'][0]['href'], + + self._update_resoure( + cloud_owner, cloud_region_id, flavor['id'], + flavor_info, "flavor") + + # def update_image_metadata(self, cloud_owner, cloud_region_id, image_id, metadatainfo): + # ''' + # populate image meta data + # :param cloud_owner: + # :param cloud_region_id: + # :param image_id: + # :param metadatainfo: + # metaname: string + # metaval: string + # :return: + # ''' + # + # if cloud_owner and cloud_region_id: + # retcode, content, status_code = \ + # restcall.req_to_aai( + # "/cloud-infrastructure/cloud-regions/cloud-region" + # + "/%s/%s/images/image/%s/metadata/metadatum/%s" + # % (cloud_owner, cloud_region_id, image_id, metadatainfo['metaname']), + # "PUT", content=metadatainfo) + # + # self._logger.debug("update_image,vimid:%s_%s req_to_aai: %s/%s, return %s, %s, %s" + # % (cloud_owner,cloud_region_id,image_id,metadatainfo['metaname'], + # retcode, content, status_code)) + # return retcode + # return 1 + + def _discover_images(self, vimid="", session=None, viminfo=None): cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for image in content.get('images'): + for image in self._get_list_resources( + "/v2/images", "image", session, viminfo, vimid, + "images"): image_info = { 'image-id': image['id'], 'image-name': image['name'], @@ -237,7 +167,9 @@ class Registry(APIView): 'image-architecture': image.get('architecture'), } - ret = self.update_image(cloud_owner, cloud_region_id, image_info) + ret = self._update_resoure( + cloud_owner, cloud_region_id, image['id'], image_info, + "image") if ret != 0: # failed to update image self._logger.debug("failed to populate image info into AAI: %s, image id: %s, ret:%s" @@ -255,57 +187,22 @@ class Registry(APIView): self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" % (vimid, req_resource, resp.status_code, content)) - if resp.status_code == status.HTTP_200_OK: + # if resp.status_code == status.HTTP_200_OK: # parse the schema? TBD # self.update_image(cloud_owner, cloud_region_id, image_info) #metadata_info = {} - pass - pass - - - def update_az(self, cloud_owner, cloud_region_id, azinfo): - ''' - populate available zone into AAI - :param cloud_owner: - :param cloud_region_id: - :param azinfo: - availability-zone-name: string - hypervisor-type: string - operational-status: string - :return: - ''' - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai( - "/cloud-infrastructure/cloud-regions/cloud-region" - + "/%s/%s/availability-zones/availability-zone/%s" - % (cloud_owner, cloud_region_id, azinfo['availability-zone-name']), - "PUT", content=azinfo) - - self._logger.debug("update_az,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, azinfo['availability-zone-name'], - retcode, content, status_code)) - - return retcode - return 1 # unknown cloud owner,region_id - - def discover_availablezones(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/os-availability-zone/detail" - service = {'service_type': "compute", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) + def _discover_availability_zones(self, vimid="", session=None, + viminfo=None): cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for az in content.get('availabilityZoneInfo'): + for az in self._get_list_resources( + "/os-availability-zone/detail", "compute", session, + viminfo, vimid, + "availabilityZoneInfo"): az_info = { 'availability-zone-name': az['zoneName'], - 'operational-status': az['zoneState']['available'] if az.get('zoneState') else '', + 'operational-status': az['zoneState']['available'] + if az.get('zoneState') else '', 'hypervisor-type': '', } if az.get('hosts'): @@ -324,119 +221,40 @@ class Registry(APIView): if len(content.get('hypervisors')) else '' break - ret = self.update_az(cloud_owner, cloud_region_id, az_info) + ret = self._update_resoure( + cloud_owner, cloud_region_id, az['zoneName'], az_info, + "availability-zone") if ret != 0: # failed to update image self._logger.debug("failed to populate az info into AAI: %s, az name: %s, ret:%s" % (vimid, az_info['availability-zone-name'], ret)) - continue - pass - - def update_vg(self, cloud_owner, cloud_region_id, vginfo): - ''' - populate volume group into AAI - :param cloud_owner: - :param cloud_region_id: - :param vginfo: - volume-group-id: string - volume-group-name: string - vnf-type: string - model-customization-id: string - heat-stack-id: string - orchestration-status: string - vf-module-model-customization-id: string - - :return: - ''' - - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai( - "/cloud-infrastructure/cloud-regions/cloud-region" - + "/%s/%s/volume-groups/volume-group/%s" - % (cloud_owner, cloud_region_id, vginfo['volume-group-id']), - "PUT", content=vginfo) - - self._logger.debug("update_vg,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, vginfo['volume-group-id'], - retcode, content, status_code)) - - return retcode - return 1 # unknown cloud owner,region_id - - def discover_volumegroups(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/consistencygroups/detail" - service = {'service_type': "volumev3", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) - - cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for cg in content.get('consistencygroups'): - vg_info = { - 'volume-group-id': cg['id'], - 'volume-group-name': cg['name'], - 'vnf-type': '', - } - - ret = self.update_az(cloud_owner, cloud_region_id, vg_info) - if ret != 0: - # failed to update image - self._logger.debug("failed to populate volumegroup info into AAI: %s, volume-group-id: %s, ret:%s" - % (vimid, vg_info['volume-group-id'], ret)) - continue - pass - - def update_snapshot(self, cloud_owner, cloud_region_id, snapshotinfo): - ''' - populate snapshot into AAI - :param cloud_owner: - :param cloud_region_id: - :param snapshotinfo: - snapshot-id: string - snapshot-name: string - snapshot-architecture: string - snapshot-os-distro: string - snapshot-os-version: string - application: string - application-vendor: string - application-version: string - snapshot-selflink: string - prev-snapshot-id: string - - :return: - ''' - - if cloud_owner and cloud_region_id: - retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/volume-groups/volume-group/%s" - % (cloud_owner, cloud_region_id, snapshotinfo['snapshot-id']), "PUT", content=snapshotinfo) - - self._logger.debug("update_snapshot,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" - % (cloud_owner,cloud_region_id, snapshotinfo['snapshot-id'], retcode, content, status_code)) - - return retcode - return 1 # unknown cloud owner,region_id - - def discover_snapshots(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/snapshots/detail" - service = {'service_type': "volumev3", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) + # def _discover_volumegroups(self, vimid="", session=None, viminfo=None): + # cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) + # for cg in self._get_list_resources( + # "/consistencygroups/detail", "volumev3", session, + # viminfo, vimid, + # "consistencygroups"): + # vg_info = { + # 'volume-group-id': cg['id'], + # 'volume-group-name': cg['name'], + # 'vnf-type': '', + # } + # + # ret = self._update_resoure( + # cloud_owner, cloud_region_id, cg['id'], vg_info, + # "volume-group") + # if ret != 0: + # # failed to update image + # self._logger.debug("failed to populate volumegroup info into AAI: %s, volume-group-id: %s, ret:%s" + # % (vimid, vg_info['volume-group-id'], ret)) + + def _discover_snapshots(self, vimid="", session=None, viminfo=None): cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for ss in content.get('snapshots'): + for ss in self._get_list_resources( + "/snapshots/detail", "volumev3", session, + viminfo, vimid, + "snapshots"): snapshot_info = { 'snapshot-id': ss['id'], 'snapshot-name': ss['name'], @@ -451,29 +269,21 @@ class Registry(APIView): snapshot_info['snapshot-selflink'] = ss['metadata'].get('selflink') snapshot_info['prev-snapshot-id'] = ss['metadata'].get('prev-snapshot-id') - ret = self.update_az(cloud_owner, cloud_region_id, snapshot_info) + ret = self._update_resoure( + cloud_owner, cloud_region_id, ss['id'], snapshot_info, + "snapshot") if ret != 0: # failed to update image self._logger.debug("failed to populate snapshot info into AAI: %s, snapshot-id: %s, ret:%s" % (vimid, snapshot_info['snapshot-id'], ret)) - continue - pass - def discover_servergroups(self, request, vimid="", session=None, viminfo=None): + # def _discover_servergroups(self, vimid="", session=None, viminfo=None): + # for sg in self._get_list_resources( + # "/os-server-groups", "compute", session, + # viminfo, vimid, + # "security groups"): - req_resource = "/os-server-groups" - service = {'service_type': "compute", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) - pass - - - def update_pserver(self, cloud_owner, cloud_region_id, pserverinfo): + def _update_pserver(self, cloud_owner, cloud_region_id, pserverinfo): ''' populate pserver into AAI :param cloud_owner: @@ -512,7 +322,7 @@ class Registry(APIView): if cloud_owner and cloud_region_id: retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/pservers/pserver/%s" + restcall.req_to_aai("/cloud-infrastructure/pservers/pserver/%s" % (pserverinfo['hostname']), "PUT", content=pserverinfo) self._logger.debug("update_snapshot,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" @@ -551,31 +361,22 @@ class Registry(APIView): } retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/pservers/pserver/%s/relationship-list/relationship" + restcall.req_to_aai("/cloud-infrastructure/pservers/pserver/%s/relationship-list/relationship" % (pserverinfo['hostname']), "PUT", content=relationship_data) self._logger.debug("update_pserver,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" % (cloud_owner, cloud_region_id, pserverinfo['hostname'], retcode, content, status_code)) - pass return retcode return 1 # unknown cloud owner,region_id - def discover_pservers(self, request, vimid="", session=None, viminfo=None): - - req_resource = "/os-hypervisors/detail" - service = {'service_type': "compute", - 'interface': 'public', - 'region_id': viminfo['cloud_region_id']} - resp = session.get(req_resource, endpoint_filter=service) - content = resp.json() - - self._logger.debug("vimid: %s, req: %s,resp code: %s, body: %s" - % (vimid, req_resource, resp.status_code,content)) - + def _discover_pservers(self, vimid="", session=None, viminfo=None): cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - for hypervisor in content.get('hypervisors'): + for hypervisor in self._get_list_resources( + "/os-hypervisors/detail", "compute", session, + viminfo, vimid, + "hypervisors"): hypervisor_info = { 'hostname': hypervisor['hypervisor_hostname'], 'in-maint': hypervisor['state'], @@ -593,16 +394,14 @@ class Registry(APIView): n_cpus = cputopo['cores'] * cputopo['threads'] * cputopo['sockets'] hypervisor_info['number-of-cpus'] = n_cpus - ret = self.update_pserver(cloud_owner, cloud_region_id, hypervisor_info) + ret = self._update_pserver(cloud_owner, cloud_region_id, + hypervisor_info) if ret != 0: # failed to update image self._logger.debug("failed to populate pserver info into AAI: %s, hostname: %s, ret:%s" % (vimid, hypervisor_info['hostname'], ret)) - continue - pass - - def update_epa_caps(self, cloud_owner, cloud_region_id, epa_caps_info): + def _update_epa_caps(self, cloud_owner, cloud_region_id, epa_caps_info): ''' populate cloud EPA Capabilities information into AAI :param cloud_owner: @@ -618,7 +417,7 @@ class Registry(APIView): if cloud_owner and cloud_region_id: retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/" + restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/" % (cloud_owner, cloud_region_id, ), "PUT", content=cloud_epa_caps) self._logger.debug( @@ -628,23 +427,21 @@ class Registry(APIView): return retcode return 1 # unknown cloud owner,region_id - def discover_epa_resources(self, request, vimid="", session=None, viminfo=None): + def _discover_epa_resources(self, vimid="", viminfo=None): cloud_epa_caps_info = {} cloud_extra_info = viminfo.get('cloud_extra_info') if cloud_extra_info: cloud_epa_caps_info.update(json.loads(cloud_extra_info)) - pass cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - ret = self.update_epa_caps(cloud_owner, cloud_region_id, cloud_epa_caps_info) + ret = self._update_epa_caps(cloud_owner, cloud_region_id, + cloud_epa_caps_info) if ret != 0: # failed to update image self._logger.debug("failed to populate EPA CAPs info into AAI: %s, ret:%s" % (vimid, ret)) - pass - - def update_proxy_identity_endpoint(self, cloud_owner, cloud_region_id, url): + def _update_proxy_identity_endpoint(self, cloud_owner, cloud_region_id, url): ''' update cloud_region's identity url :param cloud_owner: @@ -654,7 +451,7 @@ class Registry(APIView): ''' if cloud_owner and cloud_region_id: retcode, content, status_code = \ - req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s" + restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s" % (cloud_owner, cloud_region_id), "PUT", content={'identity-url': url}) self._logger.debug("update_proxy_identity_endpoint,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" @@ -667,41 +464,42 @@ class Registry(APIView): try: # populate proxy identity url cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid) - self.update_proxy_identity_endpoint(cloud_owner, cloud_region_id, - self.proxy_prefix + "/%s/identity/v3" % vimid) + self._update_proxy_identity_endpoint(cloud_owner, cloud_region_id, + self.proxy_prefix + "/%s/identity/v3" % vimid) # prepare request resource to vim instance # get token: viminfo = VimDriverUtils.get_vim_info(vimid) # set the default tenant since there is no tenant info in the VIM yet - sess = VimDriverUtils.get_session(viminfo, tenantname=request.data['defaultTenant']) + sess = VimDriverUtils.get_session( + viminfo, tenantname=request.data['defaultTenant']) # step 1. discover all projects and populate into AAI - self.discover_tenants(request, vimid,sess, viminfo) + self._discover_tenants(vimid, sess, viminfo) # discover all flavors - self.discover_flavors(request, vimid, sess, viminfo) + self._discover_flavors(vimid, sess, viminfo) # discover all images - self.discover_images(request, vimid, sess, viminfo) + self._discover_images(vimid, sess, viminfo) # discover all az - self.discover_availablezones(request, vimid, sess, viminfo) + self._discover_availability_zones(vimid, sess, viminfo) # discover all vg - #self.discover_volumegroups(request, vimid, sess, viminfo) + #self._discover_volumegroups(vimid, sess, viminfo) # discover all snapshots - self.discover_snapshots(request, vimid, sess, viminfo) + self._discover_snapshots(vimid, sess, viminfo) # discover all server groups #self.discover_servergroups(request, vimid, sess, viminfo) # discover all pservers - self.discover_pservers(request, vimid, sess, viminfo) + self._discover_pservers(vimid, sess, viminfo) # discover all epa resources, e.g. sriov pf and vf, etc. - self.discover_epa_resources(request, vimid, sess, viminfo) + self._discover_epa_resources(vimid, viminfo) return Response(status=status.HTTP_202_ACCEPTED) @@ -712,8 +510,9 @@ class Registry(APIView): 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) + return Response( + data={'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) def delete(self, request, vimid=""): self._logger.debug("Registration--delete::data> %s" % request.data) diff --git a/newton/newton/requests/tests/test_base.py b/newton/newton/requests/tests/test_base.py index 3e6b59c3..06f610cb 100644 --- a/newton/newton/requests/tests/test_base.py +++ b/newton/newton/requests/tests/test_base.py @@ -31,23 +31,20 @@ def get_mock_session(http_actions, response_dict={}): mock_session = mock.Mock( name='mock_session',spec=http_actions) for action in http_actions: - mock_response_obj = mock.Mock(spec=MockResponse) - mock_response_obj.content = response_dict.get( - action).get("content") - mock_response_obj.json.return_value = response_dict.get( - action).get("content") - mock_response_obj.status_code = response_dict.get( - action).get("status_code", status.HTTP_200_OK) - if action == "get": - mock_session.get.return_value = mock_response_obj - if action == "post": - mock_session.post.return_value = mock_response_obj - if action == "put": - mock_session.put.return_value = mock_response_obj - if action == "delete": - mock_session.delete.return_value = mock_response_obj - if action == "head": - mock_session.head.return_value = mock_response_obj + side_effect = response_dict.get("side_effect") + if side_effect and isinstance(side_effect, list): + mock_session.__getattr__(action).__setattr__( + "side_effect", side_effect) + else: + mock_response_obj = mock.Mock(spec=MockResponse) + mock_response_obj.content = response_dict.get( + action).get("content") + mock_response_obj.json.return_value = response_dict.get( + action).get("content") + mock_response_obj.status_code = response_dict.get( + action).get("status_code", status.HTTP_200_OK) + mock_session.__getattr__(action).__setattr__( + "return_value", mock_response_obj) return mock_session diff --git a/newton/newton/requests/tests/test_server.py b/newton/newton/requests/tests/test_server.py index c3e9e0f6..b5e6e3fe 100644 --- a/newton/newton/requests/tests/test_server.py +++ b/newton/newton/requests/tests/test_server.py @@ -84,6 +84,7 @@ MOCK_POST_SERVER_CREATED_THREAD_RESPONSE = { } } + class TestNetwork(test_base.TestRequest): @mock.patch.object(VimDriverUtils, 'get_vim_info') |