Skip to content

Commit

Permalink
Added tag validation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-variacode committed Oct 23, 2024
1 parent bafa421 commit 8503180
Showing 1 changed file with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.rundeck.plugins.ansible.ansible.InventoryList;
import com.rundeck.plugins.ansible.util.VaultPrompt;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.rundeck.app.spi.Services;
import org.rundeck.storage.api.PathUtil;
import org.rundeck.storage.api.StorageException;
Expand All @@ -47,7 +48,6 @@
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -56,8 +56,12 @@
import java.util.Properties;
import java.util.Set;

import static com.rundeck.plugins.ansible.ansible.InventoryList.*;
import static com.rundeck.plugins.ansible.ansible.InventoryList.ALL;
import static com.rundeck.plugins.ansible.ansible.InventoryList.CHILDREN;
import static com.rundeck.plugins.ansible.ansible.InventoryList.HOSTS;
import static com.rundeck.plugins.ansible.ansible.InventoryList.NodeTag;

@Slf4j
public class AnsibleResourceModelSource implements ResourceModelSource, ProxyRunnerPlugin {

public static final String HOST_TPL_J2 = "host-tpl.j2";
Expand Down Expand Up @@ -707,36 +711,43 @@ public void ansibleInventoryList(NodeSetImpl nodes, AnsibleRunner.AnsibleRunnerB
}

Map<String, Object> all = InventoryList.getValue(allInventory, ALL);
Map<String, Object> children = InventoryList.getValue(all, CHILDREN);

for (Map.Entry<String, Object> pair : children.entrySet()) {
String hostGroup = pair.getKey();
Map<String, Object> hostNames = InventoryList.getType(pair.getValue());
Map<String, Object> hosts = InventoryList.getValue(hostNames, HOSTS);

for (Map.Entry<String, Object> hostNode : hosts.entrySet()) {
NodeEntryImpl node = new NodeEntryImpl();
node.setTags(Set.of(hostGroup));
String hostName = hostNode.getKey();
node.setHostname(hostName);
node.setNodename(hostName);
Map<String, Object> 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());
}
});

nodes.putNode(node);
if (!isTagEmpty(all, ALL)) {
Map<String, Object> children = InventoryList.getValue(all, CHILDREN);

if (!isTagEmpty(children, CHILDREN)) {
for (Map.Entry<String, Object> pair : children.entrySet()) {
String hostGroup = pair.getKey();
Map<String, Object> hostNames = InventoryList.getType(pair.getValue());
Map<String, Object> hosts = InventoryList.getValue(hostNames, HOSTS);

if (!isTagEmpty(hosts, HOSTS)) {
for (Map.Entry<String, Object> hostNode : hosts.entrySet()) {
NodeEntryImpl node = new NodeEntryImpl();
node.setTags(Set.of(hostGroup));
String hostName = hostNode.getKey();
node.setHostname(hostName);
node.setNodename(hostName);
Map<String, Object> 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());
}
});

nodes.putNode(node);
}
}
}
}
}
}
Expand Down Expand Up @@ -840,4 +851,20 @@ public List<String> listSecretsPathResourceModel(Map<String, Object> configurati

}

/**
* Validates if a tag is empty.
*
* @param tagMap The map containing the tag content.
* @param tagName The name of the tag to validate.
* @return True if the tag is empty, false otherwise.
*/
private boolean isTagEmpty(Map<String, Object> tagMap, String tagName) {
if (tagMap == null) {
log.warn("Tag '{}' is empty!", tagName);
return true;
}
log.warn("Tag '{}' is NOT empty!", tagName);
return false;
}

}

0 comments on commit 8503180

Please sign in to comment.