From 9c90b19c85cfa674df0f1e8fe01b4282b6998071 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 21 Feb 2026 08:51:18 +0000 Subject: [PATCH] No fix, just nicer code --- ecs_composex/common/settings.py | 9 +++-- ecs_composex/common/tagging.py | 65 ++++++++++++++++----------------- use-cases/blog.features.yml | 22 +++++------ 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/ecs_composex/common/settings.py b/ecs_composex/common/settings.py index 6c94ebdd..173517bb 100644 --- a/ecs_composex/common/settings.py +++ b/ecs_composex/common/settings.py @@ -24,7 +24,8 @@ from yaml import Dumper as Dumper from yaml import Loader as Loader except ImportError: - from yaml import CDumper as Dumper, CLoader as Loader + from yaml import CDumper as Dumper + from yaml import CLoader as Loader from botocore.exceptions import ClientError from cfn_flip.yaml_dumper import LongCleanDumper @@ -175,7 +176,7 @@ def __init__( self.upload = False if self.no_upload else True self.parse_command(kwargs, content) - self.compose_content = {} + self.compose_content: dict = {} self.original_content: dict = {} self.input_file = ( kwargs[self.input_file_arg] if keyisset(self.input_file_arg, kwargs) else {} @@ -183,7 +184,7 @@ def __init__( self.set_content(kwargs, content) self.set_output_settings(kwargs) self.evaluate_private_namespace() - self.name = kwargs[self.name_arg] + self.name = kwargs.get(self.name_arg) self._ecs_cluster = None self.ignore_ecr_findings = keyisset(self.ecr_arg, kwargs) self.x_resources_void = [] @@ -530,7 +531,7 @@ def set_content(self, kwargs, content=None, fully_load=True): LOG.debug(f"Input files: {files}") content_def = ComposeDefinition(files, content) self.original_content = content_def.definition - self.compose_content = deepcopy(content_def.definition) + self.compose_content: dict = deepcopy(content_def.definition) source = str(pkg_files("ecs_composex").joinpath("specs/compose-spec.json")) LOG.debug(f"Validating against input schema {source}") diff --git a/ecs_composex/common/tagging.py b/ecs_composex/common/tagging.py index b18c8e20..4a8a96fb 100644 --- a/ecs_composex/common/tagging.py +++ b/ecs_composex/common/tagging.py @@ -20,6 +20,16 @@ """ +from __future__ import annotations + +from imaplib import Commands +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from troposphere import Template + + from ecs_composex.common.settings import ComposeXSettings + import copy from compose_x_common.compose_x_common import keyisset @@ -37,26 +47,16 @@ from ecs_composex.common.troposphere_tools import add_parameters -def define_tag_parameter_title(tag_name): +def define_tag_parameter_title(tag_name: str) -> str: """ Returns the formatted name title for a given tag - - :param tag_name: name of the tag as defined in the ComposeX file - :type tag_name: str - :return: reformatted tag name to work on CFN - :rtype: str """ return f"{NONALPHANUM.sub('', tag_name).strip().title()}Tag" -def define_extended_tags(tags): +def define_extended_tags(tags: list | dict) -> Tags | None: """ Function to generate the tags to be added to objects from x-tags - - :param tags: tags as defined in composex file - :type tags: list or dict - :return: Tags() or None - :rtype: troposphere.Tags or None """ rendered_tags = [] if isinstance(tags, list): @@ -72,12 +72,9 @@ def define_extended_tags(tags): return None -def generate_tags_parameters(tags): +def generate_tags_parameters(tags: list | dict) -> list[Parameter]: """ Function to generate a list of parameters used for the tags values - - :return: list of parameters and tags to add to objects - :rtype: tuple """ parameters = [] for tag in tags: @@ -100,14 +97,9 @@ def generate_tags_parameters(tags): return parameters -def expand_launch_template_tags_specs(lt, tags): +def expand_launch_template_tags_specs(lt: LaunchTemplate, tags: Tags) -> None: """ Function to expand the LaunchTemplate TagSpecifications with defined x-tags. - - :param lt: the LaunchTemplate object - :type: troposphere.ec2.LaunchTemplate - :param tags: the Tags as built from x-tags - :type tags: troposphere.Tags """ LOG.debug("Setting tags to LaunchTemplate") try: @@ -169,13 +161,15 @@ def add_object_tags(obj, tags): setattr(obj, "Tags", clean_tags) -def default_tags(): +def default_tags(settings: ComposeXSettings) -> Tags: """ Function to return default tags to set on resource :return: default compose-x tags :rtype: troposphere.Tags """ - return Tags(CreatedByComposeX=True, **{"compose-x:version": version}) + return Tags( + **{"compose-x::version": version, "compose-x::project-name": settings.name} + ) def apply_tags_to_resources(settings, resource, params, xtags): @@ -203,28 +197,31 @@ def apply_tags_to_resources(settings, resource, params, xtags): add_object_tags(stack_resource, xtags) -def add_all_tags(root_template, settings, params=None, xtags=None): +def add_all_tags( + root_template: Template, + settings: ComposeXSettings, + params: list = None, + xtags: Tags = None, +) -> None: """ Function to go through all stacks of a given template and update the template It will recursively render sub stacks defined. If there are no substacks, it will go over the resources of the template add the tags. - :param troposphere.Template root_template: the root template to iterate over the resources. - :param ecs_composex.common.settings.ComposeXSettings settings: Execution settings - :param list params: Parameters to add to template if any - :param troposphere.Tags xtags: List of Tags to add to the resources. """ if not params or not xtags: if not keyisset("x-tags", settings.compose_content): - xtags = default_tags() + xtags = default_tags(settings) params = None else: tags = settings.compose_content["x-tags"] params = generate_tags_parameters(tags) xtags = define_extended_tags(tags) - xtags += default_tags() + xtags += default_tags(settings) - resources = root_template.resources if root_template else [] - for resource_name in resources: - resource = resources[resource_name] + resources = root_template.resources if root_template else {} + for resource_name, resource in resources.items(): + LOG.debug( + "Applying tags to resource {}: {}".format(resource_name, type(resource)) + ) apply_tags_to_resources(settings, resource, params, xtags) diff --git a/use-cases/blog.features.yml b/use-cases/blog.features.yml index 32ecb3ab..bdadcfd1 100644 --- a/use-cases/blog.features.yml +++ b/use-cases/blog.features.yml @@ -1,6 +1,6 @@ --- # base file for services with the x-keys for BDD -version: '3.8' +version: "3.8" secrets: abcd: {} john: @@ -22,7 +22,7 @@ services: - net.ipv4.tcp_syncookies=1 cap_add: - ALL -# env_file: ./use-cases/env-files/dummy.env + # env_file: ./use-cases/env-files/dummy.env deploy: update_config: failure_action: rollback @@ -74,7 +74,7 @@ services: - cloudwatch:PutMetricData Effect: Allow Resource: - - '*' + - "*" Sid: AllowPublishMetricsToCw x-xray: false x-scaling: @@ -102,8 +102,8 @@ services: depends_on: - app01 - bignicefamily -# env_file: -# - ./use-cases/env-files/dummy.env + # env_file: + # - ./use-cases/env-files/dummy.env deploy: update_config: failure_action: pause @@ -113,7 +113,7 @@ services: replicas: 2 resources: reservations: - cpus: '0.1' + cpus: "0.1" memory: 64000kB environment: LOGLEVEL: DEBUG @@ -173,7 +173,7 @@ services: testing: value resources: reservations: - cpus: '0.25' + cpus: "0.25" memory: 134217728b environment: LOGLEVEL: DEBUG @@ -199,7 +199,7 @@ services: Name: ANYWHERE x-logging: - RetentionInDays: 30 + RetentionInDays: 30 x-scaling: Range: 1-10 rproxy: @@ -217,10 +217,10 @@ services: replicas: 1 resources: limits: - cpus: '0.25' + cpus: "0.25" memory: 64M reservations: - cpus: '0.1' + cpus: "0.1" memory: 32M image: nginx volumes: @@ -241,4 +241,4 @@ volumes: normal-vol: {} x-tags: - costcentre: lambda + validation: tagging-applied