Commit c4d03f7b authored by Hardik Juneja's avatar Hardik Juneja

slapos/recipe/slapconfiguration: Support key.subkey.subkey like parameters in...

slapos/recipe/slapconfiguration: Support key.subkey.subkey like parameters in slap partition parameters

Currently, slap partition parameters only support simple key value pair like parameters and json
to provide multi level keys. This patch allow user to give subkeys seprated by '.' while supporting
the existing ways.

For example:

An instance parameter like this

"zodb-zeo.tcpv4-port": 8888

will be available as:

slapparameter["zodb-zeo"]["tcpv4-port"]
parent 65c794dd
......@@ -43,6 +43,11 @@ logger = logging.getLogger("slapos")
class Recipe(object):
"""
Retrieve slap partition parameters and make them available in buildout section.
Slap partition parameters can be provided as normal keys-value pairs and if sub keys are
required, keys can be joined using ".".
For example:
"zodb-zeo.tcpv4-port": 8888 will be available as slapparameter["zodb-zeo"]["tcpv4-port"]
There are two sources of parameters. First is configuration file slapos.cfg and
derived information.
......@@ -267,7 +272,7 @@ class Recipe(object):
options[key] = value
# print out augmented options to see what we are passing
logger.debug(str(options))
return self._expandParameterDict(options, parameter_dict)
return self._expandParameterDict(options, unflatten_dict(parameter_dict))
def _expandParameterDict(self, options, parameter_dict):
options['configuration'] = parameter_dict
......@@ -295,6 +300,18 @@ class JsonDump(Recipe):
update = install
def unflatten_dict(parameter_dict):
"""Transform a flattened dict joined using "." into a multi dimentional dictionary"""
output = dict()
for key, value in parameter_dict.iteritems():
keys = key.split(".")
d = output
for subkey in keys[:-1]:
if subkey not in d:
d[subkey] = dict()
d = d[subkey]
d[keys[-1]] = value
return output
def flatten_dict(data, key_prefix=''):
"""Transform folded dict into one-level key-subkey-subsubkey dictionary."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment