From 1dc6c806b32044420bf17d3076dd712185b7ac07 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 5 Dec 2012 23:13:37 +0000 Subject: Fix handling of unimplemented host actions Fixes bug #1060884 Host actions like enable/disable, maintenance mode and power management are not implemented in most virt drivers. In the case of set_host_enabled() in the libvirt driver, we have a no-op implementation. In other cases we have an implementation which raises NotImplementedError. Since the default implementation is to do this, we should just remove the redundant methods from the drivers which don't actually implement these actions. This ensures NotImplementedError is consistently raised. On the API side, rather than looking at the return value of these methods to determine whether they're implemented, we should just catch NotImplementedError and return a sensible error message. In order to do this, we need to ensure the NotImplementedError is properly deserialized from the RemoteError which will encapsualte it. The fix for bug #1086798 ensures this happens. Finally, add some tests to make sure we properly respond with 501 when the virt driver doesn't implement these actions. Change-Id: I2d4fc51a4b16d5230e61bde75915142ea450557d --- nova/api/openstack/compute/contrib/hosts.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/hosts.py b/nova/api/openstack/compute/contrib/hosts.py index 0d2b68f11..7da596a78 100644 --- a/nova/api/openstack/compute/contrib/hosts.py +++ b/nova/api/openstack/compute/contrib/hosts.py @@ -176,9 +176,11 @@ class HostController(object): context = req.environ['nova.context'] LOG.audit(_("Putting host %(host)s in maintenance " "mode %(mode)s.") % locals()) - result = self.api.set_host_maintenance(context, host, mode) - if result not in ("on_maintenance", "off_maintenance"): - raise webob.exc.HTTPBadRequest(explanation=result) + try: + result = self.api.set_host_maintenance(context, host, mode) + except NotImplementedError: + msg = _("Virt driver does not implement host maintenance mode.") + raise webob.exc.HTTPNotImplemented(explanation=msg) return {"host": host, "maintenance_mode": result} def _set_enabled_status(self, req, host, enabled): @@ -186,11 +188,12 @@ class HostController(object): context = req.environ['nova.context'] state = "enabled" if enabled else "disabled" LOG.audit(_("Setting host %(host)s to %(state)s.") % locals()) - result = self.api.set_host_enabled(context, host=host, - enabled=enabled) - if result not in ("enabled", "disabled"): - # An error message was returned - raise webob.exc.HTTPBadRequest(explanation=result) + try: + result = self.api.set_host_enabled(context, host=host, + enabled=enabled) + except NotImplementedError: + msg = _("Virt driver does not implement host disabled status.") + raise webob.exc.HTTPNotImplemented(explanation=msg) return {"host": host, "status": result} def _host_power_action(self, req, host, action): @@ -200,8 +203,9 @@ class HostController(object): try: result = self.api.host_power_action(context, host=host, action=action) - except NotImplementedError as e: - raise webob.exc.HTTPBadRequest(explanation=e.msg) + except NotImplementedError: + msg = _("Virt driver does not implement host power management.") + raise webob.exc.HTTPNotImplemented(explanation=msg) return {"host": host, "power_action": result} @wsgi.serializers(xml=HostActionTemplate) -- cgit