Merge "Add some optimization around registered node query"

This commit is contained in:
Jenkins 2014-09-05 12:12:43 +00:00 committed by Gerrit Code Review
commit f04e97b170
3 changed files with 40 additions and 18 deletions

View File

@ -393,7 +393,7 @@ class Resource(base.APIResourceWrapper):
self._role = kwargs['role']
@classmethod
def get_by_node(cls, request, node):
def get_by_node(cls, request, node, all_resources=None):
"""Return the specified Heat Resource given a Node
:param request: request object
@ -409,13 +409,31 @@ class Resource(base.APIResourceWrapper):
# TODO(tzumainn): this is terribly inefficient, but I don't see a
# better way. Maybe if Heat set some node metadata. . . ?
if node.instance_uuid:
for stack in Stack.list(request):
for resource in stack.resources(with_joins=False):
if resource.physical_resource_id == node.instance_uuid:
return resource
if all_resources is None:
resource_list = cls.list_all_resources(request)
else:
resource_list = all_resources
for resource in resource_list:
if resource.physical_resource_id == node.instance_uuid:
return resource
msg = _('Could not find resource matching node "%s"') % node.uuid
raise exceptions.NotFound(msg)
@classmethod
def list_all_resources(cls, request):
"""Iterate through all the stacks and return all relevant resources
:param request: request object
:type request: django.http.HttpRequest
:return: list of resources
:rtype: list of tuskar_ui.api.heat.Resource
"""
all_resources = []
for stack in Stack.list(request):
all_resources.extend(stack.resources(with_joins=False))
return all_resources
@cached_property
def role(self):
"""Return the OvercloudRole associated with this Resource

View File

@ -84,16 +84,17 @@ class RegisteredTab(tabs.TableTab):
if 'errors' in self.request.GET:
return api.node.filter_nodes(nodes, healthy=False)
for node in nodes:
# TODO(tzumainn): this could probably be done more efficiently
# by getting the resource for all nodes at once
try:
resource = api.heat.Resource.get_by_node(self.request, node)
node.role_name = resource.role.name
node.role_id = resource.role.id
node.stack_id = resource.stack.id
except exceptions.NotFound:
node.role_name = '-'
if nodes:
all_resources = api.heat.Resource.list_all_resources(self.request)
for node in nodes:
try:
resource = api.heat.Resource.get_by_node(
self.request, node, all_resources=all_resources)
node.role_name = resource.role.name
node.role_id = resource.role.id
node.stack_id = resource.stack.id
except exceptions.NotFound:
node.role_name = '-'
return nodes

View File

@ -95,9 +95,10 @@ class NodesTests(test.BaseAdminViewTests, helpers.APITestCase):
'image_get.return_value': image,
}),
patch('tuskar_ui.api.heat.Resource', **{
'spec_set': ['get_by_node'], # Only allow these attributes
'spec_set': ['get_by_node', 'list_all_resources'],
'get_by_node.side_effect': (
self._raise_horizon_exception_not_found),
'list_all_resources.return_value': [],
}),
) as (_OvercloudRole, Node, _nova, _glance, _resource):
res = self.client.get(INDEX_URL + '?tab=nodes__registered')
@ -315,9 +316,10 @@ class NodesTests(test.BaseAdminViewTests, helpers.APITestCase):
'image_get.return_value': image,
}),
patch('tuskar_ui.api.heat.Resource', **{
'spec_set': ['get_by_node'], # Only allow these attributes
'spec_set': ['get_by_node', 'list_all_resources'],
'get_by_node.side_effect': (
self._raise_horizon_exception_not_found),
'list_all_resources.return_value': [],
}),
) as (mock_node_client, mock_node, mock_role, mock_nova, mock_glance,
mock_resource):
@ -362,9 +364,10 @@ class NodesTests(test.BaseAdminViewTests, helpers.APITestCase):
'image_get.return_value': image,
}),
patch('tuskar_ui.api.heat.Resource', **{
'spec_set': ['get_by_node'], # Only allow these attributes
'spec_set': ['get_by_node', 'list_all_resources'],
'get_by_node.side_effect': (
self._raise_horizon_exception_not_found),
'list_all_resources.return_value': [],
}),
) as (mock_node_client, mock_node, mock_role, mock_nova, mock_glance,
mock_resource):