diff options
| author | Jenkins <jenkins@review.openstack.org> | 2011-11-11 22:03:42 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2011-11-11 22:03:42 +0000 |
| commit | 4efb01f4cbcf29150bdaa608242605a6dfef4140 (patch) | |
| tree | c47285c206d90d738ae32a838aa065cf09994920 | |
| parent | 45af0a0ac8ed37be9e62fd5a92dbe96517da0517 (diff) | |
| parent | 574166045c6d4b7d8f5e2afeaa6d13aabf03bdb0 (diff) | |
| download | nova-4efb01f4cbcf29150bdaa608242605a6dfef4140.tar.gz nova-4efb01f4cbcf29150bdaa608242605a6dfef4140.tar.xz nova-4efb01f4cbcf29150bdaa608242605a6dfef4140.zip | |
Merge "Converting rescue/unrescue to use instance objects"
| -rw-r--r-- | nova/api/openstack/contrib/rescue.py | 20 | ||||
| -rw-r--r-- | nova/compute/api.py | 6 | ||||
| -rw-r--r-- | nova/tests/api/openstack/contrib/test_rescue.py | 10 | ||||
| -rw-r--r-- | nova/tests/test_compute.py | 25 |
4 files changed, 52 insertions, 9 deletions
diff --git a/nova/api/openstack/contrib/rescue.py b/nova/api/openstack/contrib/rescue.py index 4e1beb0ba..d3f38b200 100644 --- a/nova/api/openstack/contrib/rescue.py +++ b/nova/api/openstack/contrib/rescue.py @@ -17,12 +17,13 @@ import webob from webob import exc +from nova.api.openstack import extensions as exts +from nova.api.openstack import faults from nova import compute +from nova import exception from nova import flags from nova import log as logging from nova import utils -from nova.api.openstack import extensions as exts -from nova.api.openstack import faults FLAGS = flags.FLAGS @@ -41,24 +42,33 @@ class Rescue(exts.ExtensionDescriptor): super(Rescue, self).__init__(ext_mgr) self.compute_api = compute.API() + def _get_instance(self, context, instance_id): + try: + return self.compute_api.get(context, instance_id) + except exception.InstanceNotFound: + msg = _("Server not found") + raise exc.HTTPNotFound(msg) + @exts.wrap_errors def _rescue(self, input_dict, req, instance_id): """Rescue an instance.""" context = req.environ["nova.context"] + if input_dict['rescue'] and 'adminPass' in input_dict['rescue']: password = input_dict['rescue']['adminPass'] else: password = utils.generate_password(FLAGS.password_length) - self.compute_api.rescue(context, instance_id, rescue_password=password) + instance = self._get_instance(context, instance_id) + self.compute_api.rescue(context, instance, rescue_password=password) return {'adminPass': password} @exts.wrap_errors def _unrescue(self, input_dict, req, instance_id): """Unrescue an instance.""" context = req.environ["nova.context"] - self.compute_api.unrescue(context, instance_id) - + instance = self._get_instance(context, instance_id) + self.compute_api.unrescue(context, instance) return webob.Response(status_int=202) def get_actions(self): diff --git a/nova/compute/api.py b/nova/compute/api.py index 7e17c8819..5ec19b859 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1416,8 +1416,9 @@ class API(base.Base): self._cast_compute_message('resume_instance', context, instance_id) @scheduler_api.reroute_compute("rescue") - def rescue(self, context, instance_id, rescue_password=None): + def rescue(self, context, instance, rescue_password=None): """Rescue the given instance.""" + instance_id = instance['uuid'] self.update(context, instance_id, vm_state=vm_states.ACTIVE, @@ -1430,8 +1431,9 @@ class API(base.Base): params=rescue_params) @scheduler_api.reroute_compute("unrescue") - def unrescue(self, context, instance_id): + def unrescue(self, context, instance): """Unrescue the given instance.""" + instance_id = instance['uuid'] self.update(context, instance_id, vm_state=vm_states.RESCUED, diff --git a/nova/tests/api/openstack/contrib/test_rescue.py b/nova/tests/api/openstack/contrib/test_rescue.py index 403bcfd4c..f5b69865c 100644 --- a/nova/tests/api/openstack/contrib/test_rescue.py +++ b/nova/tests/api/openstack/contrib/test_rescue.py @@ -23,17 +23,23 @@ from nova.tests.api.openstack import fakes FLAGS = flags.FLAGS -def rescue(self, context, instance_id, rescue_password=None): +def rescue(self, context, instance, rescue_password=None): pass -def unrescue(self, context, instance_id): +def unrescue(self, context, instance): pass class RescueTest(test.TestCase): def setUp(self): super(RescueTest, self).setUp() + + def fake_compute_get(*args, **kwargs): + uuid = '70f6db34-de8d-4fbd-aafb-4065bdfa6114' + return {'id': 1, 'uuid': uuid} + + self.stubs.Set(compute.api.API, "get", fake_compute_get) self.stubs.Set(compute.api.API, "rescue", rescue) self.stubs.Set(compute.api.API, "unrescue", unrescue) diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index 2e15a7f26..7dd6cd8d1 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -1190,6 +1190,31 @@ class ComputeAPITestCase(BaseTestCase): self.compute.terminate_instance(self.context, instance_id) + def test_rescue_unrescue(self): + instance_id = self._create_instance() + self.compute.run_instance(self.context, instance_id) + + inst_ref = db.instance_get(self.context, instance_id) + self.assertEqual(inst_ref['vm_state'], vm_states.ACTIVE) + self.assertEqual(inst_ref['task_state'], None) + + self.compute_api.rescue(self.context, inst_ref) + + inst_ref = db.instance_get(self.context, instance_id) + self.assertEqual(inst_ref['vm_state'], vm_states.ACTIVE) + self.assertEqual(inst_ref['task_state'], task_states.RESCUING) + + params = {'vm_state': vm_states.RESCUED, 'task_state': None} + db.instance_update(self.context, instance_id, params) + + self.compute_api.unrescue(self.context, inst_ref) + + inst_ref = db.instance_get(self.context, instance_id) + self.assertEqual(inst_ref['vm_state'], vm_states.RESCUED) + self.assertEqual(inst_ref['task_state'], task_states.UNRESCUING) + + self.compute.terminate_instance(self.context, instance_id) + def test_snapshot(self): """Can't backup an instance which is already being backed up.""" instance_id = self._create_instance() |
