Merge "VMware: add method for getting hosts attached to datastore"

This commit is contained in:
Jenkins 2016-02-01 14:56:15 +00:00 committed by Gerrit Code Review
commit ac1968242f
2 changed files with 46 additions and 0 deletions

View File

@ -447,3 +447,30 @@ class DsUtilTestCase(test.NoDBTestCase):
"normal",
"VMFS",
datastore_regex))
def test_get_connected_hosts_none(self):
with mock.patch.object(self.session,
'_call_method') as _call_method:
hosts = ds_util.get_connected_hosts(self.session,
'fake_datastore')
self.assertEqual([], hosts)
_call_method.assert_called_once_with(
mock.ANY, 'get_object_property',
'fake_datastore', 'host')
def test_get_connected_hosts(self):
host = mock.Mock(spec=object)
host.value = 'fake-host'
host_mount = mock.Mock(spec=object)
host_mount.key = host
host_mounts = mock.Mock(spec=object)
host_mounts.DatastoreHostMount = [host_mount]
with mock.patch.object(self.session, '_call_method',
return_value=host_mounts) as _call_method:
hosts = ds_util.get_connected_hosts(self.session,
'fake_datastore')
self.assertEqual(['fake-host'], hosts)
_call_method.assert_called_once_with(
mock.ANY, 'get_object_property',
'fake_datastore', 'host')

View File

@ -484,3 +484,22 @@ def get_dc_info(session, ds_ref):
def dc_cache_reset():
global _DS_DC_MAPPING
_DS_DC_MAPPING = {}
def get_connected_hosts(session, datastore):
"""Get all the hosts to which the datastore is connected.
:param datastore: Reference to the datastore entity
:return: List of managed object references of all connected
hosts
"""
host_mounts = session._call_method(vutil, 'get_object_property',
datastore, 'host')
if not hasattr(host_mounts, 'DatastoreHostMount'):
return []
connected_hosts = []
for host_mount in host_mounts.DatastoreHostMount:
connected_hosts.append(host_mount.key.value)
return connected_hosts