Skip to content

Commit 9ee84ca

Browse files
authored
x-tagging code updates & typing (#821)
1 parent 400fdfe commit 9ee84ca

3 files changed

Lines changed: 47 additions & 49 deletions

File tree

ecs_composex/common/settings.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from yaml import Dumper as Dumper
2525
from yaml import Loader as Loader
2626
except ImportError:
27-
from yaml import CDumper as Dumper, CLoader as Loader
27+
from yaml import CDumper as Dumper
28+
from yaml import CLoader as Loader
2829

2930
from botocore.exceptions import ClientError
3031
from cfn_flip.yaml_dumper import LongCleanDumper
@@ -175,15 +176,15 @@ def __init__(
175176

176177
self.upload = False if self.no_upload else True
177178
self.parse_command(kwargs, content)
178-
self.compose_content = {}
179+
self.compose_content: dict = {}
179180
self.original_content: dict = {}
180181
self.input_file = (
181182
kwargs[self.input_file_arg] if keyisset(self.input_file_arg, kwargs) else {}
182183
)
183184
self.set_content(kwargs, content)
184185
self.set_output_settings(kwargs)
185186
self.evaluate_private_namespace()
186-
self.name = kwargs[self.name_arg]
187+
self.name = kwargs.get(self.name_arg)
187188
self._ecs_cluster = None
188189
self.ignore_ecr_findings = keyisset(self.ecr_arg, kwargs)
189190
self.x_resources_void = []
@@ -530,7 +531,7 @@ def set_content(self, kwargs, content=None, fully_load=True):
530531
LOG.debug(f"Input files: {files}")
531532
content_def = ComposeDefinition(files, content)
532533
self.original_content = content_def.definition
533-
self.compose_content = deepcopy(content_def.definition)
534+
self.compose_content: dict = deepcopy(content_def.definition)
534535
source = str(pkg_files("ecs_composex").joinpath("specs/compose-spec.json"))
535536
LOG.debug(f"Validating against input schema {source}")
536537

ecs_composex/common/tagging.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
2121
"""
2222

23+
from __future__ import annotations
24+
25+
from imaplib import Commands
26+
from typing import TYPE_CHECKING
27+
28+
if TYPE_CHECKING:
29+
from troposphere import Template
30+
31+
from ecs_composex.common.settings import ComposeXSettings
32+
2333
import copy
2434

2535
from compose_x_common.compose_x_common import keyisset
@@ -37,26 +47,16 @@
3747
from ecs_composex.common.troposphere_tools import add_parameters
3848

3949

40-
def define_tag_parameter_title(tag_name):
50+
def define_tag_parameter_title(tag_name: str) -> str:
4151
"""
4252
Returns the formatted name title for a given tag
43-
44-
:param tag_name: name of the tag as defined in the ComposeX file
45-
:type tag_name: str
46-
:return: reformatted tag name to work on CFN
47-
:rtype: str
4853
"""
4954
return f"{NONALPHANUM.sub('', tag_name).strip().title()}Tag"
5055

5156

52-
def define_extended_tags(tags):
57+
def define_extended_tags(tags: list | dict) -> Tags | None:
5358
"""
5459
Function to generate the tags to be added to objects from x-tags
55-
56-
:param tags: tags as defined in composex file
57-
:type tags: list or dict
58-
:return: Tags() or None
59-
:rtype: troposphere.Tags or None
6060
"""
6161
rendered_tags = []
6262
if isinstance(tags, list):
@@ -72,12 +72,9 @@ def define_extended_tags(tags):
7272
return None
7373

7474

75-
def generate_tags_parameters(tags):
75+
def generate_tags_parameters(tags: list | dict) -> list[Parameter]:
7676
"""
7777
Function to generate a list of parameters used for the tags values
78-
79-
:return: list of parameters and tags to add to objects
80-
:rtype: tuple
8178
"""
8279
parameters = []
8380
for tag in tags:
@@ -100,14 +97,9 @@ def generate_tags_parameters(tags):
10097
return parameters
10198

10299

103-
def expand_launch_template_tags_specs(lt, tags):
100+
def expand_launch_template_tags_specs(lt: LaunchTemplate, tags: Tags) -> None:
104101
"""
105102
Function to expand the LaunchTemplate TagSpecifications with defined x-tags.
106-
107-
:param lt: the LaunchTemplate object
108-
:type: troposphere.ec2.LaunchTemplate
109-
:param tags: the Tags as built from x-tags
110-
:type tags: troposphere.Tags
111103
"""
112104
LOG.debug("Setting tags to LaunchTemplate")
113105
try:
@@ -169,13 +161,15 @@ def add_object_tags(obj, tags):
169161
setattr(obj, "Tags", clean_tags)
170162

171163

172-
def default_tags():
164+
def default_tags(settings: ComposeXSettings) -> Tags:
173165
"""
174166
Function to return default tags to set on resource
175167
:return: default compose-x tags
176168
:rtype: troposphere.Tags
177169
"""
178-
return Tags(CreatedByComposeX=True, **{"compose-x:version": version})
170+
return Tags(
171+
**{"compose-x::version": version, "compose-x::project-name": settings.name}
172+
)
179173

180174

181175
def apply_tags_to_resources(settings, resource, params, xtags):
@@ -203,28 +197,31 @@ def apply_tags_to_resources(settings, resource, params, xtags):
203197
add_object_tags(stack_resource, xtags)
204198

205199

206-
def add_all_tags(root_template, settings, params=None, xtags=None):
200+
def add_all_tags(
201+
root_template: Template,
202+
settings: ComposeXSettings,
203+
params: list = None,
204+
xtags: Tags = None,
205+
) -> None:
207206
"""
208207
Function to go through all stacks of a given template and update the template
209208
It will recursively render sub stacks defined.
210209
If there are no substacks, it will go over the resources of the template add the tags.
211210
212-
:param troposphere.Template root_template: the root template to iterate over the resources.
213-
:param ecs_composex.common.settings.ComposeXSettings settings: Execution settings
214-
:param list params: Parameters to add to template if any
215-
:param troposphere.Tags xtags: List of Tags to add to the resources.
216211
"""
217212
if not params or not xtags:
218213
if not keyisset("x-tags", settings.compose_content):
219-
xtags = default_tags()
214+
xtags = default_tags(settings)
220215
params = None
221216
else:
222217
tags = settings.compose_content["x-tags"]
223218
params = generate_tags_parameters(tags)
224219
xtags = define_extended_tags(tags)
225-
xtags += default_tags()
220+
xtags += default_tags(settings)
226221

227-
resources = root_template.resources if root_template else []
228-
for resource_name in resources:
229-
resource = resources[resource_name]
222+
resources = root_template.resources if root_template else {}
223+
for resource_name, resource in resources.items():
224+
LOG.debug(
225+
"Applying tags to resource {}: {}".format(resource_name, type(resource))
226+
)
230227
apply_tags_to_resources(settings, resource, params, xtags)

use-cases/blog.features.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
# base file for services with the x-keys for BDD
3-
version: '3.8'
3+
version: "3.8"
44
secrets:
55
abcd: {}
66
john:
@@ -22,7 +22,7 @@ services:
2222
- net.ipv4.tcp_syncookies=1
2323
cap_add:
2424
- ALL
25-
# env_file: ./use-cases/env-files/dummy.env
25+
# env_file: ./use-cases/env-files/dummy.env
2626
deploy:
2727
update_config:
2828
failure_action: rollback
@@ -74,7 +74,7 @@ services:
7474
- cloudwatch:PutMetricData
7575
Effect: Allow
7676
Resource:
77-
- '*'
77+
- "*"
7878
Sid: AllowPublishMetricsToCw
7979
x-xray: false
8080
x-scaling:
@@ -102,8 +102,8 @@ services:
102102
depends_on:
103103
- app01
104104
- bignicefamily
105-
# env_file:
106-
# - ./use-cases/env-files/dummy.env
105+
# env_file:
106+
# - ./use-cases/env-files/dummy.env
107107
deploy:
108108
update_config:
109109
failure_action: pause
@@ -113,7 +113,7 @@ services:
113113
replicas: 2
114114
resources:
115115
reservations:
116-
cpus: '0.1'
116+
cpus: "0.1"
117117
memory: 64000kB
118118
environment:
119119
LOGLEVEL: DEBUG
@@ -173,7 +173,7 @@ services:
173173
testing: value
174174
resources:
175175
reservations:
176-
cpus: '0.25'
176+
cpus: "0.25"
177177
memory: 134217728b
178178
environment:
179179
LOGLEVEL: DEBUG
@@ -199,7 +199,7 @@ services:
199199
Name: ANYWHERE
200200

201201
x-logging:
202-
RetentionInDays: 30
202+
RetentionInDays: 30
203203
x-scaling:
204204
Range: 1-10
205205
rproxy:
@@ -217,10 +217,10 @@ services:
217217
replicas: 1
218218
resources:
219219
limits:
220-
cpus: '0.25'
220+
cpus: "0.25"
221221
memory: 64M
222222
reservations:
223-
cpus: '0.1'
223+
cpus: "0.1"
224224
memory: 32M
225225
image: nginx
226226
volumes:
@@ -241,4 +241,4 @@ volumes:
241241
normal-vol: {}
242242

243243
x-tags:
244-
costcentre: lambda
244+
validation: tagging-applied

0 commit comments

Comments
 (0)