From bb8d87ceb6d41a3e9d268ee14b8e91088cfa8219 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Sat, 27 Jun 2015 01:01:08 -0400 Subject: [PATCH] Allow field attributes which are lists to validate the type of the list items Starting to apply this for tags too, however it is not correcting things as would be expected. --- lib/ansible/playbook/attribute.py | 3 ++- lib/ansible/playbook/base.py | 4 ++++ lib/ansible/playbook/taggable.py | 4 +++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/ansible/playbook/attribute.py b/lib/ansible/playbook/attribute.py index 8a727a0193..b2e89c7733 100644 --- a/lib/ansible/playbook/attribute.py +++ b/lib/ansible/playbook/attribute.py @@ -21,12 +21,13 @@ __metaclass__ = type class Attribute: - def __init__(self, isa=None, private=False, default=None, required=False): + def __init__(self, isa=None, private=False, default=None, required=False, listof=None): self.isa = isa self.private = private self.default = default self.required = required + self.listof = listof class FieldAttribute(Attribute): pass diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index 2d931748eb..e33bedf3c8 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -274,6 +274,10 @@ class Base: elif attribute.isa == 'list': if not isinstance(value, list): value = [ value ] + if attribute.listof is not None: + for item in value: + if not isinstance(item, attribute.listof): + raise AnsibleParserError("the field '%s' should be a list of %s, but the item '%s' is a %s" % (name, attribute.listof, item, type(item)), obj=self.get_ds()) elif attribute.isa == 'dict' and not isinstance(value, dict): raise TypeError() diff --git a/lib/ansible/playbook/taggable.py b/lib/ansible/playbook/taggable.py index 40e05d1817..6ddd4b7439 100644 --- a/lib/ansible/playbook/taggable.py +++ b/lib/ansible/playbook/taggable.py @@ -19,6 +19,8 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from six import string_types + from ansible.errors import AnsibleError from ansible.playbook.attribute import FieldAttribute from ansible.template import Templar @@ -26,7 +28,7 @@ from ansible.template import Templar class Taggable: untagged = set(['untagged']) - _tags = FieldAttribute(isa='list', default=[]) + _tags = FieldAttribute(isa='list', default=[], listof=(string_types,int)) def __init__(self): super(Taggable, self).__init__()