From 1927d4852a9fa2e094d55bff7a73733610d7026a Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 4 Sep 2013 22:11:08 +0200 Subject: [PATCH 1/3] add tests for configurable default values for parameters Signed-off-by: Steven Armstrong --- cdist/test/cdist_type/__init__.py | 7 +++++++ .../parameter/default/optional1 | 1 + .../__with_parameter_defaults/parameter/optional | 2 ++ cdist/test/emulator/__init__.py | 15 +++++++++++++++ .../parameter/default/optional1 | 1 + .../type/__argument_defaults/parameter/optional | 2 ++ 6 files changed, 28 insertions(+) create mode 100644 cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/default/optional1 create mode 100644 cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/optional create mode 100644 cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/default/optional1 create mode 100644 cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/optional diff --git a/cdist/test/cdist_type/__init__.py b/cdist/test/cdist_type/__init__.py index e97ce7e6..79f824d3 100644 --- a/cdist/test/cdist_type/__init__.py +++ b/cdist/test/cdist_type/__init__.py @@ -146,3 +146,10 @@ class TypeTestCase(test.CdistTestCase): cdist_type = core.CdistType(base_path, '__without_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') + diff --git a/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/default/optional1 b/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/default/optional1 new file mode 100644 index 00000000..ef208405 --- /dev/null +++ b/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/default/optional1 @@ -0,0 +1 @@ +value1 diff --git a/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/optional b/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/optional new file mode 100644 index 00000000..8174d2a9 --- /dev/null +++ b/cdist/test/cdist_type/fixtures/__with_parameter_defaults/parameter/optional @@ -0,0 +1,2 @@ +optional1 +optional2 diff --git a/cdist/test/emulator/__init__.py b/cdist/test/emulator/__init__.py index 9f6d7000..8c2dcd72 100644 --- a/cdist/test/emulator/__init__.py +++ b/cdist/test/emulator/__init__.py @@ -219,6 +219,21 @@ class ArgumentsTestCase(test.CdistTestCase): self.assertFalse('optional2' in cdist_object.parameters) 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): diff --git a/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/default/optional1 b/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/default/optional1 new file mode 100644 index 00000000..ef208405 --- /dev/null +++ b/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/default/optional1 @@ -0,0 +1 @@ +value1 diff --git a/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/optional b/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/optional new file mode 100644 index 00000000..8174d2a9 --- /dev/null +++ b/cdist/test/emulator/fixtures/conf/type/__argument_defaults/parameter/optional @@ -0,0 +1,2 @@ +optional1 +optional2 From fc40a40ae0b4bb884f50443187ec2852b1766553 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 4 Sep 2013 22:11:42 +0200 Subject: [PATCH 2/3] implement configurable default values for parameters Signed-off-by: Steven Armstrong --- cdist/core/cdist_type.py | 17 +++++++++++++++++ cdist/emulator.py | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index b6ba4f00..46e126f9 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -62,6 +62,7 @@ class CdistType(object): self.__optional_parameters = None self.__optional_multiple_parameters = None self.__boolean_parameters = None + self.__parameter_defaults = None @classmethod def list_types(cls, base_path): @@ -190,3 +191,19 @@ class CdistType(object): finally: self.__boolean_parameters = 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 diff --git a/cdist/emulator.py b/cdist/emulator.py index f1f4b622..b1cd8f2d 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -96,10 +96,12 @@ class Emulator(object): parser.add_argument(argument, dest=parameter, action='append', required=True) for parameter in self.cdist_type.optional_parameters: 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: 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: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='store_const', const='') From 9358efd273fdbe84814976215acbb0b4c2ec453c Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Wed, 4 Sep 2013 22:23:01 +0200 Subject: [PATCH 3/3] document new feature: configurable default values for parameters Signed-off-by: Steven Armstrong --- docs/man/cdist-reference.text.sh | 7 ++++++- docs/man/man7/cdist-type.text | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/man/cdist-reference.text.sh b/docs/man/cdist-reference.text.sh index b0a9d32c..b41be801 100755 --- a/docs/man/cdist-reference.text.sh +++ b/docs/man/cdist-reference.text.sh @@ -116,8 +116,13 @@ confdir/type//parameter/required:: confdir/type//parameter/optional:: Parameters optionally accepted by type, \n seperated list. +confdir/type//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//parameter/default/foo. + confdir/type//parameter/boolean:: - Boolean parameters accepted by type, \n seperated list. + Boolean parameters accepted by type, \n seperated list. confdir/type//explorer:: Location of the type specific explorers. diff --git a/docs/man/man7/cdist-type.text b/docs/man/man7/cdist-type.text index cfb50414..8415f991 100644 --- a/docs/man/man7/cdist-type.text +++ b/docs/man/man7/cdist-type.text @@ -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 or no parameters at all. +Default values for optional parameters can be predefined in +***parameter/default/***. + Example: -------------------------------------------------------------------------------- echo servername >> cdist/conf/type/__nginx_vhost/parameter/required 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 use_ssl >> cdist/conf/type/__nginx_vhost/parameter/boolean -------------------------------------------------------------------------------- @@ -108,6 +114,9 @@ if [ -f "$__object/parameter/logdirectory" ]; then logdirectory="$(cat "$__object/parameter/logdirectory")" fi +# optional parameter with predefined default +loglevel="$(cat "$__object/parameter/loglevel")" + # boolean parameter if [ -f "$__object/parameter/use_ssl" ]; then # file exists -> True