From 0dfae173e3ad302a2b0c28cbd977c3dce424afe0 Mon Sep 17 00:00:00 2001 From: Sam Liu Date: Tue, 13 Jul 2021 11:00:25 -0700 Subject: [PATCH] Improve account source selection --- .../commands/pipeline/bootstrap/guided_context.py | 12 ++++++------ .../pipeline/bootstrap/test_guided_context.py | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/samcli/commands/pipeline/bootstrap/guided_context.py b/samcli/commands/pipeline/bootstrap/guided_context.py index a282533081..02785c75bd 100644 --- a/samcli/commands/pipeline/bootstrap/guided_context.py +++ b/samcli/commands/pipeline/bootstrap/guided_context.py @@ -54,25 +54,25 @@ def _prompt_account_id(self) -> None: """ ) ) - if os.getenv(EnvProvider.ACCESS_KEY) and os.getenv(EnvProvider.SECRET_KEY): - click.echo(f" e. Environment variables: {EnvProvider.ACCESS_KEY} and {EnvProvider.SECRET_KEY}") + has_env_creds = os.getenv(EnvProvider.ACCESS_KEY) and os.getenv(EnvProvider.SECRET_KEY) + click.echo(f" 1. Environment variables{' (not available)' if not has_env_creds else ''}") for i, profile in enumerate(profiles): - click.echo(f" {i + 1}. {profile} (named profile)") + click.echo(f" {i + 2}. {profile} (named profile)") click.echo(" q. Quit and configure AWS credential myself") answer = click.prompt( "Select an account source to associate with this stage", show_choices=False, show_default=False, - type=click.Choice([str(i + 1) for i in range(len(profiles))] + ["q", "e"]), + type=click.Choice((["1"] if has_env_creds else []) + [str(i + 2) for i in range(len(profiles))] + ["q"]), ) if answer == "q": sys.exit(0) - elif answer == "e": + elif answer == "1": # by default, env variable has higher precedence # https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-list self.profile = None else: - self.profile = profiles[int(answer) - 1] + self.profile = profiles[int(answer) - 2] try: account_id = get_current_account_id(self.profile) diff --git a/tests/unit/commands/pipeline/bootstrap/test_guided_context.py b/tests/unit/commands/pipeline/bootstrap/test_guided_context.py index a4a4965478..c4c11e9792 100644 --- a/tests/unit/commands/pipeline/bootstrap/test_guided_context.py +++ b/tests/unit/commands/pipeline/bootstrap/test_guided_context.py @@ -131,14 +131,14 @@ def test_prompt_account_id_can_display_profiles_and_environment( ): getenv_mock.return_value = "not None" list_available_profiles_mock.return_value = ["profile1", "profile2"] - click_mock.prompt.return_value = "e" # select environment variable + click_mock.prompt.return_value = "1" # select environment variable get_current_account_id_mock.return_value = "account_id" guided_context_mock = Mock() GuidedContext._prompt_account_id(guided_context_mock) click_mock.prompt.assert_called_once_with( - ANY, show_choices=False, show_default=False, type=click_mock.Choice(["1", "2", "q", "e"]) + ANY, show_choices=False, show_default=False, type=click_mock.Choice(["1", "2", "3", "q"]) ) @patch("samcli.commands.pipeline.bootstrap.guided_context.get_current_account_id") @@ -150,14 +150,14 @@ def test_prompt_account_id_wont_show_environment_option_when_it_doesnt_exist( ): getenv_mock.return_value = None list_available_profiles_mock.return_value = ["profile1", "profile2"] - click_mock.prompt.return_value = "e" # select environment variable + click_mock.prompt.return_value = "1" # select environment variable get_current_account_id_mock.return_value = "account_id" guided_context_mock = Mock() GuidedContext._prompt_account_id(guided_context_mock) click_mock.prompt.assert_called_once_with( - ANY, show_choices=False, show_default=False, type=click_mock.Choice(["1", "2", "q"]) + ANY, show_choices=False, show_default=False, type=click_mock.Choice(["2", "3", "q"]) ) @patch("samcli.commands.pipeline.bootstrap.guided_context.get_current_account_id") @@ -169,7 +169,7 @@ def test_prompt_account_id_select_environment_unset_self_profile( ): getenv_mock.return_value = "not None" list_available_profiles_mock.return_value = ["profile1", "profile2"] - click_mock.prompt.return_value = "e" # select environment variable + click_mock.prompt.return_value = "1" # select environment variable get_current_account_id_mock.return_value = "account_id" guided_context_mock = Mock() @@ -180,11 +180,11 @@ def test_prompt_account_id_select_environment_unset_self_profile( @parameterized.expand( [ ( - "1", + "2", "profile1", ), ( - "2", + "3", "profile2", ), ]