Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/enhancement-ssoconfigure-32569.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "enhancement",
"category": "``sso configure``",
"description": "Add sorting to accounts and roles `#6108 <https://github.com/aws/aws-cli/issues/6108>`__"
}
15 changes: 13 additions & 2 deletions awscli/customizations/configure/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ def display_account(account):
return account_template.format(**account)


def get_account_sorting_key(account):
only_account_id = ('accountName' not in account and 'emailAddress' not in account)
for key in ('accountName', 'emailAddress', 'accountId'):
value = account.get(key, None)
if value is not None:
return (only_account_id, value.lower())
return (only_account_id, None)


class SSOSessionConfigurationPrompter:
_DEFAULT_SSO_SCOPE = 'sso:account:access'
_KNOWN_SSO_SCOPES = {
Expand Down Expand Up @@ -441,8 +450,9 @@ def _handle_multiple_accounts(self, accounts):
'There are {} AWS accounts available to you.\n'
)
uni_print(available_accounts_msg.format(len(accounts)))
sorted_accounts = sorted(accounts, key=get_account_sorting_key)
selected_account = self._selector(
accounts, display_format=display_account
sorted_accounts, display_format=display_account
)
sso_account_id = selected_account['accountId']
return sso_account_id
Expand Down Expand Up @@ -473,7 +483,8 @@ def _handle_single_role(self, roles):
def _handle_multiple_roles(self, roles):
available_roles_msg = 'There are {} roles available to you.\n'
uni_print(available_roles_msg.format(len(roles)))
role_names = [r['roleName'] for r in roles]
sorted_roles = sorted(roles, key=lambda x: x['roleName'].lower())
role_names = [r['roleName'] for r in sorted_roles]
sso_role_name = self._selector(role_names)
return sso_role_name

Expand Down
69 changes: 64 additions & 5 deletions tests/unit/customizations/configure/test_sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
SSOSessionConfigurationPrompter,
StartUrlValidator,
display_account,
get_account_sorting_key,
)
from awscli.customizations.sso.utils import (
PrintOnlyHandler,
Expand Down Expand Up @@ -359,13 +360,15 @@ def account_id_select(account_id):
}
return SelectMenu(
answer=selected_account,
expected_choices=[
selected_account,
{"accountId": "1234567890", "emailAddress": "account2@site.com"},
],
expected_choices=sorted(
[
selected_account,
{"accountId": "1234567890", "emailAddress": "account2@site.com"},
],
key=get_account_sorting_key,
)
)


@pytest.fixture
def role_name_select(role_name):
return SelectMenu(answer=role_name, expected_choices=[role_name, "roleB"])
Expand Down Expand Up @@ -1524,6 +1527,60 @@ def test_single_account_single_role_device_code_fallback(
],
)

def test_account_list_sorted_by_name(self):
selected_account = {
'accountId': self.account_id,
'emailAddress': 'account@example.com',
}
first_account = {
'accountId': '1111111111',
'accountName': 'alpha',
'emailAddress': 'alpha@example.com'
}
second_account = {
'accountId': '2222222222',
'accountName': 'Bravo',
'emailAddress': 'Bravo@example.com'
}
third_account = {
'accountId': '3333333333',
'emailAddress': 'charlie@example.com'
}
accounts = [selected_account, second_account,
third_account, first_account]
expected_accounts = [first_account,
second_account, selected_account, third_account]
self._add_prompt_responses()
self._add_list_accounts_response(accounts)
self._add_list_account_roles_response([{'roleName': self.role_name}])
self.selector.side_effect = [selected_account]
with self.sso_stub:
self.configure_sso(args=[], parsed_globals=self.global_args)
printed_accounts = self.selector.call_args[0][0]
self.assertEqual(printed_accounts, expected_accounts)

def test_role_list_sorted_by_name(self):
selected_account = {
'accountId': self.account_id,
'emailAddress': 'account@example.com',
}
first_role = {'roleName': 'AdministratorAccess',
'accountId': self.account_id}
second_role = {'roleName': 'DataScientist',
'accountId': self.account_id}
third_role = {'roleName': 'SystemAdministrator',
'accountId': self.account_id}
roles = [second_role, third_role, first_role]
expected_roles = [first_role['roleName'],
second_role['roleName'], third_role['roleName']]
self._add_prompt_responses()
self._add_list_accounts_response([selected_account])
self._add_list_account_roles_response(roles)
self.selector.side_effect = [selected_account]
with self.sso_stub:
self.configure_sso(args=[], parsed_globals=self.global_args)
printed_roles = self.selector.call_args[0][0]
self.assertEqual(printed_roles, expected_roles)

class TestPrintConclusion:
def test_print_conclusion_default_profile_with_credentials(
Expand Down Expand Up @@ -2059,6 +2116,8 @@ def passes_validator(validator, text):
(ScopesValidator, "value-1, value-2 value3", None, False),
],
)


def test_validators(validator_cls, input_value, default, is_valid):
validator = validator_cls(default)
assert passes_validator(validator, input_value) == is_valid
Expand Down
Loading