Skip to content

Commit

Permalink
Add output unpacking function
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjjung committed Dec 18, 2024
1 parent de3959c commit 326bcf6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
5 changes: 1 addition & 4 deletions tb_pulumi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ def flatten(item: dict | list | ThunderbirdComponentResource | pulumi.Output | p

if to_flatten is not None:
for item in to_flatten:
if isinstance(item, pulumi.Output):
item.apply(lambda i: flattened.extend(flatten(i)))
else:
flattened.extend(flatten(item))
flattened.extend(flatten(item))

pulumi.info(f'FLATTENED: {set(flattened)}')
return set(flattened)
5 changes: 4 additions & 1 deletion tb_pulumi/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def __init__(
aws.cloudfront.Function: CloudFrontFunctionAlarmGroup,
aws.ecs.Service: EcsServiceAlarmGroup,
}

# Sometimes monitorable resources appear as outputs that we have to wait on resolution for; split them off
project_resources = self.project.flatten()
supported_resources = [
resource for resource in self.project.flatten() if type(resource) in supported_types.keys()
resource for resource in project_resources if type(resource) in supported_types.keys()
]

sns_topic = aws.sns.Topic(
Expand Down
34 changes: 34 additions & 0 deletions tb_pulumi/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ def __init__(
super().__init__(pulumi_type=pulumi_type, name=name, project=project, opts=opts)
self.config = config

# Not all things in a project's `resources` dict are actually Pulumi Resources. Sometimes we build resources
# downstream of a Pulumi Output, which makes those resources actually Outputs and not recognizable resource
# types. We can only detect what kind of thing those Outputs are from within a function called by an `apply`
# function. This necessitates an unpacking process on this end of things.

# Start with a list of all resources; sort them out into known and unknown things
_all_contents = self.project.flatten()
_all_resources = [res for res in _all_contents if not isinstance(res, pulumi.Output)]

def __parse_resource_item(
item: list | dict | pulumi.Output | pulumi.Resource | tb_pulumi.ThunderbirdComponentResource,
):
"""Given a Pulumi resource or output, or a list or dict of such, determine what kind of item we're dealing
with and respond appropriately.
"""

if type(item) is list:
for i in item:
__parse_resource_item(i)
elif type(item) is dict:
for i in item.values():
__parse_resource_item(i)
elif isinstance(item, tb_pulumi.ThunderbirdComponentResource):
__parse_resource_item(item.resources)
elif isinstance(item, pulumi.Resource):
_all_resources.append(item)
elif isinstance(item, pulumi.Output):
item.apply(__parse_resource_item)

_all_outputs = [res for res in _all_contents if isinstance(res, pulumi.Output)]
for output in _all_outputs:
__parse_resource_item(output)

pulumi.Output.all(*_all_outputs).apply(lambda outputs: pulumi.info(f'OUTPUTS: {outputs}'))

class AlarmGroup(tb_pulumi.ThunderbirdComponentResource):
"""A collection of alarms set up to monitor a particular single resource. For example, there are multiple metrics to
Expand Down

0 comments on commit 326bcf6

Please sign in to comment.