Skip to content

Commit

Permalink
Fix sam remote invoke to take profile properly (#5823)
Browse files Browse the repository at this point in the history
* Fix sam remote invoke to take profile properly

* error handling
  • Loading branch information
hawflau committed Aug 23, 2023
1 parent cae580d commit 3dc46e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
19 changes: 16 additions & 3 deletions samcli/commands/remote/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand All @@ -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
12 changes: 10 additions & 2 deletions tests/unit/commands/remote/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -106,6 +111,9 @@ def test_remote_invoke_command(
(InvalideBotoResponseException,),
(ErrorBotoApiCallException,),
(InvalidResourceBotoParameterException,),
(ProfileNotFound,),
(NoCredentialsError,),
(NoRegionError,),
]
)
@patch("samcli.commands.remote.remote_invoke_context.RemoteInvokeContext")
Expand Down

0 comments on commit 3dc46e5

Please sign in to comment.