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-15 23:22:54 +00:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-10-15 22:08:28 +00:00
|
|
|
from io import FileIO
|
|
|
|
|
|
|
|
from six import iteritems, string_types
|
|
|
|
|
2014-10-03 20:37:32 +00:00
|
|
|
from ansible.playbook.attribute import Attribute, FieldAttribute
|
2014-10-08 19:59:24 +00:00
|
|
|
from ansible.parsing import load as ds_load
|
2014-10-03 12:08:03 +00:00
|
|
|
|
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):
|
2014-10-08 19:59:24 +00:00
|
|
|
|
|
|
|
# each class knows attributes set upon it, see Task.py for example
|
2014-10-03 20:37:32 +00:00
|
|
|
self._attributes = dict()
|
2014-10-06 21:06:13 +00:00
|
|
|
|
2014-10-15 22:08:28 +00:00
|
|
|
for (name, value) in iteritems(self.__class__.__dict__):
|
2014-10-03 21:08:52 +00:00
|
|
|
aname = name[1:]
|
2014-10-06 21:06:13 +00:00
|
|
|
if isinstance(value, Attribute):
|
|
|
|
self._attributes[aname] = value.default
|
2014-10-03 12:08:03 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
def munge(self, ds):
|
|
|
|
''' infrequently used method to do some pre-processing of legacy terms '''
|
|
|
|
|
|
|
|
return ds
|
|
|
|
|
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-06 20:29:02 +00:00
|
|
|
|
2014-10-15 22:08:28 +00:00
|
|
|
if isinstance(ds, string_types) or isinstance(ds, FileIO):
|
2014-10-08 19:59:24 +00:00
|
|
|
ds = ds_load(ds)
|
|
|
|
|
2014-10-06 20:29:02 +00:00
|
|
|
# we currently don't do anything with private attributes but may
|
|
|
|
# later decide to filter them out of 'ds' here.
|
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
ds = self.munge(ds)
|
2014-10-03 20:37:32 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
# walk all attributes in the class
|
2014-10-15 22:08:28 +00:00
|
|
|
for (name, attribute) in iteritems(self.__class__.__dict__):
|
2014-10-03 21:08:52 +00:00
|
|
|
aname = name[1:]
|
2014-10-03 20:37:32 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
# process Field attributes which get loaded from the YAML
|
|
|
|
|
2014-10-03 20:37:32 +00:00
|
|
|
if isinstance(attribute, FieldAttribute):
|
2014-10-06 19:27:41 +00:00
|
|
|
|
|
|
|
# copy the value over unless a _load_field method is defined
|
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-08 19:59:24 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
# return the constructed object
|
|
|
|
self.validate()
|
2014-10-03 20:37:32 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
|
2014-10-03 18:53:28 +00:00
|
|
|
def validate(self):
|
2014-10-08 19:59:24 +00:00
|
|
|
''' validation that is done at parse time, not load time '''
|
2014-10-06 19:27:41 +00:00
|
|
|
|
|
|
|
# walk all fields in the object
|
2014-10-06 20:29:02 +00:00
|
|
|
for (name, attribute) in self.__dict__.iteritems():
|
2014-10-06 19:27:41 +00:00
|
|
|
|
|
|
|
# find any field attributes
|
|
|
|
if isinstance(attribute, FieldAttribute):
|
|
|
|
|
|
|
|
if not name.startswith("_"):
|
|
|
|
raise AnsibleError("FieldAttribute %s must start with _" % name)
|
2014-10-08 19:59:24 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
aname = name[1:]
|
2014-10-08 19:59:24 +00:00
|
|
|
|
2014-10-06 19:27:41 +00:00
|
|
|
# run validator only if present
|
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-06 19:27:41 +00:00
|
|
|
'''
|
|
|
|
we can't tell that everything is of the right type until we have
|
2014-10-08 19:59:24 +00:00
|
|
|
all the variables. Run basic types (from isa) as well as
|
2014-10-06 19:27:41 +00:00
|
|
|
any _post_validate_<foo> functions.
|
2014-10-08 19:59:24 +00:00
|
|
|
'''
|
2014-10-06 19:27:41 +00:00
|
|
|
|
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):
|
2014-10-06 19:27:41 +00:00
|
|
|
|
2014-10-06 21:06:13 +00:00
|
|
|
# return any attribute names as if they were real
|
|
|
|
# optionally allowing masking by accessors
|
|
|
|
|
|
|
|
if not needle.startswith("_"):
|
|
|
|
method = "get_%s" % needle
|
|
|
|
if method in self.__dict__:
|
|
|
|
return method(self)
|
|
|
|
|
2014-10-03 21:08:52 +00:00
|
|
|
if needle in self._attributes:
|
|
|
|
return self._attributes[needle]
|
|
|
|
|
2014-10-06 20:29:02 +00:00
|
|
|
raise AttributeError("attribute not found: %s" % needle)
|