Avoid extra flavor_get in resize server form

commit d269b1640f fixes the bug on
the resize server form, but it introduced an extra flavor_get call
even when we retrieve the list of flavors. This commit recovers
the previous behavior that we first looks up the list of flavors.

Related-Bug: #1940834
Change-Id: I891aa6b8652f330326535732d0886362dfabb989
This commit is contained in:
Akihiro Motoki 2022-02-24 00:26:47 +09:00 committed by Radomir Dopieralski
parent ba079cbe38
commit a7956cd004
3 changed files with 13 additions and 31 deletions

View File

@ -2172,13 +2172,11 @@ class InstanceTests2(InstanceTestBase, InstanceTableTestMixin):
@helpers.create_mocks({api.nova: ('server_get', @helpers.create_mocks({api.nova: ('server_get',
'flavor_list', 'flavor_list',
'tenant_absolute_limits', 'tenant_absolute_limits',
'is_feature_available', 'is_feature_available')})
'flavor_get')})
def _test_instance_resize_get(self, server, nova_api_lt_2_47=False): def _test_instance_resize_get(self, server, nova_api_lt_2_47=False):
self.mock_server_get.return_value = server self.mock_server_get.return_value = server
self.mock_flavor_list.return_value = self.flavors.list() self.mock_flavor_list.return_value = self.flavors.list()
self.mock_tenant_absolute_limits.return_value = self.limits['absolute'] self.mock_tenant_absolute_limits.return_value = self.limits['absolute']
self.mock_flavor_get.return_value = self.flavors.first()
url = reverse('horizon:project:instances:resize', args=[server.id]) url = reverse('horizon:project:instances:resize', args=[server.id])
res = self.client.get(url) res = self.client.get(url)
@ -2220,11 +2218,6 @@ class InstanceTests2(InstanceTestBase, InstanceTableTestMixin):
mock.call(helpers.IsHttpRequest())) mock.call(helpers.IsHttpRequest()))
self.mock_tenant_absolute_limits.assert_called_once_with( self.mock_tenant_absolute_limits.assert_called_once_with(
helpers.IsHttpRequest(), reserved=True) helpers.IsHttpRequest(), reserved=True)
if nova_api_lt_2_47:
self.mock_flavor_get.assert_called_once_with(
helpers.IsHttpRequest(), server.flavor['id'])
else:
self.mock_flavor_get.assert_not_called()
def test_instance_resize_get_nova_api_lt_2_47(self): def test_instance_resize_get_nova_api_lt_2_47(self):
server = self.servers.first() server = self.servers.first()
@ -2250,11 +2243,9 @@ class InstanceTests2(InstanceTestBase, InstanceTableTestMixin):
helpers.IsHttpRequest(), server.id) helpers.IsHttpRequest(), server.id)
@helpers.create_mocks({api.nova: ('server_get', @helpers.create_mocks({api.nova: ('server_get',
'flavor_list', 'flavor_list',)})
'flavor_get')}) def test_instance_resize_get_flavor_list_exception(self):
def _test_instance_resize_get_flavor_list_exception( server = self.servers.first()
self, server, nova_api_lt_2_47=False):
self.mock_server_get.return_value = server
self.mock_flavor_list.side_effect = self.exceptions.nova self.mock_flavor_list.side_effect = self.exceptions.nova
url = reverse('horizon:project:instances:resize', url = reverse('horizon:project:instances:resize',
@ -2266,21 +2257,6 @@ class InstanceTests2(InstanceTestBase, InstanceTableTestMixin):
self.mock_server_get.assert_called_once_with(helpers.IsHttpRequest(), self.mock_server_get.assert_called_once_with(helpers.IsHttpRequest(),
server.id) server.id)
self.mock_flavor_list.assert_called_once_with(helpers.IsHttpRequest()) self.mock_flavor_list.assert_called_once_with(helpers.IsHttpRequest())
if nova_api_lt_2_47:
self.mock_flavor_get.assert_called_once_with(
helpers.IsHttpRequest(), server.flavor['id'])
else:
self.mock_flavor_get.assert_not_called()
def test_instance_resize_get_flavor_list_exception_nova_api_lt_2_47(self):
server = self.servers.first()
self._test_instance_resize_get_flavor_list_exception(
server, nova_api_lt_2_47=True)
def test_instance_resize_get_flavor_list_exception(self):
server = self.servers.first()
self._populate_server_flavor_nova_api_ge_2_47(server)
self._test_instance_resize_get_flavor_list_exception(server)
# TODO(amotoki): This is requred only when nova API <=2.46 is used. # TODO(amotoki): This is requred only when nova API <=2.46 is used.
# Once server_get() uses nova API >=2.47 only, this test can be droppped. # Once server_get() uses nova API >=2.47 only, this test can be droppped.

View File

@ -236,11 +236,12 @@ def server_group_field_data(request):
return [("", _("No server groups available")), ] return [("", _("No server groups available")), ]
def resolve_flavor(request, instance, **kwargs): def resolve_flavor(request, instance, flavors=None, **kwargs):
"""Resolves name of instance flavor independent of API microversion """Resolves name of instance flavor independent of API microversion
:param request: django http request object :param request: django http request object
:param instance: api._nova.Server instance to resolve flavor :param instance: api._nova.Server instance to resolve flavor
:param flavors: dict of flavors already retrieved
:param kwargs: flavor parameters to return if hit some flavor discrepancy :param kwargs: flavor parameters to return if hit some flavor discrepancy
:return: flavor name or default placeholder :return: flavor name or default placeholder
""" """
@ -252,8 +253,12 @@ def resolve_flavor(request, instance, **kwargs):
""" """
return namedtuple('Flavor', flavor_dict.keys())(*flavor_dict.values()) return namedtuple('Flavor', flavor_dict.keys())(*flavor_dict.values())
if flavors is None:
flavors = {}
flavor_id = instance.flavor.get('id') flavor_id = instance.flavor.get('id')
if flavor_id: # Nova API <=2.46 if flavor_id: # Nova API <=2.46
if flavor_id in flavors:
return flavors[flavor_id]
try: try:
return api.nova.flavor_get(request, flavor_id) return api.nova.flavor_get(request, flavor_id)
except Exception: except Exception:

View File

@ -588,8 +588,9 @@ class ResizeView(workflows.WorkflowView):
redirect = reverse("horizon:project:instances:index") redirect = reverse("horizon:project:instances:index")
msg = _('Unable to retrieve instance details.') msg = _('Unable to retrieve instance details.')
exceptions.handle(self.request, msg, redirect=redirect) exceptions.handle(self.request, msg, redirect=redirect)
instance.flavor_name = instance_utils.resolve_flavor(self.request, flavors = self.get_flavors()
instance).name flavor = instance_utils.resolve_flavor(self.request, instance, flavors)
instance.flavor_name = flavor.name
return instance return instance
@memoized.memoized_method @memoized.memoized_method