From 964adebb3c4ca297dd61487dffffca48283be3b5 Mon Sep 17 00:00:00 2001 From: "Kevin L. Mitchell" Date: Wed, 20 Jun 2012 11:43:03 -0500 Subject: Admin action to reset states. Adds an Admin API action to reset the state of an instance. This will at least allow easy clean-up from bugs which corrupt the state of an instance and inhibit the owner of the instance from deleting it. Change-Id: Ia059cbd643e24e04dede06da330f444d03b07674 --- .../api/openstack/compute/contrib/admin_actions.py | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/admin_actions.py b/nova/api/openstack/compute/contrib/admin_actions.py index 18adf8377..72815ed00 100644 --- a/nova/api/openstack/compute/contrib/admin_actions.py +++ b/nova/api/openstack/compute/contrib/admin_actions.py @@ -22,6 +22,7 @@ from nova.api.openstack import common from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova import compute +from nova.compute import vm_states from nova import exception from nova import flags from nova import log as logging @@ -31,6 +32,10 @@ FLAGS = flags.FLAGS LOG = logging.getLogger(__name__) +# States usable in resetState action +state_map = dict(active=vm_states.ACTIVE, error=vm_states.ERROR) + + def authorize(context, action_name): action = 'admin_actions:%s' % action_name extensions.extension_authorizer('compute', action)(context) @@ -284,6 +289,33 @@ class AdminActionsController(wsgi.Controller): return webob.Response(status_int=202) + @wsgi.action('os-resetState') + def _reset_state(self, req, id, body): + """Permit admins to reset the state of a server.""" + context = req.environ["nova.context"] + authorize(context, 'resetState') + + # Identify the desired state from the body + try: + state = state_map[body["os-resetState"]["state"]] + except (TypeError, KeyError): + msg = _("Desired state must be specified. Valid states " + "are: %s") % ', '.join(sorted(state_map.keys())) + raise exc.HTTPBadRequest(explanation=msg) + + try: + instance = self.compute_api.get(context, id) + self.compute_api.update(context, instance, + vm_state=state, + task_state=None) + except exception.InstanceNotFound: + raise exc.HTTPNotFound(_("Server not found")) + except Exception: + readable = traceback.format_exc() + LOG.exception(_("Compute.api::resetState %s"), readable) + raise exc.HTTPUnprocessableEntity() + return webob.Response(status_int=202) + class Admin_actions(extensions.ExtensionDescriptor): """Enable admin-only server actions -- cgit