From 1d341bea20773d76d41a56e630d8b77beb01732c Mon Sep 17 00:00:00 2001 From: Lyndon Hook Date: Tue, 2 Jan 2018 11:22:11 +1100 Subject: [PATCH 1/2] Added support for AWSHelperFn props to be passed as dictionaries. --- tests/test_dict.py | 16 ++++++++++++++-- troposphere/__init__.py | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/test_dict.py b/tests/test_dict.py index d014be238..3738adc88 100644 --- a/tests/test_dict.py +++ b/tests/test_dict.py @@ -1,7 +1,8 @@ import unittest -from troposphere import Template, Ref +from troposphere import Template, Ref, Tags from troposphere import ecs from troposphere import iam +from troposphere import s3 class TestDict(unittest.TestCase): @@ -32,7 +33,14 @@ def setUp(self): "HostPort": 5001 } ], - "Links": ["containerA", "containerB"], + "Links": ["containerA", "containerB"] + } + + self.b = { + "BucketName": "bucketname", + "Tags": { + "tagname": "mytag" + } } def test_valid_data(self): @@ -74,6 +82,10 @@ def test_invalid_subproperty_definition(self): with self.assertRaises(ValueError): ecs.ContainerDefinition.from_dict("mycontainer", self.d) + def test_sub_property_aws_helper_fn(self): + bkt = s3.Bucket.from_dict("mybucket", self.b) + self.assertIsInstance(bkt.Tags, Tags) + if __name__ == '__main__': unittest.main() diff --git a/troposphere/__init__.py b/troposphere/__init__.py index cb4fb42e8..5abced7c2 100644 --- a/troposphere/__init__.py +++ b/troposphere/__init__.py @@ -50,6 +50,16 @@ def is_aws_object_subclass(cls): return is_aws_object +def is_aws_helper_function_subclass(cls): + is_aws_helper_function = False + try: + is_aws_helper_function = issubclass(cls, AWSHelperFn) + # prop_type isn't a class + except TypeError: + pass + return is_aws_helper_function + + def encode_to_dict(obj): if hasattr(obj, 'to_dict'): # Calling encode_to_dict to ensure object is @@ -257,13 +267,16 @@ def _from_dict(cls, title=None, **kwargs): prop_name)) prop_type = prop_attrs[0] value = kwargs[prop_name] - is_aws_object = is_aws_object_subclass(prop_type) - if is_aws_object: + + if is_aws_object_subclass(prop_type): if not isinstance(value, collections.Mapping): raise ValueError("Property definition for %s must be " "a Mapping type" % prop_name) value = prop_type._from_dict(**value) + if is_aws_helper_function_subclass(prop_type): + value = prop_type(**value) + if isinstance(prop_type, list): if not isinstance(value, list): raise TypeError("Attribute %s must be a " @@ -277,6 +290,8 @@ def _from_dict(cls, title=None, **kwargs): "Property definition for %s must be " "a list of Mapping types" % prop_name) new_v = prop_type[0]._from_dict(**v) + if is_aws_helper_function_subclass(prop_type[0]): + new_v = prop_type(**v) new_value.append(new_v) value = new_value props[prop_name] = value From b368c88a7f1cee1a2c1ca4612d5f03fbaf4d1c32 Mon Sep 17 00:00:00 2001 From: Lyndon Hook Date: Tue, 2 Jan 2018 15:07:46 +1100 Subject: [PATCH 2/2] Tigger new build --- tests/test_dict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dict.py b/tests/test_dict.py index 3738adc88..41c853249 100644 --- a/tests/test_dict.py +++ b/tests/test_dict.py @@ -1,5 +1,5 @@ import unittest -from troposphere import Template, Ref, Tags +from troposphere import Ref, Tags, Template from troposphere import ecs from troposphere import iam from troposphere import s3