diff --git a/openstack_dashboard/api/nova.py b/openstack_dashboard/api/nova.py index 88ce1c4b7..17a786965 100644 --- a/openstack_dashboard/api/nova.py +++ b/openstack_dashboard/api/nova.py @@ -463,9 +463,11 @@ def get_auth_params_from_request(request): @memoized_with_request(get_auth_params_from_request) -def novaclient(request_auth_params): +def novaclient(request_auth_params, version=None): username, token_id, project_id, nova_url, auth_url = request_auth_params - c = nova_client.Client(VERSIONS.get_active_version()['version'], + if version is None: + version = VERSIONS.get_active_version()['version'] + c = nova_client.Client(version, username, token_id, project_id=project_id, @@ -478,6 +480,15 @@ def novaclient(request_auth_params): return c +def upgrade_api(request, client, version): + """Ugrade the nova API to the specified version if possible.""" + + min_ver, max_ver = api_versions._get_server_version_range(client) + if min_ver <= api_versions.APIVersion(version) <= max_ver: + client = novaclient(request, version) + return client + + @profiler.trace def server_vnc_console(request, instance_id, console_type='novnc'): return VNCConsole(novaclient(request).servers.get_vnc_console( @@ -900,7 +911,7 @@ def _merge_usage_list(usages, next_usage_list): @profiler.trace def usage_get(request, tenant_id, start, end): - client = novaclient(request) + client = upgrade_api(request, novaclient(request), '2.40') usage = client.usage.get(tenant_id, start, end) if client.api_version >= api_versions.APIVersion('2.40'): # If the number of instances used to calculate the usage is greater @@ -917,7 +928,7 @@ def usage_get(request, tenant_id, start, end): @profiler.trace def usage_list(request, start, end): - client = novaclient(request) + client = upgrade_api(request, novaclient(request), '2.40') usage_list = client.usage.list(start, end, True) if client.api_version >= api_versions.APIVersion('2.40'): # If the number of instances used to calculate the usage is greater diff --git a/openstack_dashboard/test/api_tests/nova_tests.py b/openstack_dashboard/test/api_tests/nova_tests.py index 43103bdda..315e14826 100644 --- a/openstack_dashboard/test/api_tests/nova_tests.py +++ b/openstack_dashboard/test/api_tests/nova_tests.py @@ -192,6 +192,9 @@ class ComputeApiTests(test.APITestCase): def test_usage_get(self): novaclient = self.stub_novaclient() + novaclient.versions = self.mox.CreateMockAnything() + novaclient.versions.get_current().AndReturn( + api_versions.APIVersion('2.1')) novaclient.usage = self.mox.CreateMockAnything() novaclient.usage.get(self.tenant.id, 'start', @@ -204,6 +207,9 @@ class ComputeApiTests(test.APITestCase): def test_usage_get_paginated(self): novaclient = self.stub_novaclient() + novaclient.versions = self.mox.CreateMockAnything() + novaclient.versions.get_current().AndReturn( + api_versions.APIVersion('2.40')) novaclient.api_version = api_versions.APIVersion('2.40') novaclient.usage = self.mox.CreateMockAnything() novaclient.usage.get(self.tenant.id, 'start', 'end')\ @@ -224,6 +230,9 @@ class ComputeApiTests(test.APITestCase): usages = self.usages.list() novaclient = self.stub_novaclient() + novaclient.versions = self.mox.CreateMockAnything() + novaclient.versions.get_current().AndReturn( + api_versions.APIVersion('2.1')) novaclient.usage = self.mox.CreateMockAnything() novaclient.usage.list('start', 'end', True).AndReturn(usages) self.mox.ReplayAll() @@ -236,6 +245,9 @@ class ComputeApiTests(test.APITestCase): usages = self.usages.list() novaclient = self.stub_novaclient() + novaclient.versions = self.mox.CreateMockAnything() + novaclient.versions.get_current().AndReturn( + api_versions.APIVersion('2.40')) novaclient.api_version = api_versions.APIVersion('2.40') novaclient.usage = self.mox.CreateMockAnything() novaclient.usage.list('start', 'end', True).AndReturn(usages)