Skip to content

Commit

Permalink
Update attributes code and clean up some uses of the node attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Jun 10, 2011
1 parent 33986d3 commit f0758dc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static NodeEntryImpl createFromMap(final Map<String, Object> map) throws
//populate attributes with any keys outside of nodeprops
for (final String key : map.keySet()) {
if (!ResourceXMLConstants.allPropSet.contains(key)) {
nodeEntry.getAttributes().put(key, (String) map.get(key));
nodeEntry.setAttribute(key, (String) map.get(key));
}
}

Expand All @@ -110,42 +110,9 @@ public static Map<String,String> toMap(final INodeEntry node) {
if(null!=node.getAttributes()) {
map.putAll(node.getAttributes());
}
try {
final Map describe = BeanUtils.describe(node);
map.putAll(describe);
/*for (final String s : ResourceXMLConstants.allPropSet) {

}*/
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null!=node.getTags()) {
//convert Set of tag strings into comma-separated scalar
String valueString = "";
Set values = node.getTags();
StringBuffer sb = new StringBuffer();
for (final Object tagValue : new TreeSet(values)) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(tagValue);
}
valueString = sb.toString();
map.put("tags", valueString);
}
HashSet<String> toremove = new HashSet<String>();
toremove.addAll(Arrays.asList(excludeProps));
for (final String s : map.keySet()) {
if(null==map.get(s)) {
toremove.add(s);
}
}
for (final String s : toremove) {
map.remove(s);
if(null==map.get("tags")) {
map.put("tags", "");
}
return map;
}
Expand Down
51 changes: 30 additions & 21 deletions core/src/java/com/dtolabs/rundeck/core/common/NodeEntryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public class NodeEntryImpl extends NodeBaseImpl implements INodeEntry, INodeDesc

protected static final String USER_AT_HOSTNAME_REGEX = "([^@])+@([^@:])+";
protected static final String PORT_REGEX = "([^:]+):([0-9]+)";
public static final String OS_NAME = "os-name";
public static final String OS_FAMILY = "os-family";
public static final String OS_VERSION = "os-version";
public static final String OS_NAME = "osName";
public static final String OS_FAMILY = "osFamily";
public static final String OS_VERSION = "osVersion";
public static final String HOSTNAME = "hostname";
public static final String OS_ARCH = "os-arch";
public static final String OS_ARCH = "osArch";
public static final String USERNAME = "username";
public static final String DESCRIPTION = "description";
public static final String NAME = "name";
public static final String NAME = "nodename";
public static final String TAGS = "tags";

