Don't query compute_node through service object in nova-manage

The relationship between compute_node and service tables was
removed in a recent commit and to be able to remove compat code in
the objects, calls to get them should be done directly instead of
going through one or the other.

Since we are really just interested in the compute_node object here
this in addition saves us one extra db call.

Change-Id: I5270882321f7d16f993746498f751f17b0cb52fd
Related-Bug: #1438189
This commit is contained in:
Hans Lindgren 2015-03-30 13:53:17 +02:00
parent 55ab88dd55
commit 20a22adc3c
2 changed files with 51 additions and 7 deletions

View File

@ -775,15 +775,12 @@ class ServiceCommands(object):
"""
# Getting compute node info and related instances info
service_ref = objects.Service.get_by_compute_host(context, host)
instance_refs = db.instance_get_all_by_host(context,
service_ref.host)
compute_ref = (
objects.ComputeNode.get_first_node_by_host_for_old_compat(context,
host))
instance_refs = db.instance_get_all_by_host(context, host)
# Getting total available/used resource
# NOTE(sbauza): We're lazily loading the compute_node field here but
# we will change that later to get the ComputeNode object by using
# the Service host field
compute_ref = service_ref.compute_node
resource = {'vcpus': compute_ref.vcpus,
'memory_mb': compute_ref.memory_mb,
'local_gb': compute_ref.local_gb,

View File

@ -0,0 +1,47 @@
# 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 nova.cmd import manage
from nova import objects
from nova import test
class ServiceCommandsTestCase(test.NoDBTestCase):
def setUp(self):
super(ServiceCommandsTestCase, self).setUp()
self.svc_cmds = manage.ServiceCommands()
@mock.patch('nova.db.instance_get_all_by_host')
@mock.patch.object(objects.ComputeNode,
'get_first_node_by_host_for_old_compat')
def test__show_host_resources(self, mock_cn_get, mock_inst_get):
resources = {'vcpus': 4,
'memory_mb': 65536,
'local_gb': 100,
'vcpus_used': 1,
'memory_mb_used': 16384,
'local_gb_used': 20}
mock_cn_get.return_value = objects.ComputeNode(**resources)
mock_inst_get.return_value = []
result = self.svc_cmds._show_host_resources(mock.sentinel.ctxt,
mock.sentinel.host)
mock_cn_get.assert_called_once_with(mock.sentinel.ctxt,
mock.sentinel.host)
mock_inst_get.assert_called_once_with(mock.sentinel.ctxt,
mock.sentinel.host)
self.assertEqual(resources, result['resource'])
self.assertEqual({}, result['usage'])