feat: support adding nodes recursively

This commit is contained in:
Mauro Torrez 2020-10-05 22:28:31 -03:00
parent 936faac069
commit 497420ab0f

View File

@ -53,7 +53,7 @@ class ActionModule(ActionBase):
try:
cur_state = json.loads(cur_result.get('stdout')).get('result')
except Exception as ex:
except json.JSONDecodeError as ex:
return merge_hash(cur_result, dict(
failed=True,
msg='Error parsing JSON from Wildfly CLI: {}'.format(str(ex))
@ -70,7 +70,7 @@ class ActionModule(ActionBase):
item.get('state', 'present'),
False)
except Exception as ex:
except WildflyError as ex:
return merge_hash(cur_result, dict(
failed=True,
msg='Error while generating Wildfly batch: {}'.format(
@ -175,12 +175,18 @@ def wildfly_batch(prev, root, attrs, state='present', wrap=True):
display.v("current config: {}".format(cur))
# consider dict attrs whose values are also dicts as sub-nodes
sub_nodes = [k for k in sorted(attrs) if (
isinstance(attrs[k], dict) and all(
[isinstance(attrs[k][v], dict) for v in attrs[k]]
)) ]
if state.lower() == 'present':
if cur:
# node exists, add missing attrs
for k in sorted(attrs):
if k not in cur or cur[k] != attrs[k]:
if k not in sub_nodes and (k not in cur or cur[k] != attrs[k]):
output = '{}{}:write-attribute(name={},value={})\n'.format(
output, root, k, format_attr(attrs[k]))
# update configuration tree
@ -191,10 +197,22 @@ def wildfly_batch(prev, root, attrs, state='present', wrap=True):
output = '{}{}:add({})\n'.format(
output, root,
','.join(['{}={}'.format(
k, format_attr(attrs[k])) for k in sorted(attrs)])
k, format_attr(attrs[k])) for k in sorted(attrs)
if k not in sub_nodes])
)
# config_add_node(prev, root, attrs)
# add sub-nodes recursively
for key in sub_nodes:
for val in attrs[key]:
output = output + wildfly_batch(
prev,
'{}/{}={}'.format(root, key, val),
attrs[key][val],
state,
False
)
if state.lower() == 'absent' and cur:
# remove existing node
output = '{}{}:remove()\n'.format(output, root)
@ -221,7 +239,7 @@ def config_query(config, path):
# either ptr is none (Type), or ptr[qry] not found (Key)
return None
except Exception as ex:
raise WildflyError("config query error: {}".format(str(ex)))
raise WildflyError("Error looking up config") from ex
return ptr