Skip to content

Commit

Permalink
Validate the type for Tags
Browse files Browse the repository at this point in the history
Prior to this change Tags (which is a subclass of AWSHelperFn)
would not do validation of the expected type and it prevented
validation of Json style Tags needing a dict.
  • Loading branch information
markpeek committed Sep 28, 2024
1 parent df6a618 commit 8321648
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tests/test_serverless.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from troposphere import ImportValue, Parameter, Ref, Sub, Tags, Template
from troposphere import ImportValue, Parameter, Ref, Sub, Template
from troposphere.s3 import Filter, Rules, S3Key
from troposphere.serverless import (
SERVERLESS_TRANSFORM,
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_tags(self):
Handler="index.handler",
Runtime="nodejs",
CodeUri="s3://bucket/handler.zip",
Tags=Tags({"Tag1": "TagValue1", "Tag2": "TagValue2"}),
Tags={"Tag1": "TagValue1", "Tag2": "TagValue2"},
)
t = Template()
t.add_resource(serverless_func)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,47 @@ def test_Formats(self):
with self.assertRaises(TypeError):
Tags({}, "key", "value")

def test_json_tags(self):
from troposphere.batch import ContainerProperties, JobDefinition

JobDefinition(
"myjobdef",
Type="container",
ContainerProperties=ContainerProperties(
Image="image",
Vcpus=2,
Memory=2000,
Command=["echo", "foobar"],
),
Tags={"foo": "bar"},
)

with self.assertRaises(TypeError):
JobDefinition(
"myjobdef",
Type="container",
ContainerProperties=ContainerProperties(
Image="image",
Vcpus=2,
Memory=2000,
Command=["echo", "foobar"],
),
Tags=Tags({"foo": "bar"}),
)

with self.assertRaises(TypeError):
JobDefinition(
"myjobdef",
Type="container",
ContainerProperties=ContainerProperties(
Image="image",
Vcpus=2,
Memory=2000,
Command=["echo", "foobar"],
),
Tags="string",
)


if __name__ == "__main__":
unittest.main()
6 changes: 4 additions & 2 deletions troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ def __setattr__(self, name: str, value: Any) -> None:
expected_type = self.props[name][0]

# If the value is a AWSHelperFn we can't do much validation
# we'll have to leave that to Amazon. Maybe there's another way
# we'll have to leave that to Amazon. Maybe there's another way
# to deal with this that we'll come up with eventually
if isinstance(value, AWSHelperFn):
#
# Don't do this for Tags since we can validate the assigned type below
if isinstance(value, AWSHelperFn) and name != "Tags":
return self.properties.__setitem__(name, value)

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

0 comments on commit 8321648

Please # to comment.