2014-10-02 17:07:05 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-10-03 18:53:28 +00:00
|
|
|
#from ansible.cmmon.errors import AnsibleError
|
|
|
|
#from playbook.tag import Tag
|
2014-10-03 20:37:32 +00:00
|
|
|
from ansible.playbook.attribute import Attribute, FieldAttribute
|
2014-10-03 12:08:03 +00:00
|
|
|
|
2014-10-04 13:48:25 +00:00
|
|
|
|
|
|
|
# general concept
|
|
|
|
# FooObject.load(datastructure) -> Foo
|
|
|
|
# FooObject._load_field # optional
|
|
|
|
# FooObject._validate_field # optional
|
|
|
|
# FooObject._post_validate_field # optional
|
|
|
|
# FooObject.evaluate(host_context) -> FooObject ? (calls post_validators, templates all members)
|
|
|
|
# question - are there some things that need to be evaluated *before* host context, i.e. globally?
|
|
|
|
# most things should be templated but want to provide as much early checking as possible
|
|
|
|
# TODO: also check for fields in datastructure that are not valid
|
|
|
|
# TODO: PluginAttribute(type) allows all the valid plugins as valid types of names
|
|
|
|
# lookupPlugins start with "with_", ModulePluginAttribute allows any key
|
|
|
|
|
2014-10-02 17:29:24 +00:00
|
|
|
class Base(object):
|
2014-10-02 17:07:05 +00:00
|
|
|
|
2014-10-03 20:37:32 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._data = dict()
|
|
|
|
self._attributes = dict()
|
2014-10-03 21:08:52 +00:00
|
|
|
|
|
|
|
for name in self.__class__.__dict__:
|
|
|
|
aname = name[1:]
|
|
|
|
if isinstance(aname, Attribute) and not isinstance(aname, FieldAttribute):
|
|
|
|
self._attributes[aname] = None
|
2014-10-03 12:08:03 +00:00
|
|
|
|
2014-10-03 20:37:32 +00:00
|
|
|
def load_data(self, ds):
|
|
|
|
''' walk the input datastructure and assign any values '''
|
2014-10-03 18:53:28 +00:00
|
|
|
|
2014-10-03 20:37:32 +00:00
|
|
|
assert ds is not None
|
|
|
|
|
2014-10-03 21:08:52 +00:00
|
|
|
for (name, attribute) in self.__class__.__dict__.iteritems():
|
|
|
|
aname = name[1:]
|
2014-10-03 20:37:32 +00:00
|
|
|
|
2014-10-03 21:08:52 +00:00
|
|
|
# process Fields
|
2014-10-03 20:37:32 +00:00
|
|
|
if isinstance(attribute, FieldAttribute):
|
2014-10-03 21:08:52 +00:00
|
|
|
method = getattr(self, '_load_%s' % aname, None)
|
2014-10-03 20:37:32 +00:00
|
|
|
if method:
|
2014-10-03 21:08:52 +00:00
|
|
|
self._attributes[aname] = method(self, attribute)
|
2014-10-03 20:37:32 +00:00
|
|
|
else:
|
2014-10-03 21:08:52 +00:00
|
|
|
if aname in ds:
|
|
|
|
self._attributes[aname] = ds[aname]
|
2014-10-03 20:37:32 +00:00
|
|
|
|
2014-10-03 21:08:52 +00:00
|
|
|
# TODO: implement PluginAtrribute which allows "with_" and "action" aliases.
|
2014-10-03 20:37:32 +00:00
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2014-10-03 18:53:28 +00:00
|
|
|
def validate(self):
|
2014-10-03 21:08:52 +00:00
|
|
|
# TODO: finish
|
2014-10-03 20:37:32 +00:00
|
|
|
for name in self.__dict__:
|
2014-10-03 21:08:52 +00:00
|
|
|
aname = name[1:]
|
|
|
|
attribute = self.__dict__[aname]
|
2014-10-03 20:37:32 +00:00
|
|
|
if instanceof(attribute, FieldAttribute):
|
2014-10-03 21:08:52 +00:00
|
|
|
method = getattr(self, '_validate_%s' % (prefix, aname), None)
|
2014-10-03 20:37:32 +00:00
|
|
|
if method:
|
|
|
|
method(self, attribute)
|
|
|
|
|
|
|
|
def post_validate(self, runner_context):
|
2014-10-03 21:08:52 +00:00
|
|
|
# TODO: finish
|
2014-10-03 20:37:32 +00:00
|
|
|
raise exception.NotImplementedError
|
2014-10-03 19:25:21 +00:00
|
|
|
|
2014-10-03 21:08:52 +00:00
|
|
|
def __getattr__(self, needle):
|
|
|
|
if needle in self._attributes:
|
|
|
|
return self._attributes[needle]
|
|
|
|
if needle in self.__dict__:
|
|
|
|
return self.__dict__[needle]
|
|
|
|
raise AttributeError
|
|
|
|
|
|
|
|
#def __setattr__(self, needle, value):
|
|
|
|
# if needle in self._attributes:
|
|
|
|
# self._attributes[needle] = value
|
|
|
|
# if needle in self.__dict__:
|
|
|
|
# super(Base, self).__setattr__(needle, value)
|
|
|
|
# # self.__dict__[needle] = value
|
|
|
|
# raise AttributeError
|
|
|
|
|
|
|
|
|