feat: support adding nodes recursively
This commit is contained in:
parent
936faac069
commit
497420ab0f
28
wildfly.py
28
wildfly.py
@ -53,7 +53,7 @@ class ActionModule(ActionBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
cur_state = json.loads(cur_result.get('stdout')).get('result')
|
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(
|
return merge_hash(cur_result, dict(
|
||||||
failed=True,
|
failed=True,
|
||||||
msg='Error parsing JSON from Wildfly CLI: {}'.format(str(ex))
|
msg='Error parsing JSON from Wildfly CLI: {}'.format(str(ex))
|
||||||
@ -70,7 +70,7 @@ class ActionModule(ActionBase):
|
|||||||
item.get('state', 'present'),
|
item.get('state', 'present'),
|
||||||
False)
|
False)
|
||||||
|
|
||||||
except Exception as ex:
|
except WildflyError as ex:
|
||||||
return merge_hash(cur_result, dict(
|
return merge_hash(cur_result, dict(
|
||||||
failed=True,
|
failed=True,
|
||||||
msg='Error while generating Wildfly batch: {}'.format(
|
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))
|
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 state.lower() == 'present':
|
||||||
|
|
||||||
if cur:
|
if cur:
|
||||||
# node exists, add missing attrs
|
# node exists, add missing attrs
|
||||||
for k in sorted(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 = '{}{}:write-attribute(name={},value={})\n'.format(
|
||||||
output, root, k, format_attr(attrs[k]))
|
output, root, k, format_attr(attrs[k]))
|
||||||
# update configuration tree
|
# update configuration tree
|
||||||
@ -191,10 +197,22 @@ def wildfly_batch(prev, root, attrs, state='present', wrap=True):
|
|||||||
output = '{}{}:add({})\n'.format(
|
output = '{}{}:add({})\n'.format(
|
||||||
output, root,
|
output, root,
|
||||||
','.join(['{}={}'.format(
|
','.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)
|
# 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:
|
if state.lower() == 'absent' and cur:
|
||||||
# remove existing node
|
# remove existing node
|
||||||
output = '{}{}:remove()\n'.format(output, root)
|
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)
|
# either ptr is none (Type), or ptr[qry] not found (Key)
|
||||||
return None
|
return None
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
raise WildflyError("config query error: {}".format(str(ex)))
|
raise WildflyError("Error looking up config") from ex
|
||||||
return ptr
|
return ptr
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user