From 7b2e63c7b096520ea4a8b1b549ed0e0bae7e6799 Mon Sep 17 00:00:00 2001 From: Ben Bridts Date: Wed, 2 Oct 2024 19:00:08 +0200 Subject: [PATCH] Allow AWSHelperFn for Tag properties (#2271) 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. --- tests/test_tags.py | 47 ++++++++++++++++++++++++++++++++++++++++- troposphere/__init__.py | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/tests/test_tags.py b/tests/test_tags.py index b3cb4ebfc..2ea6a65e2 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -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 @@ -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", @@ -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() diff --git a/troposphere/__init__.py b/troposphere/__init__.py index 73b48e01d..53c274c13 100644 --- a/troposphere/__init__.py +++ b/troposphere/__init__.py @@ -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...