Skip to content

Commit f2a3f2a

Browse files
committed
Merge branch 'morosi-feat' into 'master'
Add version argument to invoke_permission See merge request it/e3-aws!64
2 parents cf30a3b + 3b1c67c commit f2a3f2a

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/e3/aws/troposphere/awslambda/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def invoke_permission(
262262
service: str,
263263
source_arn: str,
264264
source_account: str | None = None,
265+
version: Version | Alias | None = None,
265266
) -> awslambda.Permission:
266267
"""Create a Lambda Permission object for a given service.
267268
@@ -271,11 +272,14 @@ def invoke_permission(
271272
:param source_account: account that holds the resource. This is
272273
mandatory only when using S3 as a service as a bucket arn is
273274
not linked to an account.
275+
:param version: specific version or alias to give permission for
274276
:return: an AWSObject
275277
"""
278+
target = version if version is not None else self
279+
276280
params = {
277281
"Action": "lambda:InvokeFunction",
278-
"FunctionName": self.ref,
282+
"FunctionName": target.ref,
279283
"Principal": f"{service}.amazonaws.com",
280284
"SourceArn": source_arn,
281285
}
@@ -284,7 +288,7 @@ def invoke_permission(
284288
if source_account is not None:
285289
params["SourceAccount"] = source_account
286290

287-
return awslambda.Permission(name_to_id(self.name + name_suffix), **params)
291+
return awslambda.Permission(name_to_id(target.name + name_suffix), **params)
288292

289293

290294
class DockerFunction(Function):

tests/tests_e3_aws/troposphere/awslambda/awslambda_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from e3.aws import AWSEnv
2323
from e3.aws.troposphere import Stack
2424
from e3.aws.troposphere.awslambda import (
25+
Function,
2526
PyFunction,
2627
Py38Function,
2728
DockerFunction,
@@ -1024,3 +1025,64 @@ def test_base64_response(base64_response_server: Flask) -> None:
10241025
assert response["statusCode"] == 200
10251026
assert response["headers"]["Content-Type"] == "image/png"
10261027
assert response["body"] == "4pavUE5H4pCN4pCK4pCa4pCK"
1028+
1029+
1030+
@pytest.mark.parametrize(
1031+
"version, expected_function_name_ref",
1032+
[
1033+
# Add the permission on the function itself
1034+
(
1035+
None,
1036+
"Mypylambda",
1037+
),
1038+
# Add the permission on a version of the function
1039+
(
1040+
Version(
1041+
name="myversion", description="this is some version", lambda_arn=""
1042+
),
1043+
"Myversion",
1044+
),
1045+
# Add the permission on an alias of the function
1046+
(
1047+
Alias(
1048+
name="myalias",
1049+
description="this is some alias",
1050+
lambda_arn="",
1051+
lambda_version="",
1052+
),
1053+
"Myalias",
1054+
),
1055+
],
1056+
)
1057+
def test_invoke_permission(
1058+
version: Version | Alias | None,
1059+
expected_function_name_ref: str,
1060+
) -> None:
1061+
"""Test Function.invoke_permission with various targets.
1062+
1063+
:param version: a version or alias of the function
1064+
:param expected_function_name_ref: name that should be referenced in FunctionName
1065+
"""
1066+
function = Function(
1067+
name="mypylambda",
1068+
description="this is a test",
1069+
role="somearn",
1070+
)
1071+
1072+
permission = function.invoke_permission(
1073+
name_suffix="TopicName",
1074+
service="sns",
1075+
source_arn="arn:aws:sns:eu-west-1:123456789012:TopicName",
1076+
version=version,
1077+
)
1078+
1079+
assert permission.title == f"{expected_function_name_ref}TopicName"
1080+
assert permission.to_dict() == {
1081+
"Properties": {
1082+
"Action": "lambda:InvokeFunction",
1083+
"FunctionName": {"Ref": expected_function_name_ref},
1084+
"Principal": "sns.amazonaws.com",
1085+
"SourceArn": "arn:aws:sns:eu-west-1:123456789012:TopicName",
1086+
},
1087+
"Type": "AWS::Lambda::Permission",
1088+
}

0 commit comments

Comments
 (0)