Skip to content

Commit 66a7bac

Browse files
ryansonshineRyan Sonshine
authored andcommitted
Add profile and role sorting to sso configure
This change adds sorting to profiles and roles when running `aws sso configure` listing them in ascending alphabetical order by default (case insensitive).
1 parent da94e8c commit 66a7bac

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "enhancement",
3+
"category": "``sso configure``",
4+
"description": "Add sorting to accounts and roles `#6108 <https://github.com/aws/aws-cli/issues/6108>`__"
5+
}

awscli/customizations/configure/sso.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ def _handle_multiple_accounts(self, accounts):
177177
'There are {} AWS accounts available to you.\n'
178178
)
179179
uni_print(available_accounts_msg.format(len(accounts)))
180-
selected_account = self._selector(accounts, display_account)
180+
sorted_accounts = sorted(accounts, key=lambda x: (
181+
'accountName' not in x, x.get('accountName',
182+
x.get('accountId')).lower()))
183+
selected_account = self._selector(sorted_accounts, display_account)
181184
sso_account_id = selected_account['accountId']
182185
return sso_account_id
183186

@@ -207,7 +210,8 @@ def _handle_single_role(self, roles):
207210
def _handle_multiple_roles(self, roles):
208211
available_roles_msg = 'There are {} roles available to you.\n'
209212
uni_print(available_roles_msg.format(len(roles)))
210-
role_names = [r['roleName'] for r in roles]
213+
sorted_roles = sorted(roles, key=lambda x: x['roleName'].lower())
214+
role_names = [r['roleName'] for r in sorted_roles]
211215
sso_role_name = self._selector(role_names)
212216
return sso_role_name
213217

tests/unit/customizations/configure/test_sso.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,61 @@ def test_prompts_suggest_values(self):
406406
]
407407
self.assert_prompt_completions(expected_completions)
408408

409+
def test_account_list_sorted_by_name(self):
410+
selected_account = {
411+
'accountId': self.account_id,
412+
'emailAddress': 'account@example.com',
413+
}
414+
first_account = {
415+
'accountId': '1111111111',
416+
'accountName': 'alpha',
417+
'emailAddress': 'alpha@example.com'
418+
}
419+
second_account = {
420+
'accountId': '2222222222',
421+
'accountName': 'Bravo',
422+
'emailAddress': 'Bravo@example.com'
423+
}
424+
third_account = {
425+
'accountId': '3333333333',
426+
'emailAddress': 'charlie@example.com'
427+
}
428+
accounts = [selected_account, second_account,
429+
third_account, first_account]
430+
expected_accounts = [first_account,
431+
second_account, selected_account, third_account]
432+
self._add_prompt_responses()
433+
self._add_list_accounts_response(accounts)
434+
self._add_list_account_roles_response([{'roleName': self.role_name}])
435+
self.selector.side_effect = [selected_account]
436+
with self.sso_stub:
437+
self.configure_sso(args=[], parsed_globals=self.global_args)
438+
printed_accounts = self.selector.call_args[0][0]
439+
self.assertEqual(printed_accounts, expected_accounts)
440+
441+
def test_role_list_sorted_by_name(self):
442+
selected_account = {
443+
'accountId': self.account_id,
444+
'emailAddress': 'account@example.com',
445+
}
446+
first_role = {'roleName': 'AdministratorAccess',
447+
'accountId': self.account_id}
448+
second_role = {'roleName': 'DataScientist',
449+
'accountId': self.account_id}
450+
third_role = {'roleName': 'SystemAdministrator',
451+
'accountId': self.account_id}
452+
roles = [second_role, third_role, first_role]
453+
expected_roles = [first_role['roleName'],
454+
second_role['roleName'], third_role['roleName']]
455+
self._add_prompt_responses()
456+
self._add_list_accounts_response([selected_account])
457+
self._add_list_account_roles_response(roles)
458+
self.selector.side_effect = [selected_account]
459+
with self.sso_stub:
460+
self.configure_sso(args=[], parsed_globals=self.global_args)
461+
printed_roles = self.selector.call_args[0][0]
462+
self.assertEqual(printed_roles, expected_roles)
463+
409464

410465
class TestDisplayAccount(unittest.TestCase):
411466
def setUp(self):

0 commit comments

Comments
 (0)