Better sorting of flavors in the "Launch Instance" view

By default, returns the available flavors, sorted by RAM usage
(ascending).  Override these behaviours with a CREATE_INSTANCE_FLAVOR_SORT
dict in local_settings.py.

DocImpact: a new django setting, CREATE_INSTANCE_FLAVOR_SORT.

Change-Id: I9f466ff306739369a85f7176da27932958fc8867
Fixes: Bug 1196690
This commit is contained in:
Nicolas Simonds 2013-07-01 13:25:05 -07:00 committed by Gerrit Code Review
parent a68fae2588
commit f02f0ee1c9
2 changed files with 21 additions and 2 deletions

View File

@ -21,6 +21,7 @@
import json import json
import logging import logging
from django.conf import settings
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.decorators.debug import sensitive_variables from django.views.decorators.debug import sensitive_variables
@ -264,15 +265,23 @@ class SetInstanceDetailsAction(workflows.Action):
return choices return choices
def populate_flavor_choices(self, request, context): def populate_flavor_choices(self, request, context):
"""By default, returns the available flavors, sorted by RAM
usage (ascending).
Override these behaviours with a CREATE_INSTANCE_FLAVOR_SORT dict
in local_settings.py."""
try: try:
flavors = api.nova.flavor_list(request) flavors = api.nova.flavor_list(request)
flavor_sort = getattr(settings, 'CREATE_INSTANCE_FLAVOR_SORT', {})
rev = flavor_sort.get('reverse', False)
key = flavor_sort.get('key', lambda flavor: flavor.ram)
flavor_list = [(flavor.id, "%s" % flavor.name) flavor_list = [(flavor.id, "%s" % flavor.name)
for flavor in flavors] for flavor in sorted(flavors, key=key, reverse=rev)]
except: except:
flavor_list = [] flavor_list = []
exceptions.handle(request, exceptions.handle(request,
_('Unable to retrieve instance flavors.')) _('Unable to retrieve instance flavors.'))
return sorted(flavor_list) return flavor_list
def populate_availability_zone_choices(self, request, context): def populate_availability_zone_choices(self, request, context):
try: try:

View File

@ -184,6 +184,16 @@ API_RESULT_PAGE_SIZE = 20
# of your entire OpenStack installation, and hopefully be in UTC. # of your entire OpenStack installation, and hopefully be in UTC.
TIME_ZONE = "UTC" TIME_ZONE = "UTC"
# When launching an instance, the menu of available flavors is
# sorted by RAM usage, ascending. Provide a callback method here
# (and/or a flag for reverse sort) for the sorted() method if you'd
# like a different behaviour. For more info, see
# http://docs.python.org/2/library/functions.html#sorted
# CREATE_INSTANCE_FLAVOR_SORT = {
# 'key': my_awesome_callback_method,
# 'reverse': False,
# }
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
# When set to True this will disable all logging except # When set to True this will disable all logging except