|
12 | 12 | from ecs_composex.compose.compose_services import ComposeService |
13 | 13 | from ecs_composex.common.settings import ComposeXSettings |
14 | 14 | from ecs_composex.common.stacks import ComposeXStack |
| 15 | + from troposphere.ecs import PortMapping |
15 | 16 | from ecs_composex.ecs_ingress.ecs_ingress_stack import XStack as EcsIngressStack |
16 | 17 |
|
17 | 18 | from json import dumps |
@@ -318,6 +319,21 @@ def find_namespace( |
318 | 319 | return namespace.get_resource_attribute_value(parameter, family)[0] |
319 | 320 |
|
320 | 321 |
|
| 322 | +def find_port_mapping(port_name, family: ComposeFamily) -> PortMapping | None: |
| 323 | + """ |
| 324 | + Goes over all the services/containers of the task definition and over each PortMapping of the container. |
| 325 | + If the port name matches, we have found the PortMapping |
| 326 | + """ |
| 327 | + for service in family.ordered_services: |
| 328 | + port_mappings = getattr(service.container_definition, "PortMappings", []) |
| 329 | + if not port_mappings: |
| 330 | + continue |
| 331 | + for _port_mapping in port_mappings: |
| 332 | + if _port_mapping.Name == port_name: |
| 333 | + return _port_mapping |
| 334 | + return |
| 335 | + |
| 336 | + |
321 | 337 | def set_ecs_connect_from_macro( |
322 | 338 | family: ComposeFamily, |
323 | 339 | service: ComposeService, |
@@ -347,8 +363,19 @@ def set_ecs_connect_from_macro( |
347 | 363 | f"No port called {port_name} in family {family.name}", |
348 | 364 | [_port["name"] for _port in family.service_networking.ports], |
349 | 365 | ) |
350 | | - |
351 | 366 | dns_name = set_else_none("DnsName", connect_config, None) |
| 367 | + app_protocol = set_else_none("appProtocol", connect_config, NoValue) |
| 368 | + valid_protocols: list[str] = ["http", "http2", "grpc"] |
| 369 | + if ( |
| 370 | + not isinstance(app_protocol, str) and app_protocol not in valid_protocols |
| 371 | + ) and app_protocol is not NoValue: |
| 372 | + raise ValueError( |
| 373 | + "appProtocol must be one of", valid_protocols, "got", app_protocol |
| 374 | + ) |
| 375 | + if "appProtocol" not in the_port: |
| 376 | + the_port["appProtocol"] = app_protocol |
| 377 | + port_mapping: PortMapping = find_port_mapping(the_port["name"], family) |
| 378 | + setattr(port_mapping, "AppProtocol", app_protocol) |
352 | 379 | client_aliases = NoValue |
353 | 380 | if dns_name: |
354 | 381 | client_aliases = [ |
|
0 commit comments