From e83eadb5de939459d798808a9278181f16468dbd Mon Sep 17 00:00:00 2001 From: zhiyuan_cai Date: Mon, 13 Feb 2017 19:05:33 +0800 Subject: [PATCH] Fix get_pod_by_top_id method in common.utils 1. What is the problem The current implementation of "get_pod_by_top_id" method uses a hard-coded "RT_VOLUME" value to pass to "get_bottom_mappings_by_top_id" method as the "resource_type" parameter, however "RT_VOLUME" no longer exists in common.constants module. 2. What is the solution to the problem Considering that resource ID will not be reused, this patch rewrites the logic to only query resource routing entries by resource ID. Also this patch moves "get_pod_by_top_id" method to db.api module. 3. What the features need to be implemented to the Tricircle No new features. Change-Id: Ie5663a72090a0a44678de5f78f701b50c27b962a --- tricircle/common/httpclient.py | 3 +- tricircle/common/utils.py | 17 ----------- tricircle/db/api.py | 19 ++++++++++++ tricircle/tests/unit/db/test_api.py | 47 +++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 19 deletions(-) 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 1afede11..bbf9bd27 100644 --- a/tricircle/tests/unit/db/test_api.py +++ b/tricircle/tests/unit/db/test_api.py @@ -166,6 +166,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)