From bca18c6f10a186f9ffa94a4339b57c3a11da186a Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Tue, 19 Jan 2016 18:05:20 +0100 Subject: [PATCH] service instance: also recognize instance name Using the generic driver needs a service instance. The variable "service_instance_name_or_id" indicates that it's possible to use the UUID or the name of the Nova VM. Using the VM name currently only works, when the instance is in the current tenant. This fix searches the instance at first in the current tenant and if not found, it searches again over all tenants. Without this fix, you get the following error when using the name: Failed to get Nova VM. No server with a name or ID of 'myname' exists. Change-Id: I8770eff9752b76ebe490abf03a7ea6548282bf9b Closes-Bug: #1535775 --- manila/compute/nova.py | 13 ++++++++++--- manila/tests/compute/test_nova.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/manila/compute/nova.py b/manila/compute/nova.py index 2e742f2c73..18639cdb92 100644 --- a/manila/compute/nova.py +++ b/manila/compute/nova.py @@ -201,9 +201,16 @@ class API(base.Base): try: server = utils.find_resource( novaclient(context).servers, instance_name_or_id) - except nova_exception.CommandError as e: - msg = _("Failed to get Nova VM. %s") % e - raise exception.ManilaException(msg) + except nova_exception.CommandError: + # we did not find the server in the current tenant, + # and proceed searching in all tenants + try: + server = utils.find_resource( + novaclient(context).servers, instance_name_or_id, + all_tenants=True) + except nova_exception.CommandError as e: + msg = _("Failed to get Nova VM. %s") % e + raise exception.ManilaException(msg) return _untranslate_server_summary_view(server) @translate_server_exception diff --git a/manila/tests/compute/test_nova.py b/manila/tests/compute/test_nova.py index 9a15087692..42f1b905b1 100644 --- a/manila/tests/compute/test_nova.py +++ b/manila/tests/compute/test_nova.py @@ -170,6 +170,20 @@ class NovaApiTestCase(test.TestCase): self.assertEqual(instance_id, result['id']) utils.find_resource.assert_called_once_with(mock.ANY, instance_id) + def test_server_get_by_name_or_id_failed(self): + instance_id = 'instance_id1' + server = {'id': instance_id, 'fake_key': 'fake_value'} + self.mock_object(utils, 'find_resource', + mock.Mock(return_value=server, + side_effect=nova_exception.CommandError)) + + self.assertRaises(exception.ManilaException, + self.api.server_get_by_name_or_id, + self.ctx, instance_id) + utils.find_resource.assert_any_call(mock.ANY, instance_id) + utils.find_resource.assert_called_with(mock.ANY, instance_id, + all_tenants=True) + @ddt.data( {'nova_e': nova_exception.NotFound(404), 'manila_e': exception.InstanceNotFound},