diff options
| author | Jenkins <jenkins@review.openstack.org> | 2011-11-09 17:18:21 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2011-11-09 17:18:21 +0000 |
| commit | 00861098508fc675ba2d90a5c34fec152ddf5c3d (patch) | |
| tree | 6b01c410de7862077d5e89f0f89a5083358dfefd | |
| parent | 96cf15ce782c4362daea4b178f418738846bb074 (diff) | |
| parent | 8977826c949fb6d3e1de6aa4965122d01436ae5c (diff) | |
Merge "Converting reboot to use instance objects"
| -rw-r--r-- | nova/api/ec2/cloud.py | 5 | ||||
| -rw-r--r-- | nova/api/openstack/servers.py | 14 | ||||
| -rw-r--r-- | nova/compute/api.py | 6 | ||||
| -rw-r--r-- | nova/scheduler/api.py | 6 | ||||
| -rw-r--r-- | nova/tests/api/ec2/test_cloud.py | 13 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_server_actions.py | 21 | ||||
| -rw-r--r-- | nova/tests/test_compute.py | 12 |
7 files changed, 61 insertions, 16 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index b2aa120e6..735b72cef 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -1458,7 +1458,10 @@ class CloudController(object): def reboot_instances(self, context, instance_id, **kwargs): """instance_id is a list of instance ids""" LOG.audit(_("Reboot instance %r"), instance_id, context=context) - self._do_instances(self.compute_api.reboot, context, instance_id) + for ec2_id in instance_id: + _instance_id = ec2utils.ec2_id_to_id(ec2_id) + instance = self.compute_api.get(context, _instance_id) + self.compute_api.reboot(context, instance, 'HARD') return True def stop_instances(self, context, instance_id, **kwargs): diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index e11d8ceb0..2a1424e46 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -142,6 +142,13 @@ class Controller(object): limited_list = self._limit_items(instance_list, req) return self._build_list(req, limited_list, is_detail=is_detail) + def _get_server(self, context, instance_uuid): + """Utility function for looking up an instance by uuid""" + try: + return self.compute_api.get(context, instance_uuid) + except exception.NotFound: + raise exc.HTTPNotFound() + def _handle_quota_error(self, error): """ Reraise quota errors as api-specific http exceptions @@ -609,9 +616,12 @@ class Controller(object): msg = _("Missing argument 'type' for reboot") LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) + + context = req.environ['nova.context'] + instance = self._get_server(context, id) + try: - self.compute_api.reboot(req.environ['nova.context'], id, - reboot_type) + self.compute_api.reboot(context, instance, reboot_type) except Exception, e: LOG.exception(_("Error in reboot %s"), e) raise exc.HTTPUnprocessableEntity() diff --git a/nova/compute/api.py b/nova/compute/api.py index 36ad07ed7..92e492390 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1178,15 +1178,15 @@ class API(base.Base): return recv_meta @scheduler_api.reroute_compute("reboot") - def reboot(self, context, instance_id, reboot_type): + def reboot(self, context, instance, reboot_type): """Reboot the given instance.""" state = {'SOFT': task_states.REBOOTING, 'HARD': task_states.REBOOTING_HARD}[reboot_type] self.update(context, - instance_id, + instance['id'], vm_state=vm_states.ACTIVE, task_state=state) - self._cast_compute_message('reboot_instance', context, instance_id, + self._cast_compute_message('reboot_instance', context, instance['id'], params={'reboot_type': reboot_type}) @scheduler_api.reroute_compute("rebuild") diff --git a/nova/scheduler/api.py b/nova/scheduler/api.py index 0b91edccc..41cdb80a3 100644 --- a/nova/scheduler/api.py +++ b/nova/scheduler/api.py @@ -332,6 +332,12 @@ class reroute_compute(object): context and resource id. Derived class should override this.""" context = kwargs.get('context', None) instance_id = kwargs.get('instance_id', None) + + #NOTE(blamar): This is going to get worse before it gets better... + instance = kwargs.get('instance', None) + if instance is not None: + instance_id = instance['uuid'] + if len(args) > 0 and not context: context = args[1] if len(args) > 1 and not instance_id: diff --git a/nova/tests/api/ec2/test_cloud.py b/nova/tests/api/ec2/test_cloud.py index f76690831..ce53d03cc 100644 --- a/nova/tests/api/ec2/test_cloud.py +++ b/nova/tests/api/ec2/test_cloud.py @@ -1371,6 +1371,19 @@ class CloudTestCase(test.TestCase): self._restart_compute_service() + def test_reboot_instances(self): + kwargs = {'image_id': 'ami-1', + 'instance_type': FLAGS.default_instance_type, + 'max_count': 1, } + instance_id = self._run_instance(**kwargs) + + # a running instance can't be started. It is just ignored. + result = self.cloud.start_instances(self.context, [instance_id]) + self.assertTrue(result) + + result = self.cloud.reboot_instances(self.context, [instance_id]) + self.assertTrue(result) + def _volume_create(self, volume_id=None): kwargs = {'status': 'available', 'host': self.volume.host, diff --git a/nova/tests/api/openstack/test_server_actions.py b/nova/tests/api/openstack/test_server_actions.py index 25bea252b..df0bfd968 100644 --- a/nova/tests/api/openstack/test_server_actions.py +++ b/nova/tests/api/openstack/test_server_actions.py @@ -30,6 +30,10 @@ def return_server_by_uuid(context, uuid): return stub_instance(1, uuid=uuid) +def return_server_by_uuid_not_found(context, uuid): + raise exception.NotFound() + + def instance_update(context, instance_id, kwargs): return stub_instance(instance_id) @@ -205,28 +209,37 @@ class ServerActionsControllerTest(test.TestCase): self.assertRaises(webob.exc.HTTPBadRequest, self.controller.action, req, FAKE_UUID, body) - def test_server_reboot_hard(self): + def test_reboot_hard(self): body = dict(reboot=dict(type="HARD")) req = fakes.HTTPRequest.blank(self.url) self.controller.action(req, FAKE_UUID, body) - def test_server_reboot_soft(self): + def test_reboot_soft(self): body = dict(reboot=dict(type="SOFT")) req = fakes.HTTPRequest.blank(self.url) self.controller.action(req, FAKE_UUID, body) - def test_server_reboot_incorrect_type(self): + def test_reboot_incorrect_type(self): body = dict(reboot=dict(type="NOT_A_TYPE")) req = fakes.HTTPRequest.blank(self.url) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.action, req, FAKE_UUID, body) - def test_server_reboot_missing_type(self): + def test_reboot_missing_type(self): body = dict(reboot=dict()) req = fakes.HTTPRequest.blank(self.url) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.action, req, FAKE_UUID, body) + def test_reboot_not_found(self): + self.stubs.Set(nova.db, 'instance_get_by_uuid', + return_server_by_uuid_not_found) + + body = dict(reboot=dict(type="HARD")) + req = fakes.HTTPRequest.blank(self.url) + self.assertRaises(webob.exc.HTTPNotFound, self.controller.action, + req, str(utils.gen_uuid()), body) + def test_server_rebuild_accepted_minimum(self): new_return_server = return_server_with_attributes(image_ref='2') self.stubs.Set(nova.db, 'instance_get', new_return_server) diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py index b5f627dcd..29269620c 100644 --- a/nova/tests/test_compute.py +++ b/nova/tests/test_compute.py @@ -343,7 +343,7 @@ class ComputeTestCase(test.TestCase): self.compute.resume_instance(self.context, instance_id) self.compute.terminate_instance(self.context, instance_id) - def test_soft_reboot_api(self): + def test_reboot_soft_api(self): """Ensure instance can be soft rebooted""" instance_id = self._create_instance() self.compute.run_instance(self.context, instance_id) @@ -352,14 +352,14 @@ class ComputeTestCase(test.TestCase): self.assertEqual(inst_ref['task_state'], None) reboot_type = "SOFT" - self.compute_api.reboot(self.context, instance_id, reboot_type) + self.compute_api.reboot(self.context, inst_ref, reboot_type) inst_ref = db.instance_get(self.context, instance_id) self.assertEqual(inst_ref['task_state'], task_states.REBOOTING) db.instance_destroy(self.context, instance_id) - def test_soft_reboot(self): + def test_reboot_soft(self): """Ensure instance can be soft rebooted""" instance_id = self._create_instance() self.compute.run_instance(self.context, instance_id) @@ -375,7 +375,7 @@ class ComputeTestCase(test.TestCase): self.compute.terminate_instance(self.context, instance_id) - def test_hard_reboot_api(self): + def test_reboot_hard_api(self): """Ensure instance can be hard rebooted""" instance_id = self._create_instance() self.compute.run_instance(self.context, instance_id) @@ -384,14 +384,14 @@ class ComputeTestCase(test.TestCase): self.assertEqual(inst_ref['task_state'], None) reboot_type = "HARD" - self.compute_api.reboot(self.context, instance_id, reboot_type) + self.compute_api.reboot(self.context, inst_ref, reboot_type) inst_ref = db.instance_get(self.context, instance_id) self.assertEqual(inst_ref['task_state'], task_states.REBOOTING_HARD) db.instance_destroy(self.context, instance_id) - def test_hard_reboot(self): + def test_reboot_hard(self): """Ensure instance can be hard rebooted""" instance_id = self._create_instance() self.compute.run_instance(self.context, instance_id) |
