Skip to content

Commit

Permalink
feat(invoke): Add region parameter to all invoke related commands (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
sameergoyal authored and jfuss committed Aug 10, 2018
1 parent dcd65f4 commit 6657cba
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 42 deletions.
7 changes: 5 additions & 2 deletions samcli/commands/local/cli_common/invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def __init__(self,
aws_profile=None,
debug_port=None,
debug_args=None,
debugger_path=None):
debugger_path=None,
aws_region=None):
"""
Initialize the context
Expand Down Expand Up @@ -93,6 +94,7 @@ def __init__(self,
self._log_file = log_file
self._skip_pull_image = skip_pull_image
self._aws_profile = aws_profile
self._aws_region = aws_region
self._debug_port = debug_port
self._debug_args = debug_args
self._debugger_path = debugger_path
Expand Down Expand Up @@ -178,7 +180,8 @@ def local_lambda_runner(self):
cwd=self.get_cwd(),
env_vars_values=self._env_vars_value,
debug_context=self._debug_context,
aws_profile=self._aws_profile)
aws_profile=self._aws_profile,
aws_region=self._aws_region)

@property
def stdout(self):
Expand Down
3 changes: 3 additions & 0 deletions samcli/commands/local/cli_common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def invoke_common_options(f):
click.option('--profile',
help="Specify which AWS credentials profile to use."),

click.option('--region',
help="Specify which AWS region to use."),

]

# Reverse the list to maintain ordering of options in help text printed with --help
Expand Down
9 changes: 5 additions & 4 deletions samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
@click.argument('function_identifier', required=False)
@pass_context
def cli(ctx, function_identifier, template, event, env_vars, debug_port, debug_args, debugger_path,
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile):
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region):

# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing

do_cli(ctx, function_identifier, template, event, env_vars, debug_port, debug_args, debugger_path,
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile) # pragma: no cover
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region) # pragma: no cover


def do_cli(ctx, function_identifier, template, event, env_vars, debug_port, debug_args, # pylint: disable=R0914
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile):
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""
Expand All @@ -71,7 +71,8 @@ def do_cli(ctx, function_identifier, template, event, env_vars, debug_port, debu
aws_profile=profile,
debug_port=debug_port,
debug_args=debug_args,
debugger_path=debugger_path) as context:
debugger_path=debugger_path,
aws_region=region) as context:

# Invoke the function
context.local_lambda_runner.invoke(context.function_name,
Expand Down
9 changes: 7 additions & 2 deletions samcli/commands/local/lib/local_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def __init__(self,
cwd,
env_vars_values=None,
aws_profile=None,
debug_context=None):
debug_context=None,
aws_region=None):
"""
Initializes the class
Expand All @@ -39,13 +40,15 @@ def __init__(self,
:param integer debug_port: Optional. Port to bind the debugger to
:param string debug_args: Optional. Additional arguments passed to the debugger
:param string aws_profile: Optional. AWS Credentials profile to use
:param string aws_region: Optional. AWS region to use
"""

self.local_runtime = local_runtime
self.provider = function_provider
self.cwd = cwd
self.env_vars_values = env_vars_values or {}
self.aws_profile = aws_profile
self.aws_region = aws_region
self.debug_context = debug_context

def invoke(self, function_name, event, stdout=None, stderr=None):
Expand Down Expand Up @@ -195,7 +198,9 @@ def get_aws_creds(self):
result = {}

LOG.debug("Loading AWS credentials from session with profile '%s'", self.aws_profile)
session = boto3.session.Session(profile_name=self.aws_profile)
# TODO: Consider changing it to use boto3 default session. We already have an annotation
# to pass command line arguments for region & profile to setup boto3 default session
session = boto3.session.Session(profile_name=self.aws_profile, region_name=self.aws_region)

if not session:
return result
Expand Down
9 changes: 5 additions & 4 deletions samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ def cli(ctx,

# Common Options for Lambda Invoke
template, env_vars, debug_port, debug_args, debugger_path, docker_volume_basedir,
docker_network, log_file, skip_pull_image, profile
docker_network, log_file, skip_pull_image, profile, region
):
# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing

do_cli(ctx, host, port, static_dir, template, env_vars, debug_port, debug_args, debugger_path,
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile) # pragma: no cover
docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region) # pragma: no cover


def do_cli(ctx, host, port, static_dir, template, env_vars, debug_port, debug_args, # pylint: disable=R0914
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile):
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""
Expand All @@ -76,7 +76,8 @@ def do_cli(ctx, host, port, static_dir, template, env_vars, debug_port, debug_ar
aws_profile=profile,
debug_port=debug_port,
debug_args=debug_args,
debugger_path=debugger_path) as invoke_context:
debugger_path=debugger_path,
aws_region=region) as invoke_context:

service = LocalApiService(lambda_invoke_context=invoke_context,
port=port,
Expand Down
9 changes: 5 additions & 4 deletions samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ def cli(ctx,

# Common Options for Lambda Invoke
template, env_vars, debug_port, debug_args, debugger_path, docker_volume_basedir,
docker_network, log_file, skip_pull_image, profile
docker_network, log_file, skip_pull_image, profile, region
):
# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing

do_cli(ctx, host, port, template, env_vars, debug_port, debug_args, debugger_path, docker_volume_basedir,
docker_network, log_file, skip_pull_image, profile) # pragma: no cover
docker_network, log_file, skip_pull_image, profile, region) # pragma: no cover


def do_cli(ctx, host, port, template, env_vars, debug_port, debug_args, # pylint: disable=R0914
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile):
debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
"""
Expand All @@ -89,7 +89,8 @@ def do_cli(ctx, host, port, template, env_vars, debug_port, debug_args, # pylin
aws_profile=profile,
debug_port=debug_port,
debug_args=debug_args,
debugger_path=debugger_path) as invoke_context:
debugger_path=debugger_path,
aws_region=region) as invoke_context:

