Merge branch 'feature/default_parameters' of https://github.com/asteven/cdist

This commit is contained in:
Nico Schottelius 2013-09-05 13:44:32 +02:00
commit d0dfe8f834
10 changed files with 64 additions and 3 deletions

View file

@ -62,6 +62,7 @@ class CdistType(object):
self.__optional_parameters = None self.__optional_parameters = None
self.__optional_multiple_parameters = None self.__optional_multiple_parameters = None
self.__boolean_parameters = None self.__boolean_parameters = None
self.__parameter_defaults = None
@classmethod @classmethod
def list_types(cls, base_path): def list_types(cls, base_path):
@ -190,3 +191,19 @@ class CdistType(object):
finally: finally:
self.__boolean_parameters = parameters self.__boolean_parameters = parameters
return self.__boolean_parameters return self.__boolean_parameters
@property
def parameter_defaults(self):
if not self.__parameter_defaults:
defaults = {}
try:
defaults_dir = os.path.join(self.absolute_path, "parameter", "default")
for name in os.listdir(defaults_dir):
with open(os.path.join(defaults_dir, name)) as fd:
defaults[name] = fd.read().strip()
except EnvironmentError:
# error ignored
pass
finally:
self.__parameter_defaults = defaults
return self.__parameter_defaults

View file

@ -96,10 +96,12 @@ class Emulator(object):
parser.add_argument(argument, dest=parameter, action='append', required=True) parser.add_argument(argument, dest=parameter, action='append', required=True)
for parameter in self.cdist_type.optional_parameters: for parameter in self.cdist_type.optional_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, dest=parameter, action='store', required=False) parser.add_argument(argument, dest=parameter, action='store', required=False,
default=self.cdist_type.parameter_defaults.get(parameter, None))
for parameter in self.cdist_type.optional_multiple_parameters: for parameter in self.cdist_type.optional_multiple_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, dest=parameter, action='append', required=False) parser.add_argument(argument, dest=parameter, action='append', required=False,
default=self.cdist_type.parameter_defaults.get(parameter, None))
for parameter in self.cdist_type.boolean_parameters: for parameter in self.cdist_type.boolean_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, dest=parameter, action='store_const', const='') parser.add_argument(argument, dest=parameter, action='store_const', const='')

View file

@ -146,3 +146,10 @@ class TypeTestCase(test.CdistTestCase):
cdist_type = core.CdistType(base_path, '__without_boolean_parameters') cdist_type = core.CdistType(base_path, '__without_boolean_parameters')
self.assertEqual(cdist_type.boolean_parameters, []) self.assertEqual(cdist_type.boolean_parameters, [])
def test_with_parameter_defaults(self):
base_path = fixtures
cdist_type = core.CdistType(base_path, '__with_parameter_defaults')
self.assertTrue('optional1' in cdist_type.parameter_defaults)
self.assertFalse('optional2' in cdist_type.parameter_defaults)
self.assertEqual(cdist_type.parameter_defaults['optional1'], 'value1')

View file

@ -0,0 +1,2 @@
optional1
optional2

View file

@ -219,6 +219,21 @@ class ArgumentsTestCase(test.CdistTestCase):
self.assertFalse('optional2' in cdist_object.parameters) self.assertFalse('optional2' in cdist_object.parameters)
self.assertEqual(cdist_object.parameters['optional1'], value) self.assertEqual(cdist_object.parameters['optional1'], value)
def test_argument_defaults(self):
type_name = '__argument_defaults'
object_id = 'some-id'
value = 'value1'
argv = [type_name, object_id]
os.environ.update(self.env)
emu = emulator.Emulator(argv)
emu.run()
cdist_type = core.CdistType(self.local.type_path, type_name)
cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id)
self.assertTrue('optional1' in cdist_object.parameters)
self.assertFalse('optional2' in cdist_object.parameters)
self.assertEqual(cdist_object.parameters['optional1'], value)
class StdinTestCase(test.CdistTestCase): class StdinTestCase(test.CdistTestCase):

View file

@ -0,0 +1,2 @@
optional1
optional2

View file

@ -116,8 +116,13 @@ confdir/type/<name>/parameter/required::
confdir/type/<name>/parameter/optional:: confdir/type/<name>/parameter/optional::
Parameters optionally accepted by type, \n seperated list. Parameters optionally accepted by type, \n seperated list.
confdir/type/<name>/parameter/default/*::
Default values for optional parameters.
Assuming an optional parameter name of 'foo', it's default value would
be read from the file confdir/type/<name>/parameter/default/foo.
confdir/type/<name>/parameter/boolean:: confdir/type/<name>/parameter/boolean::
Boolean parameters accepted by type, \n seperated list. Boolean parameters accepted by type, \n seperated list.
confdir/type/<name>/explorer:: confdir/type/<name>/explorer::
Location of the type specific explorers. Location of the type specific explorers.

View file

@ -82,10 +82,16 @@ follow the standard unix behaviour "the last given wins".
If either is missing, the type will have no required, no optional, no boolean If either is missing, the type will have no required, no optional, no boolean
or no parameters at all. or no parameters at all.
Default values for optional parameters can be predefined in
***parameter/default/<name>***.
Example: Example:
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
echo servername >> cdist/conf/type/__nginx_vhost/parameter/required echo servername >> cdist/conf/type/__nginx_vhost/parameter/required
echo logdirectory >> cdist/conf/type/__nginx_vhost/parameter/optional echo logdirectory >> cdist/conf/type/__nginx_vhost/parameter/optional
echo loglevel >> cdist/conf/type/__nginx_vhost/parameter/optional
mkdir cdist/conf/type/__nginx_vhost/parameter/default
echo warning > cdist/conf/type/__nginx_vhost/parameter/default/loglevel
echo server_alias >> cdist/conf/type/__nginx_vhost/parameter/optional_multiple echo server_alias >> cdist/conf/type/__nginx_vhost/parameter/optional_multiple
echo use_ssl >> cdist/conf/type/__nginx_vhost/parameter/boolean echo use_ssl >> cdist/conf/type/__nginx_vhost/parameter/boolean
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -108,6 +114,9 @@ if [ -f "$__object/parameter/logdirectory" ]; then
logdirectory="$(cat "$__object/parameter/logdirectory")" logdirectory="$(cat "$__object/parameter/logdirectory")"
fi fi
# optional parameter with predefined default
loglevel="$(cat "$__object/parameter/loglevel")"
# boolean parameter # boolean parameter
if [ -f "$__object/parameter/use_ssl" ]; then if [ -f "$__object/parameter/use_ssl" ]; then
# file exists -> True # file exists -> True