Skip to content

Commit bdff657

Browse files
committed
Add update_if_exist option to add_parameter
!77+ made `add_parameter` always update the parameter if already existing, but it may be better to allow to choose depending on the situation
1 parent 3033a36 commit bdff657

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/e3/aws/troposphere/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ def resources(self, stack: Stack) -> list[AWSObject | Construct]:
118118
# Add the parameter during template creation even if the S3 key may not
119119
# be known yet. This is useful if creating a stack from code, so that
120120
# the exported template contains the parameter
121-
stack.add_parameter(self.s3_key_parameter)
121+
stack.add_parameter(
122+
self.s3_key_parameter,
123+
update_if_exist=True,
124+
)
122125
# Adding the Output can be useful for Lambda function with versioning.
123126
# The exported value can be retrieved by the lambda without the need to update
124127
# the lambda version.
@@ -277,7 +280,7 @@ def add(self, element: AWSObject | Construct | Stack) -> Stack:
277280
# Special case to keep track of Assets and generate parameters
278281
# for the S3 keys
279282
if isinstance(construct, Asset):
280-
self.add_parameter(construct.s3_key_parameter)
283+
self.add_parameter(construct.s3_key_parameter, update_if_exist=True)
281284
self.assets[construct.name] = construct
282285

283286
constructs_to_objects.extend(construct.resources(stack=self))
@@ -296,16 +299,20 @@ def extend(self, elements: Iterable[AWSObject | Construct | Stack]) -> Stack:
296299

297300
return self
298301

299-
def add_parameter(self, parameter: Parameter | list[Parameter]) -> None:
302+
def add_parameter(
303+
self, parameter: Parameter | list[Parameter], update_if_exist: bool = False
304+
) -> None:
300305
"""Add parameters to stack template.
301306
302307
:param parameter: parameter to add to the template
308+
:param update_if_exist: update the parameter if already exists, avoiding
309+
the duplicate key exception
303310
"""
304311
if not isinstance(parameter, list):
305312
parameter = [parameter]
306313

307314
for param in parameter:
308-
if param.title in self.template.parameters:
315+
if update_if_exist and param.title in self.template.parameters:
309316
self.template.parameters[param.title] = param
310317
else:
311318
self.template.add_parameter(param)

tests/tests_e3_aws/troposphere/stack/stack_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ def test_add_parameters() -> None:
5959
assert stack.export()["Parameters"] == expected_template
6060

6161

62+
def test_update_parameter() -> None:
63+
"""Test updating an already existing parameter."""
64+
p = Parameter("Parameter", Description="My parameter", Type="String")
65+
stack = Stack("test-stack", "this is a test stack")
66+
stack.add_parameter(p)
67+
68+
# This one should fail because of the duplicate key
69+
with pytest.raises(match='duplicate key "Parameter" detected'):
70+
stack.add_parameter(p)
71+
72+
# This one should update the parameter
73+
p.Description = "Updated parameter"
74+
stack.add_parameter(p, update_if_exist=True)
75+
76+
assert stack.export()["Parameters"] == {
77+
"Parameter": {
78+
"Description": "Updated parameter",
79+
"Type": "String",
80+
},
81+
}
82+
83+
6284
def test_add_outputs() -> None:
6385
"""Test adding outputs to a stack."""
6486
stack = Stack("test-stack", "this is a test stack")

0 commit comments

Comments
 (0)