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

Fixes the case where you have a list of non BaseAWSObjects #589

Merged
merged 1 commit into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ def setUp(self):
"ContainerPort": 5001,
"HostPort": 5001
}
]
],
"Links": ["containerA", "containerB"],
}

def test_valid_data(self):
t = Template()
cd = ecs.ContainerDefinition.from_dict("mycontainer", self.d)
self.assertEquals(cd.Links[0], "containerA")
td = ecs.TaskDefinition(
"taskdef",
ContainerDefinitions=[cd],
Expand Down
53 changes: 30 additions & 23 deletions troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
valid_names = re.compile(r'^[a-zA-Z0-9]+$')


def is_aws_object_subclass(cls):
is_aws_object = False
try:
is_aws_object = issubclass(cls, BaseAWSObject)
# prop_type isn't a class
except TypeError:
pass
return is_aws_object


class BaseAWSObject(object):
def __init__(self, title, template=None, **kwargs):
self.title = title
Expand Down Expand Up @@ -173,33 +183,30 @@ def _from_dict(cls, title=None, **kwargs):
"%s property." % (cls.__name__,
prop_name))
prop_type = prop_attrs[0]
if prop_name in kwargs:
value = kwargs[prop_name]
is_aws_object = False
try:
is_aws_object = issubclass(prop_type, BaseAWSObject)
# prop_type isn't a class
except TypeError:
pass
if is_aws_object:
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 isinstance(prop_type, list):
if not isinstance(value, list):
raise TypeError("Attribute %s must be a "
"list." % prop_name)
new_value = []
for v in value:
value = kwargs[prop_name]
is_aws_object = is_aws_object_subclass(prop_type)
if is_aws_object:
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 isinstance(prop_type, list):
if not isinstance(value, list):
raise TypeError("Attribute %s must be a "
"list." % prop_name)
new_value = []
for v in value:
new_v = v
if is_aws_object_subclass(prop_type[0]):
if not isinstance(v, collections.Mapping):
raise ValueError(
"Property definition for %s must be "
"a list of Mapping types" % prop_name)
new_value.append(prop_type[0]._from_dict(**v))
value = new_value
props[prop_name] = value
new_v = prop_type[0]._from_dict(**v)
new_value.append(new_v)
value = new_value
props[prop_name] = value
if title:
return cls(title, **props)
return cls(**props)
Expand Down