Skip to content

Commit

Permalink
Allow AWSHelperFn for Tag properties (#2271)
Browse files Browse the repository at this point in the history
Instead of checking the property name, we check the type of the value.
This means that the Tags class gets checked, but other helper functions are allowed.
  • Loading branch information
benbridts authored Oct 2, 2024
1 parent a71c4e1 commit 7b2e63c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
47 changes: 46 additions & 1 deletion tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from troposphere import If, Sub, Tag, Tags
from troposphere import GetAtt, If, Sub, Tag, Tags
from troposphere.autoscaling import Tags as ASTags


Expand Down Expand Up @@ -88,6 +88,18 @@ def test_json_tags(self):
Tags={"foo": "bar"},
)

JobDefinition(
"myjobdef",
Type="container",
ContainerProperties=ContainerProperties(
Image="image",
Vcpus=2,
Memory=2000,
Command=["echo", "foobar"],
),
Tags=GetAtt("resource", "tags"),
)

with self.assertRaises(TypeError):
JobDefinition(
"myjobdef",
Expand All @@ -114,6 +126,39 @@ def test_json_tags(self):
Tags="string",
)

def test_object_tags(self):
from troposphere.dynamodb import KeySchema, Table

Table(
"mytable",
KeySchema=[KeySchema(KeyType="HASH", AttributeName="id")],
BillingMode="PAY_PER_REQUEST",
Tags=Tags(Name="Example"),
)

Table(
"mytable",
KeySchema=[KeySchema(KeyType="HASH", AttributeName="id")],
BillingMode="PAY_PER_REQUEST",
Tags=GetAtt("resource", "tags"),
)

with self.assertRaises(TypeError):
Table(
"mytable",
KeySchema=[KeySchema(KeyType="HASH", AttributeName="id")],
BillingMode="PAY_PER_REQUEST",
Tags={"Name": "Example"},
)

with self.assertRaises(TypeError):
Table(
"mytable",
KeySchema=[KeySchema(KeyType="HASH", AttributeName="id")],
BillingMode="PAY_PER_REQUEST",
Tags="string",
)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def __setattr__(self, name: str, value: Any) -> None:
# to deal with this that we'll come up with eventually
#
# Don't do this for Tags since we can validate the assigned type below
if isinstance(value, AWSHelperFn) and name != "Tags":
if isinstance(value, AWSHelperFn) and not isinstance(value, Tags):
return self.properties.__setitem__(name, value)

# If it's a function, call it...
Expand Down

0 comments on commit 7b2e63c

Please sign in to comment.