diff --git a/oslo_vmware/objects/datastore.py b/oslo_vmware/objects/datastore.py index c435fd5f..dbfa2e2a 100644 --- a/oslo_vmware/objects/datastore.py +++ b/oslo_vmware/objects/datastore.py @@ -15,6 +15,7 @@ import logging import posixpath import random +import re import six.moves.http_client as httplib import six.moves.urllib.parse as urlparse @@ -54,6 +55,34 @@ def get_datastore_by_ref(session, ds_ref): type=props.get("summary.type")) +def get_dsc_ref_and_name(session, dsc_val): + """Return reference and name of the specified datastore cluster. + + :param ds_val: datastore cluster name or datastore cluster moid + :return: tuple of dastastore cluster moref and datastore cluster name + """ + if re.match("group-p\d+", dsc_val): + # the configured value is moid + dsc_ref = vim_util.get_moref(dsc_val, 'StoragePod') + try: + dsc_name = session.invoke_api(vim_util, 'get_object_property', + session.vim, dsc_ref, 'name') + return dsc_ref, dsc_name + except exceptions.ManagedObjectNotFoundException: + # not a moid, try as a datastore cluster name + pass + + result = session.invoke_api(vim_util, 'get_objects', session.vim, + 'StoragePod', 100, ['name']) + with vim_util.WithRetrieval(session.vim, result) as objs: + for obj in objs: + if not hasattr(obj, 'propSet'): + continue + if obj.propSet[0].val == dsc_val: + return obj.obj, dsc_val + return None, None + + class Datastore(object): def __init__(self, ref, name, capacity=None, freespace=None, diff --git a/oslo_vmware/tests/objects/test_datastore.py b/oslo_vmware/tests/objects/test_datastore.py index 9ee23bb5..856191d6 100644 --- a/oslo_vmware/tests/objects/test_datastore.py +++ b/oslo_vmware/tests/objects/test_datastore.py @@ -169,6 +169,45 @@ class DatastoreTestCase(base.TestCase): self.assertFalse(datastore.Datastore.is_datastore_mount_usable(m)) +class DatastoreClusterTestCase(base.TestCase): + + def test_get_dsc_with_moid(self): + session = mock.Mock() + session.invoke_api = mock.Mock() + session.invoke_api.return_value = 'ds-cluster' + dsc_moid = 'group-p123' + dsc_ref, dsc_name = datastore.get_dsc_ref_and_name(session, dsc_moid) + self.assertEqual((dsc_moid, 'StoragePod'), + (dsc_ref.value, dsc_ref._type)) + self.assertEqual('ds-cluster', dsc_name) + session.invoke_api.assert_called_once_with(vim_util, + 'get_object_property', + session.vim, + mock.ANY, + 'name') + + @mock.patch('oslo_vmware.vim_util.continue_retrieval') + @mock.patch('oslo_vmware.vim_util.cancel_retrieval') + def test_get_dsc_by_name(self, cancel_retrieval, continue_retrieval): + pod_prop = mock.Mock() + pod_prop.val = 'ds-cluster' + pod_ref = vim_util.get_moref('group-p456', 'StoragePod') + pod = mock.Mock() + pod.propSet = [pod_prop] + pod.obj = pod_ref + + retrieve_result = mock.Mock() + retrieve_result.objects = [pod] + + session = mock.Mock() + session.invoke_api = mock.Mock() + session.invoke_api.return_value = retrieve_result + name = 'ds-cluster' + dsc_ref, dsc_name = datastore.get_dsc_ref_and_name(session, name) + self.assertEqual((pod_ref.value, pod_ref._type), + (dsc_ref.value, dsc_ref._type)) + + class DatastorePathTestCase(base.TestCase): """Test the DatastorePath object."""