package com.batix.rundeck.core;

import java.util.HashMap;
import java.util.Map;

public class AnsibleInventory {
  
  public class AnsibleInventoryHosts {
    
    protected Map<String, Map<String, String>> hosts = new HashMap<String, Map<String, String>>();
    protected Map<String, AnsibleInventoryHosts> children = new HashMap<String, AnsibleInventoryHosts>();
  
    public AnsibleInventoryHosts addHost(String nodeName) {
      hosts.put(nodeName, new HashMap<String, String>());
      return this;
    }

    public AnsibleInventoryHosts addHost(String nodeName, String host, Map<String, String> attributes) {
      attributes.put("ansible_host", host);
      hosts.put(nodeName, attributes);
      return this;
    }

    public AnsibleInventoryHosts getOrAddChildHostGroup(String groupName) {
      children.putIfAbsent(groupName, new AnsibleInventoryHosts());
      return children.get(groupName);
    }
  }

  protected AnsibleInventoryHosts all = new AnsibleInventoryHosts();

  public AnsibleInventory addHost(String nodeName, String host, Map<String, String> attributes) {
    // Remove attributes that are reserved in Ansible
    String[] reserved = { "hostvars", "group_names", "groups", "environment" };
    for (String r: reserved){
      attributes.remove(r);
    }
    all.addHost(nodeName, host, attributes);
    // Create Ansible groups by attribute
    // Group by osFamily is needed for windows hosts setup
    String[] attributeGroups = { "osFamily" };
    for (String g: attributeGroups) {
      if (attributes.containsKey(g)) {
        String groupName = attributes.get(g).toLowerCase();
        all.getOrAddChildHostGroup(groupName).addHost(nodeName);
      }
    }
    return this;
  }
}
@@ -1468,20 +1468,28 @@
 sFamily%22
+, %22tags%22
  %7D;%0A
-
     for 
@@ -1567,24 +1567,26 @@
       String
+%5B%5D
  groupName =
@@ -1583,16 +1583,17 @@
 roupName
+s
  = attri
@@ -1618,18 +1618,76 @@
 erCase()
-;%0A
+.split(%22,%22);%0A        for (String groupName: groupNames) %7B%0A  
         
@@ -1722,16 +1722,23 @@
 roupName
+.trim()
 ).addHos
@@ -1746,24 +1746,34 @@
 (nodeName);%0A
+        %7D%0A
       %7D%0A    

Add Host Groups from Tags to generated AnsibleInventory file #244