From 3dc46e5b28eba1dca70376fd76addfd2402d9bb6 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Tue, 22 Aug 2023 23:01:12 -0700 Subject: [PATCH] Fix sam remote invoke to take profile properly (#5823) * Fix sam remote invoke to take profile properly * error handling --- samcli/commands/remote/invoke/cli.py | 19 ++++++++++++++++--- tests/unit/commands/remote/invoke/test_cli.py | 12 ++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/samcli/commands/remote/invoke/cli.py b/samcli/commands/remote/invoke/cli.py index 0318566b4a..030a8dbcac 100644 --- a/samcli/commands/remote/invoke/cli.py +++ b/samcli/commands/remote/invoke/cli.py @@ -117,6 +117,12 @@ def do_cli( """ Implementation of the ``cli`` method """ + from botocore.exceptions import ( + NoCredentialsError, + NoRegionError, + ProfileNotFound, + ) + from samcli.commands.exceptions import UserException from samcli.commands.remote.remote_invoke_context import RemoteInvokeContext from samcli.lib.remote_invoke.exceptions import ( @@ -127,9 +133,9 @@ def do_cli( from samcli.lib.remote_invoke.remote_invoke_executors import RemoteInvokeExecutionInfo from samcli.lib.utils.boto_utils import get_boto_client_provider_with_config, get_boto_resource_provider_with_config - boto_client_provider = get_boto_client_provider_with_config(region_name=region) - boto_resource_provider = get_boto_resource_provider_with_config(region_name=region) try: + boto_client_provider = get_boto_client_provider_with_config(region_name=region, profile=profile) + boto_resource_provider = get_boto_resource_provider_with_config(region_name=region, profile=profile) with RemoteInvokeContext( boto_client_provider=boto_client_provider, boto_resource_provider=boto_resource_provider, @@ -142,5 +148,12 @@ def do_cli( ) remote_invoke_context.run(remote_invoke_input=remote_invoke_input) - except (ErrorBotoApiCallException, InvalideBotoResponseException, InvalidResourceBotoParameterException) as ex: + except ( + ErrorBotoApiCallException, + InvalideBotoResponseException, + InvalidResourceBotoParameterException, + ProfileNotFound, + NoCredentialsError, + NoRegionError, + ) as ex: raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex diff --git a/tests/unit/commands/remote/invoke/test_cli.py b/tests/unit/commands/remote/invoke/test_cli.py index fe9179a891..2d30ccabc5 100644 --- a/tests/unit/commands/remote/invoke/test_cli.py +++ b/tests/unit/commands/remote/invoke/test_cli.py @@ -3,6 +3,11 @@ from parameterized import parameterized +from botocore.exceptions import ( + ProfileNotFound, + NoCredentialsError, + NoRegionError, +) from samcli.commands.remote.invoke.cli import do_cli from samcli.lib.remote_invoke.remote_invoke_executors import RemoteInvokeOutputFormat from samcli.lib.remote_invoke.exceptions import ( @@ -85,8 +90,8 @@ def test_remote_invoke_command( config_env=self.config_env, ) - patched_get_boto_client_provider_with_config.assert_called_with(region_name=self.region) - patched_get_boto_resource_provider_with_config.assert_called_with(region_name=self.region) + patched_get_boto_client_provider_with_config.assert_called_with(region_name=self.region, profile=self.profile) + patched_get_boto_resource_provider_with_config.assert_called_with(region_name=self.region, profile=self.profile) mock_remote_invoke_context.assert_called_with( boto_client_provider=given_client_provider, @@ -106,6 +111,9 @@ def test_remote_invoke_command( (InvalideBotoResponseException,), (ErrorBotoApiCallException,), (InvalidResourceBotoParameterException,), + (ProfileNotFound,), + (NoCredentialsError,), + (NoRegionError,), ] ) @patch("samcli.commands.remote.remote_invoke_context.RemoteInvokeContext")