service = LocalLambdaService(lambda_invoke_context=invoke_context,
port=port,
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/commands/local/cli_common/test_invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def test_must_read_from_necessary_files(self, SamFunctionProviderMock):
aws_profile="profile",
debug_port=1111,
debugger_path="path-to-debugger",
debug_args='args')
debug_args='args',
aws_region="region")

template_dict = "template_dict"
invoke_context._get_template_data = Mock()
Expand Down Expand Up @@ -119,7 +120,8 @@ def test_must_work_in_with_statement(self, ExitMock, EnterMock):
aws_profile="profile",
debug_port=1111,
debugger_path="path-to-debugger",
debug_args='args') as context:
debug_args='args',
aws_region="region") as context:
self.assertEquals(context_obj, context)

EnterMock.assert_called_with()
Expand Down Expand Up @@ -167,7 +169,8 @@ def setUp(self):
aws_profile="profile",
debug_port=1111,
debugger_path="path-to-debugger",
debug_args='args')
debug_args='args',
aws_region="region")

@patch("samcli.commands.local.cli_common.invoke_context.ContainerManager")
@patch("samcli.commands.local.cli_common.invoke_context.LambdaRuntime")
Expand Down Expand Up @@ -198,7 +201,8 @@ def test_must_create_runner(self, LocalLambdaMock, LambdaRuntimeMock, ContainerM
cwd=cwd,
debug_context=None,
env_vars_values=ANY,
aws_profile="profile")
aws_profile="profile",
aws_region="region")


class TestInvokeContext_stdout_property(TestCase):
Expand Down
13 changes: 9 additions & 4 deletions tests/unit/commands/local/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setUp(self):
self.log_file = "logfile"
self.skip_pull_image = True
self.profile = "profile"
self.region = "region"

@patch("samcli.commands.local.invoke.cli.InvokeContext")
@patch("samcli.commands.local.invoke.cli._get_event")
Expand All @@ -50,7 +51,8 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo
docker_network=self.docker_network,
log_file=self.log_file,
skip_pull_image=self.skip_pull_image,
profile=self.profile)
profile=self.profile,
region=self.region)

InvokeContextMock.assert_called_with(template_file=self.template,
function_identifier=self.function_id,
Expand All @@ -62,7 +64,8 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo
aws_profile=self.profile,
debug_port=self.debug_port,
debug_args=self.debug_args,
debugger_path=self.debugger_path)
debugger_path=self.debugger_path,
aws_region=self.region)

context_mock.local_lambda_runner.invoke.assert_called_with(context_mock.function_name,
event=event_data,
Expand Down Expand Up @@ -96,7 +99,8 @@ def test_must_raise_user_exception_on_function_not_found(self, get_event_mock, I
docker_network=self.docker_network,
log_file=self.log_file,
skip_pull_image=self.skip_pull_image,
profile=self.profile)
profile=self.profile,
region=self.region)

msg = str(ex_ctx.exception)
self.assertEquals(msg, "Function {} not found in template".format(self.function_id))
Expand All @@ -123,7 +127,8 @@ def test_must_raise_user_exception_on_invalid_sam_template(self, get_event_mock,
docker_network=self.docker_network,
log_file=self.log_file,
skip_pull_image=self.skip_pull_image,
profile=self.profile)
profile=self.profile,
region=self.region)

msg = str(ex_ctx.exception)
self.assertEquals(msg, "bad template")
Expand Down
Loading

0 comments on commit 6657cba

Please sign in to comment.