VMware: add method for getting hosts attached to datastore

Add in a method that returns hosts attached to a datastore. This will
enable us to validate if the cinder volume is accessible by a running
instance.

Change-Id: I840ffda4926cea879d5945886ba87da72fc90805
Partial-bug: #1490257
This commit is contained in:
Gary Kotton 2015-09-03 03:50:30 -07:00
parent 727656d2f5
commit aadc18d883
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