Merge pull request #1544 from matburt/sorting_region_choices

Sort cloud regions in a stable way
This commit is contained in:
Matthew Jones 2018-03-13 18:02:31 -07:00 committed by GitHub
commit b646e675d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -38,7 +38,7 @@ from awx.main.models.notifications import (
NotificationTemplate,
JobNotificationMixin,
)
from awx.main.utils import _inventory_updates, get_ansible_version
from awx.main.utils import _inventory_updates, get_ansible_version, region_sorting
__all__ = ['Inventory', 'Host', 'Group', 'InventorySource', 'InventoryUpdate',
@ -1136,7 +1136,7 @@ class InventorySourceOptions(BaseModel):
label_parts.append(part)
label = ' '.join(label_parts)
regions.append((region.name, label))
return regions
return sorted(regions, key=region_sorting)
@classmethod
def get_ec2_group_by_choices(cls):
@ -1165,7 +1165,7 @@ class InventorySourceOptions(BaseModel):
# authenticating first. Therefore, use a list from settings.
regions = list(getattr(settings, 'GCE_REGION_CHOICES', []))
regions.insert(0, ('all', 'All'))
return regions
return sorted(regions, key=region_sorting)
@classmethod
def get_azure_rm_region_choices(self):
@ -1178,7 +1178,7 @@ class InventorySourceOptions(BaseModel):
# settings.
regions = list(getattr(settings, 'AZURE_RM_REGION_CHOICES', []))
regions.insert(0, ('all', 'All'))
return regions
return sorted(regions, key=region_sorting)
@classmethod
def get_vmware_region_choices(self):

View File

@ -174,3 +174,11 @@ def test_get_custom_venv_choices():
with TemporaryDirectory(dir=settings.BASE_VENV_PATH) as temp_dir:
os.makedirs(os.path.join(temp_dir, 'bin', 'activate'))
assert common.get_custom_venv_choices() == [os.path.join(temp_dir, '')]
def test_region_sorting():
s = [('Huey', 'China1'),
('Dewey', 'UK1'),
('Lewie', 'US1'),
('All', 'All')]
assert [x[1] for x in sorted(s, key=common.region_sorting)] == ['All', 'US1', 'China1', 'UK1']

View File

@ -44,7 +44,7 @@ logger = logging.getLogger('awx.main.utils')
__all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore', 'memoize', 'memoize_delete',
'get_ansible_version', 'get_ssh_version', 'get_licenser', 'get_awx_version', 'update_scm_url',
'get_type_for_model', 'get_model_for_type', 'copy_model_by_class',
'get_type_for_model', 'get_model_for_type', 'copy_model_by_class', 'region_sorting',
'copy_m2m_relationships', 'prefetch_page_capabilities', 'to_python_boolean',
'ignore_inventory_computed_fields', 'ignore_inventory_group_removal',
'_inventory_updates', 'get_pk_from_dict', 'getattrd', 'NoDefaultProvided',
@ -97,6 +97,14 @@ def to_python_boolean(value, allow_none=False):
raise ValueError(_(u'Unable to convert "%s" to boolean') % six.text_type(value))
def region_sorting(region):
if region[1].lower() == 'all':
return -1
elif region[1].lower().startswith('us'):
return 0
return region[1]
def camelcase_to_underscore(s):
'''
Convert CamelCase names to lowercase_with_underscore.