Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dictionary param support to resources that have AWSHelperFn props. #957

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest
from troposphere import Template, Ref
from troposphere import Ref, Tags, Template
from troposphere import ecs
from troposphere import iam
from troposphere import s3


class TestDict(unittest.TestCase):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
19 changes: 17 additions & 2 deletions troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand All @@ -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
Expand Down