Skip to content

Commit 3b16c0d

Browse files
committed
Merge branch 'morosi-feat' into 'master'
Add Stack and CFNProjectMain extend functions See merge request it/e3-aws!50
2 parents b6e725c + 8415320 commit 3b16c0d

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/e3/aws/troposphere/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
if TYPE_CHECKING: # all: no cover
1212
from typing import Union
13+
from collections.abc import Iterable
1314
from troposphere import And, Condition, Equals, If, Not, Or
1415

1516
ConditionFunction = Union[And, Condition, Equals, If, Not, Or]
@@ -130,6 +131,16 @@ def add(self, element: AWSObject | Construct | Stack) -> Stack:
130131

131132
return self
132133

134+
def extend(self, elements: Iterable[AWSObject | Construct | Stack]) -> Stack:
135+
"""Add multiple Construct or AWSObject to the stack.
136+
137+
:param elements: see Stack.add
138+
"""
139+
for el in elements:
140+
self.add(el)
141+
142+
return self
143+
133144
def add_output(self, output: Output | list[Output]) -> None:
134145
"""Add outputs to stack template.
135146
@@ -232,3 +243,10 @@ def add(self, element: AWSObject | Construct | Stack) -> Stack:
232243
:param element: resource to add to the stack.
233244
"""
234245
return self.stack.add(element)
246+
247+
def extend(self, elements: list[AWSObject | Construct | Stack]) -> Stack:
248+
"""Add resources to project's stack.
249+
250+
:param elements: resources to add to the stack.
251+
"""
252+
return self.stack.extend(elements)

tests/tests_e3_aws/troposphere/cfn_project_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,30 @@ def test_cfn_project_main(capfd: pytest.CaptureFixture[str]) -> None:
5151
print(captured.out)
5252
with open(os.path.join(TEST_DIR, "cfn_project_test.out")) as f_out:
5353
assert captured.out == f_out.read()
54+
55+
56+
def test_cfn_project_main_extend(capfd: pytest.CaptureFixture[str]) -> None:
57+
"""Test adding resources with extend."""
58+
aws_env = AWSEnv(regions=["eu-west-1"], stub=True)
59+
test = MyCFNProject(
60+
name="TestProject",
61+
account_id="12345678",
62+
stack_description="TestStack",
63+
s3_bucket="cfn-test-deploy-bucket",
64+
regions=["eu-west-1"],
65+
)
66+
test.extend(
67+
[
68+
Role(
69+
name="TestRoleB",
70+
description="TestRoleB description",
71+
trust={"Service": "test"},
72+
)
73+
]
74+
)
75+
test.execute(args=["show"], aws_env=aws_env)
76+
77+
captured = capfd.readouterr()
78+
print(captured.out)
79+
with open(os.path.join(TEST_DIR, "cfn_project_test_extend.out")) as f_out:
80+
assert captured.out == f_out.read()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: TestStack
3+
Resources:
4+
TestRole:
5+
Properties:
6+
AssumeRolePolicyDocument:
7+
Statement:
8+
- Action: sts:AssumeRole
9+
Effect: Allow
10+
Principal:
11+
Service: test
12+
Version: '2012-10-17'
13+
Description: TestRole description
14+
Path: /
15+
RoleName: TestRole
16+
Tags:
17+
- Key: Name
18+
Value: TestRole
19+
Type: AWS::IAM::Role
20+
TestRoleB:
21+
Properties:
22+
AssumeRolePolicyDocument:
23+
Statement:
24+
- Action: sts:AssumeRole
25+
Effect: Allow
26+
Principal:
27+
Service: test
28+
Version: '2012-10-17'
29+
Description: TestRoleB description
30+
Path: /
31+
RoleName: TestRoleB
32+
Tags:
33+
- Key: Name
34+
Value: TestRoleB
35+
Type: AWS::IAM::Role
36+

tests/tests_e3_aws/troposphere/stack/stack_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ def test_add_outputs() -> None:
4949
expected_template = json.load(fd)
5050

5151
assert stack.export()["Resources"] == expected_template
52+
53+
54+
def test_extend() -> None:
55+
"""Test adding multiple construct and retrieving an AWSObject from a stack."""
56+
stack = Stack("test-stack", "this is a test stack")
57+
stack.extend([Bucket("my-bucket-a"), Bucket("my-bucket-b")])
58+
my_bucket = stack["my-bucket-b"]
59+
assert my_bucket

0 commit comments

Comments
 (0)