Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sam remote invoke to take profile properly #5823

Merged
merged 2 commits into from
Aug 23, 2023
Merged
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
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