Skip to content

Commit

Permalink
Merge pull request #141 from pierretr/master
Browse files Browse the repository at this point in the history
Enable arbitrary numbers of construct layers
  • Loading branch information
pierretr authored Aug 12, 2022
2 parents 2258bbd + 2469570 commit ff14f72
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/e3/aws/troposphere/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from itertools import chain
from troposphere import AWSObject, Template

from e3.aws import cfn, name_to_id, Session
from e3.aws.cfn.main import CFNMain
from e3.aws.troposphere.iam.policy_document import PolicyDocument
Expand Down Expand Up @@ -85,6 +87,23 @@ def __init__(
self.dry_run = dry_run
self.template = Template()

def construct_to_objects(self, construct: Construct | AWSObject) -> list[AWSObject]:
"""Return list of AWS objects resources from a construct.
:param construct: construct to list resources from
"""
if isinstance(construct, AWSObject):
return [construct]
else:
return list(
chain.from_iterable(
[
self.construct_to_objects(el)
for el in construct.resources(stack=self)
]
)
)

def add(self, element: Union[AWSObject, Construct, Stack]) -> Stack:
"""Add a Construct or AWSObject to the stack.
Expand All @@ -103,14 +122,7 @@ def add(self, element: Union[AWSObject, Construct, Stack]) -> Stack:
# Update the template
resources = []
for construct in constructs:
if isinstance(construct, Construct):
for el in construct.resources(stack=self):
if isinstance(el, Construct):
resources.extend(el.resources(stack=self))
else:
resources.append(el)
if isinstance(construct, AWSObject):
resources.append(construct)
resources.extend(self.construct_to_objects(construct))
self.template.add_resource(resources)

return self
Expand Down

0 comments on commit ff14f72

Please sign in to comment.