From 497420ab0f22320396241862e763bae0137b2dd5 Mon Sep 17 00:00:00 2001 From: Mauro Torrez Date: Mon, 5 Oct 2020 22:28:31 -0300 Subject: [PATCH] feat: support adding nodes recursively --- wildfly.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/wildfly.py b/wildfly.py index 6d72b5e..3b29774 100644 --- a/wildfly.py +++ b/wildfly.py @@ -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