Get flavor list sorted when launching instance
Currently, the flavor list is random on the panel of booting instance, which is not a good experience for end user. This patch fixes it and adds test accordingly. Closes-Bug: #1715042 Change-Id: Id5369ab75487d214a3395ba507b4394d4497903e
This commit is contained in:
@@ -25,6 +25,8 @@ from openstack_dashboard import api
|
|||||||
from openstack_dashboard.api.rest import json_encoder
|
from openstack_dashboard.api.rest import json_encoder
|
||||||
from openstack_dashboard.api.rest import urls
|
from openstack_dashboard.api.rest import urls
|
||||||
from openstack_dashboard.api.rest import utils as rest_utils
|
from openstack_dashboard.api.rest import utils as rest_utils
|
||||||
|
from openstack_dashboard.dashboards.project.instances \
|
||||||
|
import utils as instances_utils
|
||||||
from openstack_dashboard.usage import quotas
|
from openstack_dashboard.usage import quotas
|
||||||
|
|
||||||
|
|
||||||
@@ -543,6 +545,8 @@ class Flavors(generic.View):
|
|||||||
get_extras = bool(get_extras and get_extras.lower() == 'true')
|
get_extras = bool(get_extras and get_extras.lower() == 'true')
|
||||||
flavors = api.nova.flavor_list(request, is_public=is_public,
|
flavors = api.nova.flavor_list(request, is_public=is_public,
|
||||||
get_extras=get_extras)
|
get_extras=get_extras)
|
||||||
|
flavors = instances_utils.sort_flavor_list(request, flavors,
|
||||||
|
with_menu_label=False)
|
||||||
result = {'items': []}
|
result = {'items': []}
|
||||||
for flavor in flavors:
|
for flavor in flavors:
|
||||||
d = flavor.to_dict()
|
d = flavor.to_dict()
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def flavor_list(request):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def sort_flavor_list(request, flavors):
|
def sort_flavor_list(request, flavors, with_menu_label=True):
|
||||||
"""Utility method to sort a list of flavors.
|
"""Utility method to sort a list of flavors.
|
||||||
|
|
||||||
By default, returns the available flavors, sorted by RAM usage (ascending).
|
By default, returns the available flavors, sorted by RAM usage (ascending).
|
||||||
@@ -57,8 +57,12 @@ def sort_flavor_list(request, flavors):
|
|||||||
return get_key(flavor, sort_key)
|
return get_key(flavor, sort_key)
|
||||||
else:
|
else:
|
||||||
key = sort_key
|
key = sort_key
|
||||||
|
|
||||||
|
if with_menu_label:
|
||||||
flavor_list = [(flavor.id, '%s' % flavor.name)
|
flavor_list = [(flavor.id, '%s' % flavor.name)
|
||||||
for flavor in sorted(flavors, key=key, reverse=rev)]
|
for flavor in sorted(flavors, key=key, reverse=rev)]
|
||||||
|
else:
|
||||||
|
flavor_list = sorted(flavors, key=key, reverse=rev)
|
||||||
return flavor_list
|
return flavor_list
|
||||||
except Exception:
|
except Exception:
|
||||||
exceptions.handle(request,
|
exceptions.handle(request,
|
||||||
|
|||||||
@@ -26,6 +26,19 @@ from openstack_dashboard.test import helpers as test
|
|||||||
from openstack_dashboard.usage import quotas
|
from openstack_dashboard.usage import quotas
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE(flwang): mock.Mock and mock.MagicMock do not support sort, so the test
|
||||||
|
# case involved sorted will fail. This fake class is for the flavor test cases
|
||||||
|
# related to sort.
|
||||||
|
class FakeFlavor(object):
|
||||||
|
def __init__(self, id, ram=1):
|
||||||
|
self.id = id
|
||||||
|
self.ram = ram
|
||||||
|
self.extras = {}
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {"id": self.id}
|
||||||
|
|
||||||
|
|
||||||
class NovaRestTestCase(test.TestCase):
|
class NovaRestTestCase(test.TestCase):
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -761,8 +774,7 @@ class NovaRestTestCase(test.TestCase):
|
|||||||
else:
|
else:
|
||||||
request = self.mock_rest_request(GET={'is_public': 'fAlsE'})
|
request = self.mock_rest_request(GET={'is_public': 'fAlsE'})
|
||||||
self.mock_flavor_list.return_value = [
|
self.mock_flavor_list.return_value = [
|
||||||
mock.Mock(**{'to_dict.return_value': {'id': '1'}}),
|
FakeFlavor("1"), FakeFlavor("2")
|
||||||
mock.Mock(**{'to_dict.return_value': {'id': '2'}}),
|
|
||||||
]
|
]
|
||||||
response = nova.Flavors().get(request)
|
response = nova.Flavors().get(request)
|
||||||
self.assertStatusCode(response, 200)
|
self.assertStatusCode(response, 200)
|
||||||
@@ -790,9 +802,9 @@ class NovaRestTestCase(test.TestCase):
|
|||||||
get_extras = False
|
get_extras = False
|
||||||
else:
|
else:
|
||||||
request = self.mock_rest_request(GET={'get_extras': 'fAlsE'})
|
request = self.mock_rest_request(GET={'get_extras': 'fAlsE'})
|
||||||
|
|
||||||
self.mock_flavor_list.return_value = [
|
self.mock_flavor_list.return_value = [
|
||||||
mock.Mock(**{'extras': {}, 'to_dict.return_value': {'id': '1'}}),
|
FakeFlavor("1"), FakeFlavor("2")
|
||||||
mock.Mock(**{'extras': {}, 'to_dict.return_value': {'id': '2'}}),
|
|
||||||
]
|
]
|
||||||
response = nova.Flavors().get(request)
|
response = nova.Flavors().get(request)
|
||||||
self.assertStatusCode(response, 200)
|
self.assertStatusCode(response, 200)
|
||||||
|
|||||||
Reference in New Issue
Block a user