Skip to content

Commit ff14f72

Browse files
authored
Merge pull request #141 from pierretr/master
Enable arbitrary numbers of construct layers
2 parents 2258bbd + 2469570 commit ff14f72

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/e3/aws/troposphere/__init__.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22
from abc import ABC, abstractmethod
3+
from itertools import chain
34
from troposphere import AWSObject, Template
5+
46
from e3.aws import cfn, name_to_id, Session
57
from e3.aws.cfn.main import CFNMain
68
from e3.aws.troposphere.iam.policy_document import PolicyDocument
@@ -85,6 +87,23 @@ def __init__(
8587
self.dry_run = dry_run
8688
self.template = Template()
8789

90+
def construct_to_objects(self, construct: Construct | AWSObject) -> list[AWSObject]:
91+
"""Return list of AWS objects resources from a construct.
92+
93+
:param construct: construct to list resources from
94+
"""
95+
if isinstance(construct, AWSObject):
96+
return [construct]
97+
else:
98+
return list(
99+
chain.from_iterable(
100+
[
101+
self.construct_to_objects(el)
102+
for el in construct.resources(stack=self)
103+
]
104+
)
105+
)
106+
88107
def add(self, element: Union[AWSObject, Construct, Stack]) -> Stack:
89108
"""Add a Construct or AWSObject to the stack.
90109
@@ -103,14 +122,7 @@ def add(self, element: Union[AWSObject, Construct, Stack]) -> Stack:
103122
# Update the template
104123
resources = []
105124
for construct in constructs:
106-
if isinstance(construct, Construct):
107-
for el in construct.resources(stack=self):
108-
if isinstance(el, Construct):
109-
resources.extend(el.resources(stack=self))
110-
else:
111-
resources.append(el)
112-
if isinstance(construct, AWSObject):
113-
resources.append(construct)
125+
resources.extend(self.construct_to_objects(construct))
114126
self.template.add_resource(resources)
115127

116128
return self

0 commit comments

Comments
 (0)