Adding block code and tests

This commit is contained in:
James Cammarata 2014-10-16 12:08:33 -07:00
commit 57d2622c8c
4 changed files with 126 additions and 11 deletions

View file

@ -19,8 +19,6 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
#from ansible.common.errors import AnsibleError
class Attribute:
def __init__(self, isa=None, private=False, default=None):

View file

@ -65,11 +65,11 @@ class Base:
if isinstance(attribute, FieldAttribute):
# copy the value over unless a _load_field method is defined
method = getattr(self, '_load_%s' % aname, None)
if method:
self._attributes[aname] = method(self, attribute)
else:
if aname in ds:
if aname in ds:
method = getattr(self, '_load_%s' % aname, None)
if method:
self._attributes[aname] = method(aname, ds[aname])
else:
self._attributes[aname] = ds[aname]
# return the constructed object

View file

@ -19,10 +19,48 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from v2.playbook.base import PlaybookBase
from ansible.playbook.base import Base
from ansible.playbook.task import Task
from ansible.playbook.attribute import Attribute, FieldAttribute
class Block(PlaybookBase):
class Block(Base):
def __init__(self):
pass
_begin = FieldAttribute(isa='list')
_rescue = FieldAttribute(isa='list')
_end = FieldAttribute(isa='list')
_otherwise = FieldAttribute(isa='list')
def __init__(self, role=None):
self.role = role
super(Block, self).__init__()
def get_variables(self):
# blocks do not (currently) store any variables directly,
# so we just return an empty dict here
return dict()
@staticmethod
def load(data, role=None):
b = Block(role=role)
return b.load_data(data)
def _load_list_of_tasks(self, ds):
assert type(ds) == list
task_list = []
for task in ds:
t = Task.load(task)
task_list.append(t)
return task_list
def _load_begin(self, attr, ds):
return self._load_list_of_tasks(ds)
def _load_rescue(self, attr, ds):
return self._load_list_of_tasks(ds)
def _load_end(self, attr, ds):
return self._load_list_of_tasks(ds)
def _load_otherwise(self, attr, ds):
return self._load_list_of_tasks(ds)