From b913ffef1831b043c6b536711e331b25466fcf8d Mon Sep 17 00:00:00 2001 From: "John \"Preston\" Mille" Date: Wed, 24 Jan 2024 08:24:29 +0000 Subject: [PATCH] Ingress for LB from macroparameter --- ecs_composex/compose/x_resources/__init__.py | 6 ++- ecs_composex/elbv2/elbv2_stack/__init__.py | 5 ++- ecs_composex/elbv2/elbv2_stack/elbv2.py | 46 +++++++++++++++----- ecs_composex/mods_manager.py | 4 +- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/ecs_composex/compose/x_resources/__init__.py b/ecs_composex/compose/x_resources/__init__.py index 8e3c439d..7cc98f95 100644 --- a/ecs_composex/compose/x_resources/__init__.py +++ b/ecs_composex/compose/x_resources/__init__.py @@ -91,7 +91,7 @@ def __init__( self.validators = [] self.logical_name = NONALPHANUM.sub("", self.name) self.settings = set_else_none("Settings", definition, alt_value={}) - self.parameters = set_else_none("MacroParameters", definition, alt_value={}) + self._parameters = {} self.lookup = set_else_none("Lookup", definition, alt_value={}) if self.lookup: self.lookup_session = define_lookup_role_from_info( @@ -129,6 +129,10 @@ def __init__( def __repr__(self): return self.logical_name + @property + def parameters(self) -> dict: + return set_else_none("MacroParameters", self.definition, alt_value={}) + @property def uses_default(self) -> bool: return not any([self.lookup, self.parameters, self.properties]) diff --git a/ecs_composex/elbv2/elbv2_stack/__init__.py b/ecs_composex/elbv2/elbv2_stack/__init__.py index 1fb275b2..d0a133cf 100644 --- a/ecs_composex/elbv2/elbv2_stack/__init__.py +++ b/ecs_composex/elbv2/elbv2_stack/__init__.py @@ -55,6 +55,8 @@ def __init__( self.is_void = False super().__init__(title, stack_template, stack_parameters=lb_input, **kwargs) + for resource in module.resources_list: + resource.stack = self if not hasattr(self, "DeletionPolicy"): setattr(self, "DeletionPolicy", module.module_deletion_policy) @@ -76,5 +78,4 @@ def __init__( settings.mappings[module.mapping_key].update( {resource.logical_name: resource.mappings} ) - for resource in module.resources_list: - resource.stack = self + resource.sort_alb_ingress(settings, stack_template) diff --git a/ecs_composex/elbv2/elbv2_stack/elbv2.py b/ecs_composex/elbv2/elbv2_stack/elbv2.py index cff6b723..bf370d5e 100644 --- a/ecs_composex/elbv2/elbv2_stack/elbv2.py +++ b/ecs_composex/elbv2/elbv2_stack/elbv2.py @@ -22,7 +22,7 @@ from ecs_composex.common import NONALPHANUM from ecs_composex.common.logging import LOG -from ecs_composex.common.troposphere_tools import ROOT_STACK_NAME +from ecs_composex.common.troposphere_tools import ROOT_STACK_NAME, add_parameters from ecs_composex.compose.x_resources.network_x_resources import NetworkXResource from ecs_composex.elbv2.elbv2_ecs import MergedTargetGroup from ecs_composex.elbv2.elbv2_params import ( @@ -254,11 +254,8 @@ def sort_alb_ingress(self, settings, stack_template): """ Method to handle Ingress to ALB """ - if ( - not self.parameters - or (self.parameters and not keyisset("Ingress", self.parameters)) - or self.is_nlb() - ): + print(self, self.is_alb(), self.attributes_outputs, self.parameters) + if self.is_nlb(): LOG.warning( "You defined ingress rules for a NLB. This is invalid. Define ingress rules at the service level." ) @@ -272,12 +269,37 @@ def sort_alb_ingress(self, settings, stack_template): ports = set_service_ports(ports) self.ingress = Ingress(self.parameters["Ingress"], ports) if self.ingress and self.is_alb(): - self.ingress.set_aws_sources_ingress( - settings, self.logical_name, GetAtt(self.lb_sg, "GroupId") - ) - self.ingress.set_ext_sources_ingress( - self.logical_name, GetAtt(self.lb_sg, "GroupId") - ) + if self.cfn_resource: + self.ingress.set_aws_sources_ingress( + settings, self.logical_name, GetAtt(self.lb_sg, "GroupId") + ) + self.ingress.set_ext_sources_ingress( + self.logical_name, GetAtt(self.lb_sg, "GroupId") + ) + else: + from ecs_composex.elbv2.elbv2_params import LB_SG_ID + + print("LOOKUP ELBV2") + add_parameters( + stack_template, + [self.attributes_outputs[LB_SG_ID]["ImportParameter"]], + ) + self.stack.Parameters.update( + { + self.attributes_outputs[LB_SG_ID][ + "ImportParameter" + ].title: self.attributes_outputs[LB_SG_ID]["ImportValue"] + } + ) + self.ingress.set_aws_sources_ingress( + settings, + self.logical_name, + Ref(self.attributes_outputs[LB_SG_ID]["ImportParameter"]), + ) + self.ingress.set_ext_sources_ingress( + self.logical_name, + Ref(self.attributes_outputs[LB_SG_ID]["ImportParameter"]), + ) self.ingress.associate_aws_ingress_rules(stack_template) self.ingress.associate_ext_ingress_rules(stack_template) diff --git a/ecs_composex/mods_manager.py b/ecs_composex/mods_manager.py index cdf3258b..ca43c84f 100644 --- a/ecs_composex/mods_manager.py +++ b/ecs_composex/mods_manager.py @@ -143,8 +143,8 @@ def lookup_resources(self) -> list: f"{resource.module.res_key}.{resource.name} is set for Lookup" " but has other properties set. Voiding them" ) - resource.properties = {} - resource.parameters = {} + if resource.properties: + resource.properties = {} lookup_resources.append(resource) return lookup_resources