Merge pull request #36509 from alexbleotu/get_root_folder-gh

Get root folder gh
This commit is contained in:
Mike Place 2016-09-26 18:52:50 +09:00 committed by GitHub
commit 6af9192a51
3 changed files with 58 additions and 1 deletions

View File

@ -386,6 +386,13 @@ class VMwareSaltError(CommandExecutionError):
'''
class VMwareRuntimeError(VMwareSaltError):
'''
Used when a runtime error is encountered when communicating with the
vCenter
'''
class VMwareConnectionError(VMwareSaltError):
'''
Used when the client fails to connect to a either a VMware vCenter server or

View File

@ -570,6 +570,22 @@ def get_inventory(service_instance):
return service_instance.RetrieveContent()
def get_root_folder(service_instance):
'''
Returns the root folder of a vCenter.
service_instance
The Service Instance Object for which to obtain the root folder.
'''
try:
log.trace('Retrieving root folder')
return service_instance.RetrieveContent().rootFolder
except vim.fault.VimFault as exc:
raise salt.exceptions.VMwareApiError(exc.msg)
except vmodl.RuntimeFault as exc:
raise salt.exceptions.VMwareRuntimeError(exc.msg)
def get_content(service_instance, obj_type, property_list=None,
container_ref=None, traversal_spec=None,
local_properties=False):

View File

@ -15,10 +15,11 @@ from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, \
PropertyMock
# Import Salt libraries
import salt.exceptions as excs
import salt.utils.vmware
# Import Third Party Libs
try:
from pyVmomi import vim
from pyVmomi import vim, vmodl
HAS_PYVMOMI = True
except ImportError:
HAS_PYVMOMI = False
@ -467,8 +468,41 @@ class GetContentTestCase(TestCase):
obj=container_ref_mock, skip=False, selectSet=None)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing')
class GetRootFolderTestCase(TestCase):
'''Tests for salt.utils.get_root_folder'''
def setUp(self):
self.mock_root_folder = MagicMock()
self.mock_content = MagicMock(rootFolder=self.mock_root_folder)
self.mock_si = MagicMock(
RetrieveContent=MagicMock(return_value=self.mock_content))
def test_raise_vim_fault(self):
exc = vim.fault.VimFault()
exc.msg = 'VimFault msg'
type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
with self.assertRaises(excs.VMwareApiError) as excinfo:
salt.utils.vmware.get_root_folder(self.mock_si)
self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
def test_raise_runtime_fault(self):
exc = vmodl.RuntimeFault()
exc.msg = 'RuntimeFault msg'
type(self.mock_content).rootFolder = PropertyMock(side_effect=exc)
with self.assertRaises(excs.VMwareRuntimeError) as excinfo:
salt.utils.vmware.get_root_folder(self.mock_si)
self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
def test_return(self):
ret = salt.utils.vmware.get_root_folder(self.mock_si)
self.assertEqual(ret, self.mock_root_folder)
if __name__ == '__main__':
from integration import run_tests
run_tests(WaitForTaskTestCase, needs_daemon=False)
run_tests(GetMorsWithPropertiesTestCase, needs_daemon=False)
run_tests(GetContentTestCase, needs_daemon=False)
run_tests(GetRootFolderTestCase, needs_daemon=False)