/**
* Convert an AWS EC2 Instance to a RunDeck INodeEntry based on the mapping input
*/
@SuppressWarnings("unchecked")
static INodeEntry instanceToNode(final Instance inst, final Properties mapping) throws GeneratorException {
final NodeEntryImpl node = new NodeEntryImpl();
//evaluate single settings.selector=tags/* mapping
if ("tags/*".equals(mapping.getProperty("attributes.selector"))) {
//iterate through instance tags and generate settings
for (final Tag tag : inst.getTags()) {
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
}
node.getAttributes().put(tag.getKey(), tag.getValue());
}
}
if (null != mapping.getProperty("tags.selector")) {
final String selector = mapping.getProperty("tags.selector");
final String value = applySelector(inst, selector, mapping.getProperty("tags.default"), true);
if (null != value) {
final String[] values = value.split(",");
final HashSet<String> tagset = new HashSet<String>();
for (final String s : values) {
tagset.add(s.trim());
}
if (null == node.getTags()) {
node.setTags(tagset);
} else {
final HashSet orig = new HashSet(node.getTags());
orig.addAll(tagset);
node.setTags(orig);
}
}
}
if (null == node.getTags()) {
node.setTags(new HashSet());
}
final HashSet orig = new HashSet(node.getTags());
//apply specific tag selectors
final Pattern tagPat = Pattern.compile("^tag\\.(.+?)\\.selector$");
//evaluate tag selectors
for (final Object o : mapping.keySet()) {
final String key = (String) o;
final String selector = mapping.getProperty(key);
//split selector by = if present
final String[] selparts = selector.split("=");
final Matcher m = tagPat.matcher(key);
if (m.matches()) {
final String tagName = m.group(1);
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
}
final String value = applySelector(inst, selparts[0], null);
if (null != value) {
if (selparts.length > 1 && !value.equals(selparts[1])) {
continue;
}
//use add the tag if the value is not null
orig.add(tagName);
}
}
}
node.setTags(orig);
//apply default values which do not have corresponding selector
final Pattern attribDefPat = Pattern.compile("^([^.]+?)\\.default$");
//evaluate selectors
for (final Object o : mapping.keySet()) {
final String key = (String) o;
final String value = mapping.getProperty(key);
final Matcher m = attribDefPat.matcher(key);
if (m.matches() && (!mapping.containsKey(key + ".selector") || "".equals(mapping.getProperty(
key + ".selector")))) {
final String attrName = m.group(1);
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
}
if (null != value) {
node.getAttributes().put(attrName, value);
}
}
}
final Pattern attribPat = Pattern.compile("^([^.]+?)\\.selector$");
//evaluate selectors
for (final Object o : mapping.keySet()) {
final String key = (String) o;
final String selector = mapping.getProperty(key);
final Matcher m = attribPat.matcher(key);
if (m.matches()) {
final String attrName = m.group(1);
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
}
final String value = applySelector(inst, selector, mapping.getProperty(attrName + ".default"));
if (null != value) {
//use nodename-settingname to make the setting unique to the node
node.getAttributes().put(attrName, value);
}
}
}
// String hostSel = mapping.getProperty("hostname.selector");
// String host = applySelector(inst, hostSel, mapping.getProperty("hostname.default"));
// if (null == node.getHostname()) {
// System.err.println("Unable to determine hostname for instance: " + inst.getInstanceId());
// return null;
// }
String name = node.getNodename();
if (null == name || "".equals(name)) {
name = node.getHostname();
}
if (null == name || "".equals(name)) {
name = inst.getInstanceId();
}
node.setNodename(name);
return node;
}