11from __future__ import annotations
22from abc import ABC , abstractmethod
3+ from itertools import chain
34from troposphere import AWSObject , Template
5+
46from e3 .aws import cfn , name_to_id , Session
57from e3 .aws .cfn .main import CFNMain
68from 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