private Set tags;
Expand Down Expand Up @@ -85,7 +85,7 @@ public static INodeEntry create(final INodeDesc node) {
@Override
public void setNodename(final String nodename) {
super.setNodename(nodename);
getAttributes().put(NAME, nodename);
setAttribute(NAME, nodename);
}

public Set getTags() {
Expand All @@ -96,57 +96,57 @@ public void setTags(final Set tags) {
this.tags = tags;
final Object[] objects = tags.toArray();
Arrays.sort(objects);
getAttributes().put(TAGS, StringArrayUtil.asString(objects, ","));
setAttribute(TAGS, StringArrayUtil.asString(objects, ", "));
}

public String getOsName() {
return getAttributes().get(OS_NAME);
return getAttribute(OS_NAME);
}


public void setOsName(final String osName) {
getAttributes().put(OS_NAME, osName);
setAttribute(OS_NAME, osName);
}

public String getOsFamily() {
return getAttributes().get(OS_FAMILY);
return getAttribute(OS_FAMILY);
}

public void setOsFamily(final String osFamily) {
getAttributes().put(OS_FAMILY, osFamily);
setAttribute(OS_FAMILY, osFamily);
}

public String getOsVersion() {
return getAttributes().get(OS_VERSION);
return getAttribute(OS_VERSION);
}

public void setOsVersion(final String osVersion) {
getAttributes().put(OS_VERSION, osVersion);
setAttribute(OS_VERSION, osVersion);
}

public String getHostname() {
return getAttributes().get(HOSTNAME);
return getAttribute(HOSTNAME);
}

public void setHostname(final String hostname) {
getAttributes().put(HOSTNAME, hostname);
setAttribute(HOSTNAME, hostname);
}

public String getOsArch() {
return getAttributes().get(OS_ARCH);
return getAttribute(OS_ARCH);
}

public void setOsArch(final String osArch) {
getAttributes().put(OS_ARCH, osArch);
setAttribute(OS_ARCH, osArch);
}


public String getUsername() {
return getAttributes().get(USERNAME);
return getAttribute(USERNAME);
}

public void setUsername(final String username) {
getAttributes().put(USERNAME, username);
setAttribute(USERNAME, username);
}


Expand Down Expand Up @@ -282,10 +282,19 @@ public void setAttributes(final Map<String, String> attributes) {
}

public void setDescription(final String description) {
getAttributes().put(DESCRIPTION, description);
setAttribute(DESCRIPTION, description);
}

public String getDescription() {
return getAttributes().get(DESCRIPTION);
return getAttribute(DESCRIPTION);
}


public String getAttribute(final String name) {
return getAttributes().get(name);
}

public String setAttribute(final String name, final String value) {
return getAttributes().put(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
import org.apache.tools.ant.types.Environment;

import java.io.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -379,7 +377,9 @@ public static Map<String, Map<String, String>> addContext(final String key, fina
*/
public static Map<String, String> nodeData(final INodeEntry nodeentry) {
final HashMap<String, String> data = new HashMap<String, String>();
if(null!=nodeentry){
if(null!=nodeentry) {
HashSet<String> skipProps = new HashSet<String>();
skipProps.addAll(Arrays.asList("nodename", "osName", "osVersion", "osArch", "osFamily"));
data.put("name", notNull(nodeentry.getNodename()));
data.put("hostname", notNull(nodeentry.getHostname()));
data.put("os-name", notNull(nodeentry.getOsName()));
Expand All @@ -392,8 +392,10 @@ public static Map<String, String> nodeData(final INodeEntry nodeentry) {
//include attributes data
if (null != nodeentry.getAttributes()) {
for (final String name : nodeentry.getAttributes().keySet()) {
if (null != nodeentry.getAttributes().get(name) && !data.containsKey(name)) {
data.put( name, notNull(nodeentry.getAttributes().get(name)));
if (null != nodeentry.getAttributes().get(name) && !data.containsKey(name) && !skipProps.contains(
name)) {

data.put(name, notNull(nodeentry.getAttributes().get(name)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,7 @@ private ResourceXMLParser.Entity createEntity(final INodeEntry node) {
final ResourceXMLParser.Entity ent = new ResourceXMLParser.Entity();
ent.setName(node.getNodename());
ent.setResourceType(NODE_ENTITY_TAG);
ent.setProperty(COMMON_DESCRIPTION, notNull(node.getDescription()));
if (null != node.getTags() && node.getTags().size() > 0) {
ent.setProperty(COMMON_TAGS, joinStrings(new TreeSet(node.getTags()), ","));
}
ent.setProperty(NODE_HOSTNAME, notNull(node.getHostname()));
ent.setProperty(NODE_OS_ARCH, notNull(node.getOsArch()));
ent.setProperty(NODE_OS_FAMILY, notNull(node.getOsFamily()));
ent.setProperty(NODE_OS_NAME, notNull(node.getOsName()));
ent.setProperty(NODE_OS_VERSION, notNull(node.getOsVersion()));
ent.setProperty(NODE_USERNAME, notNull(node.getUsername()));
if(null!=node.getAttributes() && null!=node.getAttributes().get(NODE_EDIT_URL)) {
ent.setProperty(NODE_EDIT_URL, notNull(node.getAttributes().get(NODE_EDIT_URL)));
}
if(null!=node.getAttributes() && null!=node.getAttributes().get(NODE_REMOTE_URL)) {
ent.setProperty(NODE_REMOTE_URL, notNull(node.getAttributes().get(NODE_REMOTE_URL)));
}
//iterate settings
//TODO: replace with attributes


if(null!=node.getAttributes()){
for (final String setName : node.getAttributes().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public void testNodeData() throws Exception {
final NodeEntryImpl nodeentry = new NodeEntryImpl("testhost", "test1");
final Map<String, String> stringMap = DataContextUtils.nodeData(nodeentry);
assertNotNull(stringMap);
assertEquals(9, stringMap.size());
assertEquals("wrong size: " + stringMap, 9, stringMap.size());
assertEquals("test1", stringMap.get("name"));
assertEquals("testhost", stringMap.get("hostname"));
assertEquals("", stringMap.get("os-name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void testResourceXMLGenerator() throws Exception {
assertEquals(1, root.selectNodes("node").size());
assertEquals("test1name", root.selectSingleNode("node/@name").getStringValue());
assertEquals("test1", root.selectSingleNode("node/@hostname").getStringValue());
assertEquals("a,d", root.selectSingleNode("node/@tags").getStringValue());
assertEquals("a, d", root.selectSingleNode("node/@tags").getStringValue());
assertEquals("test desc", root.selectSingleNode("node/@description").getStringValue());
assertEquals("test arch", root.selectSingleNode("node/@osArch").getStringValue());
assertEquals("test fam", root.selectSingleNode("node/@osFamily").getStringValue());
Expand Down Expand Up @@ -177,7 +177,7 @@ public void testResourceXMLGenerator() throws Exception {
assertEquals(1, root.selectNodes("node").size());
assertEquals("test1name", root.selectSingleNode("node/@name").getStringValue());
assertEquals("test1", root.selectSingleNode("node/@hostname").getStringValue());
assertEquals("a,d", root.selectSingleNode("node/@tags").getStringValue());
assertEquals("a, d", root.selectSingleNode("node/@tags").getStringValue());
assertEquals("test desc", root.selectSingleNode("node/@description").getStringValue());
assertEquals("test arch", root.selectSingleNode("node/@osArch").getStringValue());
assertEquals("test fam", root.selectSingleNode("node/@osFamily").getStringValue());
Expand Down

0 comments on commit f0758dc

Please sign in to comment.