summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Kotton <gkotton@vmware.com>2014-08-30 23:07:11 -0700
committerGary Kotton <gkotton@vmware.com>2014-09-10 01:46:49 -0700
commit6c2495335c10744b1c583cad116bfe694c84f9d8 (patch)
tree0c13db678e2034b39fd926263036b112512cd642
parent48847a585757ace607de4ab5f8d8176142a39564 (diff)
downloadoslo-vmware-6c2495335c10744b1c583cad116bfe694c84f9d8.tar.gz
Add API to enable calling module to register an exception
In certain cases the application, for example Nova, may need to treat a VC exception that is currently not supported by oslo.vmware. This will enable the application to dynamically add exceptions and then remove them when oslo.vmware is updated to support the new exception. The patch also fixes a typo with the exception fault handling. Change-Id: I332a4ff2ac2c6d4e6ac88bbd067ef0d4e659ffa3
-rw-r--r--oslo/vmware/exceptions.py12
-rw-r--r--tests/test_api.py21
2 files changed, 32 insertions, 1 deletions
diff --git a/oslo/vmware/exceptions.py b/oslo/vmware/exceptions.py
index 8622c0f..7ca515b 100644
--- a/oslo/vmware/exceptions.py
+++ b/oslo/vmware/exceptions.py
@@ -222,10 +222,20 @@ _fault_classes_registry = {
def get_fault_class(name):
- """Get a named subclass of NovaException."""
+ """Get a named subclass of VMwareDriverException."""
name = str(name)
fault_class = _fault_classes_registry.get(name)
if not fault_class:
LOG.debug('Fault %s not matched.', name)
fault_class = VMwareDriverException
return fault_class
+
+
+def register_fault_class(name, exception):
+ fault_class = _fault_classes_registry.get(name)
+ if not issubclass(exception, VMwareDriverException):
+ raise TypeError(_("exception should be a subclass of "
+ "VMwareDriverException"))
+ if fault_class:
+ LOG.debug('Overriding exception for %s', name)
+ _fault_classes_registry[name] = exception
diff --git a/tests/test_api.py b/tests/test_api.py
index 6c02051..cd24fa5 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -487,3 +487,24 @@ class VMwareAPISessionTest(base.TestCase):
for k, v in _unknown_exceptions.iteritems():
self._poll_task_well_known_exceptions(k, v)
+
+ def _create_subclass_exception(self):
+ class VimSubClass(exceptions.VMwareDriverException):
+ pass
+ return VimSubClass
+
+ def test_register_fault_class(self):
+ exc = self._create_subclass_exception()
+ exceptions.register_fault_class('ValueError', exc)
+ self.assertEqual(exc, exceptions.get_fault_class('ValueError'))
+
+ def test_register_fault_class_override(self):
+ exc = self._create_subclass_exception()
+ exceptions.register_fault_class(exceptions.ALREADY_EXISTS, exc)
+ self.assertEqual(exc,
+ exceptions.get_fault_class(exceptions.ALREADY_EXISTS))
+
+ def test_register_fault_classi_invalid(self):
+ self.assertRaises(TypeError,
+ exceptions.register_fault_class,
+ 'ValueError', ValueError)