From 96e399dfd90eaa10c546339f1216fd1f901f189f Mon Sep 17 00:00:00 2001 From: Alexander Abarca Date: Mon, 4 Nov 2024 18:29:41 -0300 Subject: [PATCH] Fixed problem processing sub children --- .../plugin/AnsibleResourceModelSource.java | 123 +++++++++++++----- 1 file changed, 91 insertions(+), 32 deletions(-) diff --git a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java index ea1afa8..aafd581 100644 --- a/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java +++ b/src/main/groovy/com/rundeck/plugins/ansible/plugin/AnsibleResourceModelSource.java @@ -48,6 +48,7 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -714,44 +715,102 @@ public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerB if (isTagMapValid(all, ALL)) { Map children = InventoryList.getValue(all, CHILDREN); + processChildren(nodes, children, new HashSet<>()); + } + } - if (isTagMapValid(children, CHILDREN)) { - for (Map.Entry pair : children.entrySet()) { - String hostGroup = pair.getKey(); - Map hostNames = InventoryList.getType(pair.getValue()); - Map hosts = InventoryList.getValue(hostNames, HOSTS); - - if (isTagMapValid(hosts, HOSTS)) { - for (Map.Entry hostNode : hosts.entrySet()) { - NodeEntryImpl node = new NodeEntryImpl(); - node.setTags(Set.of(hostGroup)); - String hostName = hostNode.getKey(); - node.setHostname(hostName); - node.setNodename(hostName); - Map nodeValues = InventoryList.getType(hostNode.getValue()); - - InventoryList.tagHandle(NodeTag.HOSTNAME, node, nodeValues); - InventoryList.tagHandle(NodeTag.USERNAME, node, nodeValues); - InventoryList.tagHandle(NodeTag.OS_FAMILY, node, nodeValues); - InventoryList.tagHandle(NodeTag.OS_NAME, node, nodeValues); - InventoryList.tagHandle(NodeTag.OS_ARCHITECTURE, node, nodeValues); - InventoryList.tagHandle(NodeTag.OS_VERSION, node, nodeValues); - InventoryList.tagHandle(NodeTag.DESCRIPTION, node, nodeValues); - - nodeValues.forEach((key, value) -> { - if (value != null) { - node.setAttribute(key, value.toString()); - } - }); + /** + * Processes the given set of nodes and populates the children map with the results. + * + * @param nodes the set of nodes to process + * @param children a map to be populated with the processed children nodes + * @param tags a set of tags to filter the nodes + * @throws ResourceModelSourceException if an error occurs while processing the nodes + */ + public void processChildren(NodeSetImpl nodes, Map children, HashSet tags) throws ResourceModelSourceException { + if (!isTagMapValid(children, CHILDREN)) { + return; + } - nodes.putNode(node); - } - } - } + for (Map.Entry pair : children.entrySet()) { + + String hostGroup = pair.getKey(); + tags.add(hostGroup); + Map hostNames = InventoryList.getType(pair.getValue()); + + if (hostNames.containsKey(CHILDREN)) { + Map subChildren = InventoryList.getValue(hostNames, CHILDREN); + processChildren(nodes, subChildren, tags); + break; } + + processHosts(nodes, hostNames, tags); + tags.clear(); + } + } + + /** + * Processes the hosts within the given host names map and adds them to the nodes set. + * + * @param nodes the set of nodes to populate + * @param hostNames the map containing host names and their attributes + * @param tags the set of tags to apply to the nodes + * @throws ResourceModelSourceException if an error occurs while processing the nodes + */ + public void processHosts(NodeSetImpl nodes, Map hostNames, HashSet tags) throws ResourceModelSourceException { + Map hosts = InventoryList.getValue(hostNames, HOSTS); + + if (!isTagMapValid(hosts, HOSTS)) { + return; + } + + for (Map.Entry hostNode : hosts.entrySet()) { + NodeEntryImpl node = createNodeEntry(hostNode, tags); + nodes.putNode(node); } } + /** + * Creates a NodeEntryImpl object from the given host node entry and tags. + * + * @param hostNode the entry containing the host name and its attributes + * @param tags the set of tags to apply to the node + * @return the created NodeEntryImpl object + */ + public NodeEntryImpl createNodeEntry(Map.Entry hostNode, HashSet tags) throws ResourceModelSourceException { + NodeEntryImpl node = new NodeEntryImpl(); + node.setTags(Set.copyOf(tags)); + String hostName = hostNode.getKey(); + node.setHostname(hostName); + node.setNodename(hostName); + Map nodeValues = InventoryList.getType(hostNode.getValue()); + + applyNodeTags(node, nodeValues); + nodeValues.forEach((key, value) -> { + if (value != null) { + node.setAttribute(key, value.toString()); + } + }); + + return node; + } + + /** + * Applies predefined tags to the given node based on the provided node values. + * + * @param node the node to which the tags will be applied + * @param nodeValues the map containing the node's attributes + */ + public void applyNodeTags(NodeEntryImpl node, Map nodeValues) throws ResourceModelSourceException { + InventoryList.tagHandle(NodeTag.HOSTNAME, node, nodeValues); + InventoryList.tagHandle(NodeTag.USERNAME, node, nodeValues); + InventoryList.tagHandle(NodeTag.OS_FAMILY, node, nodeValues); + InventoryList.tagHandle(NodeTag.OS_NAME, node, nodeValues); + InventoryList.tagHandle(NodeTag.OS_ARCHITECTURE, node, nodeValues); + InventoryList.tagHandle(NodeTag.OS_VERSION, node, nodeValues); + InventoryList.tagHandle(NodeTag.DESCRIPTION, node, nodeValues); + } + /** * Gets Ansible nodes from inventory * @return Ansible nodes