Skip to content

Commit

Permalink
Raise 404 when instance not found in admin_actions API
Browse files Browse the repository at this point in the history
The pause/unpause/suspend/resume/reset_network admin action APIs do not
handle the case where the instance in the request is not found and they
raise HTTPUnprocessableEntity (code 422) instead. There are several
other operations in the same API which handle the 404 case and translate
it correctly, this patch cleans up the rest.

Closes-Bug: #1204999

Change-Id: If54d6be99db55b4f99da11fe75c14bf21685e809
  • Loading branch information
Matt Riedemann committed Aug 3, 2013
1 parent 2d86932 commit 43ad0a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nova/api/openstack/compute/contrib/admin_actions.py
Expand Up @@ -56,6 +56,8 @@ def _pause(self, req, id, body):
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'pause')
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
except Exception:
readable = traceback.format_exc()
LOG.exception(_("Compute.api::pause %s"), readable)
Expand All @@ -73,6 +75,8 @@ def _unpause(self, req, id, body):
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'unpause')
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
except Exception:
readable = traceback.format_exc()
LOG.exception(_("Compute.api::unpause %s"), readable)
Expand All @@ -90,6 +94,8 @@ def _suspend(self, req, id, body):
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'suspend')
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
except Exception:
readable = traceback.format_exc()
LOG.exception(_("compute.api::suspend %s"), readable)
Expand All @@ -107,6 +113,8 @@ def _resume(self, req, id, body):
except exception.InstanceInvalidState as state_error:
common.raise_http_conflict_for_instance_invalid_state(state_error,
'resume')
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
except Exception:
readable = traceback.format_exc()
LOG.exception(_("compute.api::resume %s"), readable)
Expand Down Expand Up @@ -137,6 +145,8 @@ def _reset_network(self, req, id, body):
try:
instance = self.compute_api.get(context, id)
self.compute_api.reset_network(context, instance)
except exception.InstanceNotFound:
raise exc.HTTPNotFound(_("Server not found"))
except Exception:
readable = traceback.format_exc()
LOG.exception(_("Compute.api::reset_network %s"), readable)
Expand Down
30 changes: 30 additions & 0 deletions nova/tests/api/openstack/compute/contrib/test_admin_actions.py
Expand Up @@ -63,6 +63,10 @@ def fake_compute_api_raises_invalid_state(*args, **kwargs):
instance_uuid='fake')


def fake_compute_api_raises_instance_not_found(*args, **kwargs):
raise exception.InstanceNotFound(instance_id='fake_id')


def fake_compute_api_get(self, context, instance_uuid, want_objects=False):
instance = fake_instance.fake_db_instance(
id=1, uuid=instance_uuid, vm_state=vm_states.ACTIVE,
Expand All @@ -89,6 +93,17 @@ class AdminActionsTest(test.TestCase):
('resume', 'resume'),
('migrate', 'resize'))

_actions_that_check_for_instance = (
# action, method
('pause', 'pause'),
('unpause', 'unpause'),
('suspend', 'suspend'),
('resume', 'resume'),
('resetNetwork', 'reset_network'),
('injectNetworkInfo', 'inject_network_info'),
('lock', 'lock'),
('unlock', 'unlock'))

def setUp(self):
super(AdminActionsTest, self).setUp()
self.stubs.Set(compute_api.API, 'get', fake_compute_api_get)
Expand Down Expand Up @@ -128,6 +143,21 @@ def test_admin_api_actions_raise_conflict_on_invalid_state(self):
self.assertIn("Cannot \'%(_action)s\' while instance" % locals(),
res.body)

def test_admin_api_actions_raise_not_found(self):
app = fakes.wsgi_app(init_only=('servers',))

for _action, _method in self._actions_that_check_for_instance:
self.stubs.Set(compute_api.API, _method,
fake_compute_api_raises_instance_not_found)

req = webob.Request.blank('/v2/fake/servers/%s/action' % self.UUID)
req.method = 'POST'
req.body = jsonutils.dumps({_action: None})
req.content_type = 'application/json'
res = req.get_response(app)
self.assertEqual(res.status_int, 404)
self.assertIn("could not be found", res.body)

def test_migrate_live_enabled(self):
ctxt = context.get_admin_context()
ctxt.user_id = 'fake'
Expand Down

0 comments on commit 43ad0a2

Please sign in to comment.