diff --git a/tricircle/common/httpclient.py b/tricircle/common/httpclient.py index 586f9dfb..f693c133 100644 --- a/tricircle/common/httpclient.py +++ b/tricircle/common/httpclient.py @@ -22,7 +22,6 @@ from oslo_log import log as logging from tricircle.common import client from tricircle.common import constants as cons from tricircle.common.i18n import _LE -from tricircle.common import utils from tricircle.db import api as db_api @@ -162,7 +161,7 @@ def get_res_routing_ref(context, _id, t_url, s_type): :param s_type: service type :returns: service context """ - pod = utils.get_pod_by_top_id(context, _id) + pod = db_api.get_pod_by_top_id(context, _id) if not pod: return None diff --git a/tricircle/common/utils.py b/tricircle/common/utils.py index 389b95fe..71c01a04 100644 --- a/tricircle/common/utils.py +++ b/tricircle/common/utils.py @@ -22,7 +22,6 @@ from oslo_utils import uuidutils from tricircle.common import constants as cons import tricircle.common.exceptions as t_exceptions from tricircle.common.i18n import _ -import tricircle.db.api as db_api def get_import_path(cls): @@ -166,19 +165,3 @@ def format_nova_error(code, message, error_type=None): def format_cinder_error(code, message, error_type=None): return format_error(code, message, error_type) - - -def get_pod_by_top_id(context, _id): - """Get pod resource from pod table . - - :param _id: the top id of resource - :returns: pod resource - """ - mappings = db_api.get_bottom_mappings_by_top_id( - context, _id, - cons.RT_VOLUME) - - if not mappings or len(mappings) != 1: - return None - - return mappings[0][0] diff --git a/tricircle/db/api.py b/tricircle/db/api.py index 66e198f0..f21041eb 100644 --- a/tricircle/db/api.py +++ b/tricircle/db/api.py @@ -179,6 +179,25 @@ def delete_pre_created_resource_mapping(context, name): entries[0]['id']) +def get_pod_by_top_id(context, _id): + """Get pod resource from pod table by top id of resource + + :param context: context object + :param _id: the top id of resource + :returns: pod resource + """ + route_filters = [{'key': 'top_id', 'comparator': 'eq', 'value': _id}] + with context.session.begin(): + routes = core.query_resource( + context, models.ResourceRouting, route_filters, []) + if not routes or len(routes) != 1: + return None + route = routes[0] + if not route['bottom_id']: + return None + return core.get_resource(context, models.Pod, route['pod_id']) + + def get_bottom_id_by_top_id_region_name(context, top_id, region_name, resource_type): """Get resource bottom id by top id and bottom pod name diff --git a/tricircle/tests/unit/db/test_api.py b/tricircle/tests/unit/db/test_api.py index b2c4e989..50983b8e 100644 --- a/tricircle/tests/unit/db/test_api.py +++ b/tricircle/tests/unit/db/test_api.py @@ -183,6 +183,53 @@ class APITest(unittest.TestCase): self.assertEqual(routings['top_uuid_3']['top_id'], 'top_uuid_3') self.assertEqual(routings['top_uuid_3']['bottom_id'], 'top_uuid_3') + def test_get_pod_by_top_id(self): + self._create_pod(1, 'test_az_uuid1') + self._create_pod(2, 'test_az_uuid2') + routes = [ + { + 'top_id': 'top_uuid_1', + 'bottom_id': 'bottom_uuid_1', + 'pod_id': 'test_pod_uuid_1', + 'project_id': 'test_project_uuid_1', + 'resource_type': 'port' + }, + { + 'top_id': 'top_uuid_2', + 'bottom_id': 'bottom_uuid_2-1', + 'pod_id': 'test_pod_uuid_1', + 'project_id': 'test_project_uuid_1', + 'resource_type': 'network' + }, + { + 'top_id': 'top_uuid_2', + 'bottom_id': 'bottom_uuid_2-2', + 'pod_id': 'test_pod_uuid_2', + 'project_id': 'test_project_uuid_1', + 'resource_type': 'network' + }, + { + 'top_id': 'top_uuid_3', + 'bottom_id': '', + 'pod_id': 'test_pod_uuid_1', + 'project_id': 'test_project_uuid_1', + 'resource_type': 'port' + } + ] + + with self.context.session.begin(): + for route in routes: + core.create_resource( + self.context, models.ResourceRouting, route) + pod = api.get_pod_by_top_id(self.context, 'top_uuid_1') + self.assertEqual(pod['pod_id'], 'test_pod_uuid_1') + pod = api.get_pod_by_top_id(self.context, 'top_uuid_2') + # more than one routing entries found, method returns None + self.assertIsNone(pod) + pod = api.get_pod_by_top_id(self.context, 'top_uuid_3') + # bottom_id is empty, method returns None + self.assertIsNone(pod) + def test_get_next_bottom_pod(self): next_pod = api.get_next_bottom_pod(self.context) self.assertIsNone(next_pod)