Skip to content

Commit 68943d0

Browse files
committed
Refactor account sorting to a method
This takes into account email address as well, which is used when printing the output.
1 parent 9539bfd commit 68943d0

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

awscli/customizations/configure/sso.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ def display_account(account):
186186
return account_template.format(**account)
187187

188188

189+
def get_account_sorting_key(account):
190+
only_account_id = ('accountName' not in account and 'emailAddress' not in account)
191+
for key in ('accountName', 'emailAddress', 'accountId'):
192+
value = account.get(key, None)
193+
if value is not None:
194+
return (only_account_id, value.lower())
195+
return (only_account_id, None)
196+
197+
189198
class SSOSessionConfigurationPrompter:
190199
_DEFAULT_SSO_SCOPE = 'sso:account:access'
191200
_KNOWN_SSO_SCOPES = {
@@ -441,9 +450,7 @@ def _handle_multiple_accounts(self, accounts):
441450
'There are {} AWS accounts available to you.\n'
442451
)
443452
uni_print(available_accounts_msg.format(len(accounts)))
444-
sorted_accounts = sorted(accounts, key=lambda x: (
445-
'accountName' not in x, x.get('accountName',
446-
x.get('accountId')).lower()))
453+
sorted_accounts = sorted(accounts, key=get_account_sorting_key)
447454
selected_account = self._selector(
448455
sorted_accounts, display_format=display_account
449456
)

tests/unit/customizations/configure/test_sso.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
SSOSessionConfigurationPrompter,
3838
StartUrlValidator,
3939
display_account,
40+
get_account_sorting_key,
4041
)
4142
from awscli.customizations.sso.utils import (
4243
PrintOnlyHandler,
@@ -359,13 +360,15 @@ def account_id_select(account_id):
359360
}
360361
return SelectMenu(
361362
answer=selected_account,
362-
expected_choices=[
363-
selected_account,
364-
{"accountId": "1234567890", "emailAddress": "account2@site.com"},
365-
],
363+
expected_choices=sorted(
364+
[
365+
selected_account,
366+
{"accountId": "1234567890", "emailAddress": "account2@site.com"},
367+
],
368+
key=get_account_sorting_key,
369+
)
366370
)
367371

368-
369372
@pytest.fixture
370373
def role_name_select(role_name):
371374
return SelectMenu(answer=role_name, expected_choices=[role_name, "roleB"])

0 commit comments

Comments
 (0)