From 825321cd7af9fc033d05f7bfbf730b50e4c6533c Mon Sep 17 00:00:00 2001 From: Andrew Laski Date: Fri, 27 Jun 2014 16:23:27 -0400 Subject: [PATCH] Cells: Update set_admin_password for objects Cells code was calling the compute/api set_admin_password method with an old-world style instance and failing on the task_state attribute access. This adds set_admin_password to the list of methods handled via the cells RPC redirect method which is objects compatible. Change-Id: I12e212260ed62dc9380202de31fda928a9498dcb Closes-Bug: #1335315 --- nova/cells/manager.py | 5 ++++- nova/cells/messaging.py | 8 ++++++++ nova/cells/rpcapi.py | 9 +++++++++ nova/compute/api.py | 1 + nova/compute/cells_api.py | 11 ++--------- nova/tests/cells/test_cells_manager.py | 8 ++++++++ nova/tests/cells/test_cells_messaging.py | 6 ++++++ nova/tests/cells/test_cells_rpcapi.py | 11 +++++++++++ 8 files changed, 49 insertions(+), 10 deletions(-) diff --git a/nova/cells/manager.py b/nova/cells/manager.py index 5bad71fa8d74..62fc0f544b26 100644 --- a/nova/cells/manager.py +++ b/nova/cells/manager.py @@ -73,7 +73,7 @@ class CellsManager(manager.Manager): Scheduling requests get passed to the scheduler class. """ - target = oslo_messaging.Target(version='1.28') + target = oslo_messaging.Target(version='1.29') def __init__(self, *args, **kwargs): LOG.warn(_('The cells feature of Nova is considered experimental ' @@ -547,3 +547,6 @@ class CellsManager(manager.Manager): self.msg_runner.rebuild_instance(ctxt, instance, image_href, admin_password, files_to_inject, preserve_ephemeral, kwargs) + + def set_admin_password(self, ctxt, instance, new_pass): + self.msg_runner.set_admin_password(ctxt, instance, new_pass) diff --git a/nova/cells/messaging.py b/nova/cells/messaging.py index 2173a4b4da34..065ebdf92e7d 100644 --- a/nova/cells/messaging.py +++ b/nova/cells/messaging.py @@ -951,6 +951,10 @@ class _TargetedMessageMethods(_BaseMessageMethods): image_href, admin_password, files_to_inject, **kwargs) + def set_admin_password(self, message, instance, new_pass): + self._call_compute_api_with_obj(message.ctxt, instance, + 'set_admin_password', new_pass) + class _BroadcastMessageMethods(_BaseMessageMethods): """These are the methods that can be called as a part of a broadcast @@ -1806,6 +1810,10 @@ class MessageRunner(object): self._instance_action(ctxt, instance, 'rebuild_instance', extra_kwargs=extra_kwargs) + def set_admin_password(self, ctxt, instance, new_pass): + self._instance_action(ctxt, instance, 'set_admin_password', + extra_kwargs={'new_pass': new_pass}) + @staticmethod def get_message_types(): return _CELL_MESSAGE_TYPE_TO_MESSAGE_CLS.keys() diff --git a/nova/cells/rpcapi.py b/nova/cells/rpcapi.py index 769552e294bb..7ff0c7461514 100644 --- a/nova/cells/rpcapi.py +++ b/nova/cells/rpcapi.py @@ -96,6 +96,7 @@ class CellsAPI(object): can handle the version_cap being set to 1.27. * 1.28 - Make bdm_update_or_create_at_top and use bdm objects + * 1.29 - Adds set_admin_password() ''' VERSION_ALIASES = { @@ -607,3 +608,11 @@ class CellsAPI(object): instance=instance, image_href=image_ref, admin_password=new_pass, files_to_inject=injected_files, preserve_ephemeral=preserve_ephemeral, kwargs=kwargs) + + def set_admin_password(self, ctxt, instance, new_pass): + if not CONF.cells.enable: + return + + cctxt = self.client.prepare(version='1.29') + cctxt.cast(ctxt, 'set_admin_password', instance=instance, + new_pass=new_pass) diff --git a/nova/compute/api.py b/nova/compute/api.py index 9ae5f1d2622b..eef1c96b1618 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -2605,6 +2605,7 @@ class API(base.Base): @wrap_check_policy @check_instance_lock + @check_instance_cell @check_instance_state(vm_state=[vm_states.ACTIVE]) def set_admin_password(self, context, instance, password=None): """Set the root/admin password for the given instance. diff --git a/nova/compute/cells_api.py b/nova/compute/cells_api.py index 4abbd3625ce5..fd48c4390b1a 100644 --- a/nova/compute/cells_api.py +++ b/nova/compute/cells_api.py @@ -49,7 +49,8 @@ class ComputeRPCAPIRedirect(object): 'unpause_instance', 'revert_resize', 'confirm_resize', 'reset_network', 'inject_network_info', - 'backup_instance', 'snapshot_instance'] + 'backup_instance', 'snapshot_instance', + 'set_admin_password'] def __init__(self, cells_rpcapi): self.cells_rpcapi = cells_rpcapi @@ -335,14 +336,6 @@ class ComputeCellsAPI(compute_api.API): super(ComputeCellsAPI, self).unshelve(context, instance) self._cast_to_cells(context, instance, 'unshelve') - @check_instance_cell - def set_admin_password(self, context, instance, password=None): - """Set the root/admin password for the given instance.""" - super(ComputeCellsAPI, self).set_admin_password(context, instance, - password=password) - self._cast_to_cells(context, instance, 'set_admin_password', - password=password) - @wrap_check_policy @check_instance_cell def get_vnc_console(self, context, instance, console_type): diff --git a/nova/tests/cells/test_cells_manager.py b/nova/tests/cells/test_cells_manager.py index 1c214839bbc4..0ccd5e3cdca8 100644 --- a/nova/tests/cells/test_cells_manager.py +++ b/nova/tests/cells/test_cells_manager.py @@ -798,3 +798,11 @@ class CellsManagerClassTestCase(test.NoDBTestCase): image_id='fake-id', backup_type='backup-type', rotation='rotation') + + def test_set_admin_password(self): + with mock.patch.object(self.msg_runner, + 'set_admin_password') as set_admin_password: + self.cells_manager.set_admin_password(self.ctxt, + instance='fake-instance', new_pass='fake-password') + set_admin_password.assert_called_once_with(self.ctxt, + 'fake-instance', 'fake-password') diff --git a/nova/tests/cells/test_cells_messaging.py b/nova/tests/cells/test_cells_messaging.py index f5730b3086f3..32f237a897ca 100644 --- a/nova/tests/cells/test_cells_messaging.py +++ b/nova/tests/cells/test_cells_messaging.py @@ -1247,6 +1247,7 @@ class CellsTargetedMethodsTestCase(test.TestCase): 'confirm_resize': 'confirm_resize', 'reset_network': 'reset_network', 'inject_network_info': 'inject_network_info', + 'set_admin_password': 'set_admin_password', } tgt_method = method_translations.get(method, '%s_instance' % method) @@ -1400,6 +1401,11 @@ class CellsTargetedMethodsTestCase(test.TestCase): backup_type='backup-type', rotation='rotation') + def test_set_admin_password(self): + args = ['fake-password'] + self._test_instance_action_method('set_admin_password', args, {}, args, + {}, False) + class CellsBroadcastMethodsTestCase(test.TestCase): """Test case for _BroadcastMessageMethods class. Most of these diff --git a/nova/tests/cells/test_cells_rpcapi.py b/nova/tests/cells/test_cells_rpcapi.py index ec4ef26a2188..7292ee03fcd5 100644 --- a/nova/tests/cells/test_cells_rpcapi.py +++ b/nova/tests/cells/test_cells_rpcapi.py @@ -747,3 +747,14 @@ class CellsAPITestCase(test.NoDBTestCase): 'rotation': 'rotation'} self._check_result(call_info, 'backup_instance', expected_args, version='1.24') + + def test_set_admin_password(self): + call_info = self._stub_rpc_method('cast', None) + + self.cells_rpcapi.set_admin_password(self.fake_context, + 'fake-instance', 'fake-password') + + expected_args = {'instance': 'fake-instance', + 'new_pass': 'fake-password'} + self._check_result(call_info, 'set_admin_password', + expected_args, version='1.29')