Skip to content

Commit

Permalink
Ingress for LB from macroparameter
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPreston committed Jan 24, 2024
1 parent 7f1b53a commit b913ffe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
6 changes: 5 additions & 1 deletion ecs_composex/compose/x_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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])
Expand Down
5 changes: 3 additions & 2 deletions ecs_composex/elbv2/elbv2_stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
46 changes: 34 additions & 12 deletions ecs_composex/elbv2/elbv2_stack/elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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."
)
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions ecs_composex/mods_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b913ffe

Please sign in to comment.