From 1927b365fdabd665d8801bee53945477a02582a0 Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Tue, 27 Sep 2016 10:51:40 +0100 Subject: [PATCH 1/3] Added VMwareObjectRetrievalError salt exception --- salt/exceptions.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/salt/exceptions.py b/salt/exceptions.py index 9fb85dcd46..bd396aca89 100644 --- a/salt/exceptions.py +++ b/salt/exceptions.py @@ -400,6 +400,12 @@ class VMwareConnectionError(VMwareSaltError): ''' +class VMwareObjectRetrievalError(VMwareSaltError): + ''' + Used when a VMware object cannot be retrieved + ''' + + class VMwareApiError(VMwareSaltError): ''' Used when representing a generic VMware API error From 9af1426b756a9321cc8931f1dd7bdb372dae99ed Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Thu, 22 Sep 2016 17:47:12 +0100 Subject: [PATCH 2/3] Added salt.utils.get_datacenter that returns a datacenter managed object --- salt/utils/vmware.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/salt/utils/vmware.py b/salt/utils/vmware.py index 9e91b33079..9dbd364226 100644 --- a/salt/utils/vmware.py +++ b/salt/utils/vmware.py @@ -801,6 +801,27 @@ def list_datacenters(service_instance): return list_objects(service_instance, vim.Datacenter) +def get_datacenter(service_instance, datacenter_name): + ''' + Returns a vim.Datacenter managed object. + + service_instance + The Service Instance Object from which to obtain datacenter. + + datacenter_name + The datacenter name + ''' + items = [i['object'] for i in + get_mors_with_properties(service_instance, + vim.Datacenter, + property_list=['name']) + if i['name'] == datacenter_name] + if not items: + raise salt.exceptions.VMwareObjectRetrievalError( + 'Datacenter \'{0}\' was not found'.format(datacenter_name)) + return items[0] + + def list_clusters(service_instance): ''' Returns a list of clusters associated with a given service instance. From 03533e9b017e6caf73d1ba59f8c3e33465a71c39 Mon Sep 17 00:00:00 2001 From: Alexandru Bleotu Date: Thu, 22 Sep 2016 17:45:52 +0100 Subject: [PATCH 3/3] Added tests for salt.utils.vmware.get_datacenter --- .../unit/utils/vmware_test/datacenter_test.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/unit/utils/vmware_test/datacenter_test.py diff --git a/tests/unit/utils/vmware_test/datacenter_test.py b/tests/unit/utils/vmware_test/datacenter_test.py new file mode 100644 index 0000000000..3b03bb9371 --- /dev/null +++ b/tests/unit/utils/vmware_test/datacenter_test.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +''' +:codeauthor: :email:`Alexandru Bleotu ` + +Tests for datacenter related functions in salt.utils.vmware +''' + +# Import python libraries +from __future__ import absolute_import +import logging +# Import Salt testing libraries +from salttesting import TestCase, skipIf +from salttesting.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock + +from salt.exceptions import VMwareObjectRetrievalError + +# Import Salt libraries +import salt.utils.vmware as vmware +# Import Third Party Libs +try: + from pyVmomi import vim + HAS_PYVMOMI = True +except ImportError: + HAS_PYVMOMI = False + +# Get Logging Started +log = logging.getLogger(__name__) + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +@skipIf(not HAS_PYVMOMI, 'The \'pyvmomi\' library is missing') +@patch('salt.utils.vmware.get_mors_with_properties', + MagicMock(return_value=[{'name': 'fake_dc', 'object': MagicMock()}])) +class GetDatacenterTestCase(TestCase): + '''Tests for salt.utils.vmware.get_datacenter''' + + def setUp(self): + self.mock_si = MagicMock() + self.mock_properties = [MagicMock()] + self.mock_dc1 = MagicMock() + self.mock_dc2 = MagicMock() + self.mock_entries = [{'name': 'fake_dc1', + 'object': self.mock_dc1}, + {'name': 'fake_dc2', + 'object': self.mock_dc2}] + + def test_get_mors_with_properties_call(self): + mock_get_mors_with_properties = MagicMock( + return_value=[{'name': 'fake_dc', 'object': MagicMock()}]) + with patch('salt.utils.vmware.get_mors_with_properties', + mock_get_mors_with_properties): + vmware.get_datacenter(self.mock_si, 'fake_dc') + mock_get_mors_with_properties.assert_called_once_with( + self.mock_si, vim.Datacenter, property_list=['name']) + + def test_get_mors_with_properties_returns_empty_array(self): + with patch('salt.utils.vmware.get_mors_with_properties', + MagicMock(return_value=[])): + with self.assertRaises(VMwareObjectRetrievalError) as excinfo: + vmware.get_datacenter(self.mock_si, 'fake_dc') + self.assertEqual(excinfo.exception.strerror, + 'Datacenter \'fake_dc\' was not found') + + def test_datastore_not_found(self): + with patch('salt.utils.vmware.get_mors_with_properties', + MagicMock(return_value=self.mock_entries)): + with self.assertRaises(VMwareObjectRetrievalError) as excinfo: + vmware.get_datacenter(self.mock_si, 'fake_dc') + self.assertEqual(excinfo.exception.strerror, + 'Datacenter \'fake_dc\' was not found') + + def test_datastore_found(self): + with patch('salt.utils.vmware.get_mors_with_properties', + MagicMock(return_value=self.mock_entries)): + res = vmware.get_datacenter(self.mock_si, 'fake_dc2') + self.assertEqual(res, self.mock_dc2) + + +if __name__ == '__main__': + from integration import run_tests + run_tests(GetDatacenterTestCase, needs_daemon=False)