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
This commit is contained in:
zhiyuan_cai 2017-02-13 19:05:33 +08:00
parent cabc065551
commit e83eadb5de
4 changed files with 67 additions and 19 deletions

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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)