Skip to content

Commit

Permalink
Added fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-variacode committed Nov 6, 2024
1 parent 2ee6fbc commit 5b70ead
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import static com.rundeck.plugins.ansible.ansible.InventoryList.ALL;
import static com.rundeck.plugins.ansible.ansible.InventoryList.CHILDREN;
Expand Down Expand Up @@ -815,18 +816,34 @@ public void applyNodeTags(NodeEntryImpl node, Map<String, Object> nodeValues) th
InventoryList.tagHandle(NodeTag.DESCRIPTION, node, nodeValues);
}

/**
* Adds a node to the ansibleNodes map, merging tags if the node already exists.
*
* @param node The node to add.
* @param tags The tags to associate with the node.
*/
public void addNode(NodeEntryImpl node, Set<String> tags) {
if (ansibleNodes.containsKey(node.getNodename())) {
NodeEntryImpl oldNode = ansibleNodes.get(node.getNodename());
Set<String> currentTags = getStringTags(oldNode);
currentTags.addAll(tags);
oldNode.setTags(Set.copyOf(currentTags));
} else {
node.setTags(Set.copyOf(tags));
ansibleNodes.put(node.getNodename(), node);
}
ansibleNodes.compute(node.getNodename(), (key, existingNode) -> {
if (existingNode != null) {
// Node exists, merge tags efficiently
Set<String> mergedTags = new HashSet<>(getStringTags(existingNode)); // Start with existing tags
mergedTags.addAll(tags); // Add the new tags
existingNode.setTags(Set.copyOf(mergedTags)); // Update the existing node
return existingNode; // Return the updated node
} else {
// Node doesn't exist, add it with the given tags
node.setTags(Set.copyOf(tags));
return node; // Return the new node
}
});
}

/**
* Retrieves the tags from a node and converts them to strings.
*
* @param node The node whose tags are to be retrieved.
* @return A set of strings representing the node's tags. Returns an empty set if the node has no tags.
*/
public Set<String> getStringTags(NodeEntryImpl node) {
Set<String> tags = new HashSet<>();
for (Object tag : node.getTags()) {
Expand Down

0 comments on commit 5b70ead

Please sign in to comment.