Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ecs_composex/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -175,15 +176,15 @@ 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 {}
)
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 = []
Expand Down Expand Up @@ -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}")

Expand Down
65 changes: 31 additions & 34 deletions ecs_composex/common/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
22 changes: 11 additions & 11 deletions use-cases/blog.features.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# base file for services with the x-keys for BDD
version: '3.8'
version: "3.8"
secrets:
abcd: {}
john:
Expand All @@ -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
Expand Down Expand Up @@ -74,7 +74,7 @@ services:
- cloudwatch:PutMetricData
Effect: Allow
Resource:
- '*'
- "*"
Sid: AllowPublishMetricsToCw
x-xray: false
x-scaling:
Expand Down Expand Up @@ -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
Expand All @@ -113,7 +113,7 @@ services:
replicas: 2
resources:
reservations:
cpus: '0.1'
cpus: "0.1"
memory: 64000kB
environment:
LOGLEVEL: DEBUG
Expand Down Expand Up @@ -173,7 +173,7 @@ services:
testing: value
resources:
reservations:
cpus: '0.25'
cpus: "0.25"
memory: 134217728b
environment:
LOGLEVEL: DEBUG
Expand All @@ -199,7 +199,7 @@ services:
Name: ANYWHERE

x-logging:
RetentionInDays: 30
RetentionInDays: 30
x-scaling:
Range: 1-10
rproxy:
Expand All @@ -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:
Expand All @@ -241,4 +241,4 @@ volumes:
normal-vol: {}

x-tags:
costcentre: lambda
validation: tagging-applied