diff --git a/src/main/java/org/rundeck/api/ApiCall.java b/src/main/java/org/rundeck/api/ApiCall.java index 197c5a9..3293151 100644 --- a/src/main/java/org/rundeck/api/ApiCall.java +++ b/src/main/java/org/rundeck/api/ApiCall.java @@ -41,7 +41,7 @@ import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.dom4j.Document; import org.rundeck.api.RundeckApiException.RundeckApiLoginException; -import org.rundeck.api.parser.NodeParser; +import org.rundeck.api.parser.XmlNodeParser; import org.rundeck.api.parser.ParserHelper; import org.rundeck.api.util.AssertUtil; @@ -110,7 +110,7 @@ class ApiCall { * @throws RundeckApiException in case of error when calling the API * @throws RundeckApiLoginException if the login fails */ - public T get(ApiPathBuilder apiPath, NodeParser parser) throws RundeckApiException, RundeckApiLoginException { + public T get(ApiPathBuilder apiPath, XmlNodeParser parser) throws RundeckApiException, RundeckApiLoginException { String apiUrl = client.getUrl() + RundeckClient.API_ENDPOINT + apiPath; HttpClient httpClient = instantiateHttpClient(); @@ -134,7 +134,7 @@ class ApiCall { // read and parse the response Document xmlDocument = ParserHelper.loadDocument(response); - T result = parser.parseNode(xmlDocument); + T result = parser.parseXmlNode(xmlDocument); // release the connection try { diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index 42acd0a..8e2b677 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -25,6 +25,7 @@ import org.rundeck.api.RundeckApiException.RundeckApiLoginException; import org.rundeck.api.domain.RundeckAbort; import org.rundeck.api.domain.RundeckExecution; import org.rundeck.api.domain.RundeckJob; +import org.rundeck.api.domain.RundeckNode; import org.rundeck.api.domain.RundeckProject; import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; import org.rundeck.api.parser.AbortParser; @@ -32,6 +33,8 @@ import org.rundeck.api.parser.ExecutionParser; import org.rundeck.api.parser.ExecutionsParser; import org.rundeck.api.parser.JobParser; import org.rundeck.api.parser.JobsParser; +import org.rundeck.api.parser.NodeParser; +import org.rundeck.api.parser.NodesParser; import org.rundeck.api.parser.ProjectParser; import org.rundeck.api.parser.ProjectsParser; import org.rundeck.api.util.AssertUtil; @@ -585,7 +588,7 @@ public class RundeckClient implements Serializable { */ public List getRunningExecutions(String project) throws RundeckApiException, RundeckApiLoginException, IllegalArgumentException { - AssertUtil.notBlank(project, "project is mandatory to trigger an ad-hoc command !"); + AssertUtil.notBlank(project, "project is mandatory get all running executions !"); return new ApiCall(this).get(new ApiPathBuilder("/executions/running").param("project", project), new ExecutionsParser("result/executions/execution")); } @@ -673,6 +676,76 @@ public class RundeckClient implements Serializable { new AbortParser("result/abort")); } + /* + * Nodes + */ + + /** + * List all nodes (for all projects) + * + * @return a {@link List} of {@link RundeckNode} : might be empty, but won't be null + * @throws RundeckApiException in case of error when calling the API + * @throws RundeckApiLoginException if the login failed + */ + public List getNodes() throws RundeckApiException, RundeckApiLoginException { + List nodes = new ArrayList(); + for (RundeckProject project : getProjects()) { + nodes.addAll(getNodes(project.getName())); + } + return nodes; + } + + /** + * List all nodes that belongs to the given project + * + * @param project name of the project - mandatory + * @return a {@link List} of {@link RundeckNode} : might be empty, but won't be null + * @throws RundeckApiException in case of error when calling the API (non-existent project with this name) + * @throws RundeckApiLoginException if the login failed + * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) + * @see #getNodes(String, Properties) + */ + public List getNodes(String project) throws RundeckApiException, RundeckApiLoginException, + IllegalArgumentException { + return getNodes(project, null); + } + + /** + * List nodes that belongs to the given project + * + * @param project name of the project - mandatory + * @param nodeFilters for filtering the nodes - optional. See {@link NodeFiltersBuilder} + * @return a {@link List} of {@link RundeckNode} : might be empty, but won't be null + * @throws RundeckApiException in case of error when calling the API (non-existent project with this name) + * @throws RundeckApiLoginException if the login failed + * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) + */ + public List getNodes(String project, Properties nodeFilters) throws RundeckApiException, + RundeckApiLoginException, IllegalArgumentException { + AssertUtil.notBlank(project, "project is mandatory to get all nodes !"); + return new ApiCall(this).get(new ApiPathBuilder("/resources").param("project", project) + .nodeFilters(nodeFilters), + new NodesParser("project/node")); + } + + /** + * Get the definition of a single node + * + * @param name of the node - mandatory + * @param project name of the project - mandatory + * @return a {@link RundeckNode} instance - won't be null + * @throws RundeckApiException in case of error when calling the API (non-existent name or project with this name) + * @throws RundeckApiLoginException if the login failed + * @throws IllegalArgumentException if the name or project is blank (null, empty or whitespace) + */ + public RundeckNode getNode(String name, String project) throws RundeckApiException, RundeckApiLoginException, + IllegalArgumentException { + AssertUtil.notBlank(name, "the name of the node is mandatory to get a node !"); + AssertUtil.notBlank(project, "project is mandatory to get a node !"); + return new ApiCall(this).get(new ApiPathBuilder("/resource/", name).param("project", project), + new NodeParser("project/node")); + } + public String getUrl() { return url; } diff --git a/src/main/java/org/rundeck/api/domain/RundeckNode.java b/src/main/java/org/rundeck/api/domain/RundeckNode.java new file mode 100644 index 0000000..87a1a36 --- /dev/null +++ b/src/main/java/org/rundeck/api/domain/RundeckNode.java @@ -0,0 +1,261 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api.domain; + +import java.io.Serializable; +import java.util.List; + +/** + * Represents a RunDeck node (server on which RunDeck can execute jobs and commands) + * + * @author Vincent Behar + */ +public class RundeckNode implements Serializable { + + private static final long serialVersionUID = 1L; + + /** The node name. This is a logical identifier from the node. (required) */ + private String name; + + /** The node type, such as "Node". (required) */ + private String type; + + /** A brief description about the node. (optional) */ + private String description; + + /** List of filtering tags. (optional) */ + private List tags; + + /** The hostname or IP address of the remote host. (required) */ + private String hostname; + + /** The operating system architecture. (optional) */ + private String osArch; + + /** The operating system family, such as unix or windows. (optional) */ + private String osFamily; + + /** The operating system name such as Linux or Mac OS X. (optional) */ + private String osName; + + /** The operating system version. (optional) */ + private String osVersion; + + /** The username used for the remote connection. (required) */ + private String username; + + /** URL to an external resource model editor service (optional) */ + private String editUrl; + + /** URL to an external resource model service. (optional) */ + private String remoteUrl; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } + + public String getOsArch() { + return osArch; + } + + public void setOsArch(String osArch) { + this.osArch = osArch; + } + + public String getOsFamily() { + return osFamily; + } + + public void setOsFamily(String osFamily) { + this.osFamily = osFamily; + } + + public String getOsName() { + return osName; + } + + public void setOsName(String osName) { + this.osName = osName; + } + + public String getOsVersion() { + return osVersion; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEditUrl() { + return editUrl; + } + + public void setEditUrl(String editUrl) { + this.editUrl = editUrl; + } + + public String getRemoteUrl() { + return remoteUrl; + } + + public void setRemoteUrl(String remoteUrl) { + this.remoteUrl = remoteUrl; + } + + @Override + public String toString() { + return "RundeckNode [name=" + name + ", hostname=" + hostname + ", description=" + description + ", tags=" + + tags + ", type=" + type + ", username=" + username + ", osArch=" + osArch + ", osFamily=" + osFamily + + ", osName=" + osName + ", osVersion=" + osVersion + ", editUrl=" + editUrl + ", remoteUrl=" + + remoteUrl + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((editUrl == null) ? 0 : editUrl.hashCode()); + result = prime * result + ((hostname == null) ? 0 : hostname.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((osArch == null) ? 0 : osArch.hashCode()); + result = prime * result + ((osFamily == null) ? 0 : osFamily.hashCode()); + result = prime * result + ((osName == null) ? 0 : osName.hashCode()); + result = prime * result + ((osVersion == null) ? 0 : osVersion.hashCode()); + result = prime * result + ((remoteUrl == null) ? 0 : remoteUrl.hashCode()); + result = prime * result + ((tags == null) ? 0 : tags.hashCode()); + result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RundeckNode other = (RundeckNode) obj; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (editUrl == null) { + if (other.editUrl != null) + return false; + } else if (!editUrl.equals(other.editUrl)) + return false; + if (hostname == null) { + if (other.hostname != null) + return false; + } else if (!hostname.equals(other.hostname)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (osArch == null) { + if (other.osArch != null) + return false; + } else if (!osArch.equals(other.osArch)) + return false; + if (osFamily == null) { + if (other.osFamily != null) + return false; + } else if (!osFamily.equals(other.osFamily)) + return false; + if (osName == null) { + if (other.osName != null) + return false; + } else if (!osName.equals(other.osName)) + return false; + if (osVersion == null) { + if (other.osVersion != null) + return false; + } else if (!osVersion.equals(other.osVersion)) + return false; + if (remoteUrl == null) { + if (other.remoteUrl != null) + return false; + } else if (!remoteUrl.equals(other.remoteUrl)) + return false; + if (tags == null) { + if (other.tags != null) + return false; + } else if (!tags.equals(other.tags)) + return false; + if (type == null) { + if (other.type != null) + return false; + } else if (!type.equals(other.type)) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + +} diff --git a/src/main/java/org/rundeck/api/parser/AbortParser.java b/src/main/java/org/rundeck/api/parser/AbortParser.java index 87f69f7..32ce0cd 100644 --- a/src/main/java/org/rundeck/api/parser/AbortParser.java +++ b/src/main/java/org/rundeck/api/parser/AbortParser.java @@ -26,7 +26,7 @@ import org.rundeck.api.domain.RundeckAbort.AbortStatus; * * @author Vincent Behar */ -public class AbortParser implements NodeParser { +public class AbortParser implements XmlNodeParser { private String xpath; @@ -43,7 +43,7 @@ public class AbortParser implements NodeParser { } @Override - public RundeckAbort parseNode(Node node) { + public RundeckAbort parseXmlNode(Node node) { Node abortNode = xpath != null ? node.selectSingleNode(xpath) : node; RundeckAbort abort = new RundeckAbort(); @@ -55,7 +55,7 @@ public class AbortParser implements NodeParser { Node execNode = abortNode.selectSingleNode("execution"); if (execNode != null) { - RundeckExecution execution = new ExecutionParser().parseNode(execNode); + RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode); abort.setExecution(execution); } diff --git a/src/main/java/org/rundeck/api/parser/ExecutionParser.java b/src/main/java/org/rundeck/api/parser/ExecutionParser.java index 4d463d6..2af8fdf 100644 --- a/src/main/java/org/rundeck/api/parser/ExecutionParser.java +++ b/src/main/java/org/rundeck/api/parser/ExecutionParser.java @@ -27,7 +27,7 @@ import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; * * @author Vincent Behar */ -public class ExecutionParser implements NodeParser { +public class ExecutionParser implements XmlNodeParser { private String xpath; @@ -44,7 +44,7 @@ public class ExecutionParser implements NodeParser { } @Override - public RundeckExecution parseNode(Node node) { + public RundeckExecution parseXmlNode(Node node) { Node execNode = xpath != null ? node.selectSingleNode(xpath) : node; RundeckExecution execution = new RundeckExecution(); @@ -69,7 +69,7 @@ public class ExecutionParser implements NodeParser { Node jobNode = execNode.selectSingleNode("job"); if (jobNode != null) { - RundeckJob job = new JobParser().parseNode(jobNode); + RundeckJob job = new JobParser().parseXmlNode(jobNode); execution.setJob(job); } diff --git a/src/main/java/org/rundeck/api/parser/ExecutionsParser.java b/src/main/java/org/rundeck/api/parser/ExecutionsParser.java index 8839f78..a170173 100644 --- a/src/main/java/org/rundeck/api/parser/ExecutionsParser.java +++ b/src/main/java/org/rundeck/api/parser/ExecutionsParser.java @@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckExecution; * * @author Vincent Behar */ -public class ExecutionsParser implements NodeParser> { +public class ExecutionsParser implements XmlNodeParser> { private final String xpath; @@ -38,14 +38,14 @@ public class ExecutionsParser implements NodeParser> { } @Override - public List parseNode(Node node) { + public List parseXmlNode(Node node) { List executions = new ArrayList(); @SuppressWarnings("unchecked") List execNodes = node.selectNodes(xpath); for (Node execNode : execNodes) { - RundeckExecution execution = new ExecutionParser().parseNode(execNode); + RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode); executions.add(execution); } diff --git a/src/main/java/org/rundeck/api/parser/JobParser.java b/src/main/java/org/rundeck/api/parser/JobParser.java index ca56696..5785d9e 100644 --- a/src/main/java/org/rundeck/api/parser/JobParser.java +++ b/src/main/java/org/rundeck/api/parser/JobParser.java @@ -24,7 +24,7 @@ import org.rundeck.api.domain.RundeckJob; * * @author Vincent Behar */ -public class JobParser implements NodeParser { +public class JobParser implements XmlNodeParser { private String xpath; @@ -41,7 +41,7 @@ public class JobParser implements NodeParser { } @Override - public RundeckJob parseNode(Node node) { + public RundeckJob parseXmlNode(Node node) { Node jobNode = xpath != null ? node.selectSingleNode(xpath) : node; RundeckJob job = new RundeckJob(); diff --git a/src/main/java/org/rundeck/api/parser/JobsParser.java b/src/main/java/org/rundeck/api/parser/JobsParser.java index 47629ac..cfed818 100644 --- a/src/main/java/org/rundeck/api/parser/JobsParser.java +++ b/src/main/java/org/rundeck/api/parser/JobsParser.java @@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckJob; * * @author Vincent Behar */ -public class JobsParser implements NodeParser> { +public class JobsParser implements XmlNodeParser> { private final String xpath; @@ -38,14 +38,14 @@ public class JobsParser implements NodeParser> { } @Override - public List parseNode(Node node) { + public List parseXmlNode(Node node) { List jobs = new ArrayList(); @SuppressWarnings("unchecked") List jobNodes = node.selectNodes(xpath); for (Node jobNode : jobNodes) { - RundeckJob job = new JobParser().parseNode(jobNode); + RundeckJob job = new JobParser().parseXmlNode(jobNode); jobs.add(job); } diff --git a/src/main/java/org/rundeck/api/parser/NodeParser.java b/src/main/java/org/rundeck/api/parser/NodeParser.java index 795fc82..9e60e83 100644 --- a/src/main/java/org/rundeck/api/parser/NodeParser.java +++ b/src/main/java/org/rundeck/api/parser/NodeParser.java @@ -15,21 +15,54 @@ */ package org.rundeck.api.parser; +import java.util.Arrays; +import org.apache.commons.lang.StringUtils; import org.dom4j.Node; +import org.rundeck.api.domain.RundeckNode; /** - * Interface to be implemented for parsers that handle XML {@link Node}s + * Parser for a single {@link RundeckNode} * * @author Vincent Behar */ -public interface NodeParser { +public class NodeParser implements XmlNodeParser { + + private String xpath; + + public NodeParser() { + super(); + } /** - * Parse the given XML {@link Node} - * - * @param node - * @return any object holding the converted value + * @param xpath of the rundeck-node element if it is not the root xml-node */ - T parseNode(Node node); + public NodeParser(String xpath) { + super(); + this.xpath = xpath; + } + + @Override + public RundeckNode parseXmlNode(Node node) { + Node rundeckNodeNode = xpath != null ? node.selectSingleNode(xpath) : node; + + RundeckNode rundeckNode = new RundeckNode(); + + rundeckNode.setName(StringUtils.trimToNull(rundeckNodeNode.valueOf("@name"))); + rundeckNode.setType(StringUtils.trimToNull(rundeckNodeNode.valueOf("@type"))); + rundeckNode.setDescription(StringUtils.trimToNull(rundeckNodeNode.valueOf("@description"))); + rundeckNode.setHostname(StringUtils.trimToNull(rundeckNodeNode.valueOf("@hostname"))); + rundeckNode.setOsArch(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osArch"))); + rundeckNode.setOsFamily(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osFamily"))); + rundeckNode.setOsName(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osName"))); + rundeckNode.setOsVersion(StringUtils.trimToNull(rundeckNodeNode.valueOf("@osVersion"))); + rundeckNode.setUsername(StringUtils.trimToNull(rundeckNodeNode.valueOf("@username"))); + rundeckNode.setEditUrl(StringUtils.trimToNull(rundeckNodeNode.valueOf("@editUrl"))); + rundeckNode.setRemoteUrl(StringUtils.trimToNull(rundeckNodeNode.valueOf("@remoteUrl"))); + + String tags = StringUtils.trimToEmpty(rundeckNodeNode.valueOf("@tags")); + rundeckNode.setTags(Arrays.asList(StringUtils.split(tags, ","))); + + return rundeckNode; + } } diff --git a/src/main/java/org/rundeck/api/parser/NodesParser.java b/src/main/java/org/rundeck/api/parser/NodesParser.java new file mode 100644 index 0000000..3716bb7 --- /dev/null +++ b/src/main/java/org/rundeck/api/parser/NodesParser.java @@ -0,0 +1,55 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api.parser; + +import java.util.ArrayList; +import java.util.List; +import org.dom4j.Node; +import org.rundeck.api.domain.RundeckNode; + +/** + * Parser for a {@link List} of {@link RundeckNode} + * + * @author Vincent Behar + */ +public class NodesParser implements XmlNodeParser> { + + private final String xpath; + + /** + * @param xpath of the rundeck-nodes elements + */ + public NodesParser(String xpath) { + super(); + this.xpath = xpath; + } + + @Override + public List parseXmlNode(Node node) { + List rundeckNodes = new ArrayList(); + + @SuppressWarnings("unchecked") + List rundeckNodeNodes = node.selectNodes(xpath); + + for (Node rundeckNodeNode : rundeckNodeNodes) { + RundeckNode rundeckNode = new NodeParser().parseXmlNode(rundeckNodeNode); + rundeckNodes.add(rundeckNode); + } + + return rundeckNodes; + } + +} diff --git a/src/main/java/org/rundeck/api/parser/ProjectParser.java b/src/main/java/org/rundeck/api/parser/ProjectParser.java index 96c05a5..ef19a47 100644 --- a/src/main/java/org/rundeck/api/parser/ProjectParser.java +++ b/src/main/java/org/rundeck/api/parser/ProjectParser.java @@ -24,7 +24,7 @@ import org.rundeck.api.domain.RundeckProject; * * @author Vincent Behar */ -public class ProjectParser implements NodeParser { +public class ProjectParser implements XmlNodeParser { private String xpath; @@ -41,7 +41,7 @@ public class ProjectParser implements NodeParser { } @Override - public RundeckProject parseNode(Node node) { + public RundeckProject parseXmlNode(Node node) { Node projectNode = xpath != null ? node.selectSingleNode(xpath) : node; RundeckProject project = new RundeckProject(); diff --git a/src/main/java/org/rundeck/api/parser/ProjectsParser.java b/src/main/java/org/rundeck/api/parser/ProjectsParser.java index d613772..1c656f1 100644 --- a/src/main/java/org/rundeck/api/parser/ProjectsParser.java +++ b/src/main/java/org/rundeck/api/parser/ProjectsParser.java @@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckProject; * * @author Vincent Behar */ -public class ProjectsParser implements NodeParser> { +public class ProjectsParser implements XmlNodeParser> { private final String xpath; @@ -38,14 +38,14 @@ public class ProjectsParser implements NodeParser> { } @Override - public List parseNode(Node node) { + public List parseXmlNode(Node node) { List projects = new ArrayList(); @SuppressWarnings("unchecked") List projectNodes = node.selectNodes(xpath); for (Node projectNode : projectNodes) { - RundeckProject project = new ProjectParser().parseNode(projectNode); + RundeckProject project = new ProjectParser().parseXmlNode(projectNode); projects.add(project); } diff --git a/src/main/java/org/rundeck/api/parser/XmlNodeParser.java b/src/main/java/org/rundeck/api/parser/XmlNodeParser.java new file mode 100644 index 0000000..9264916 --- /dev/null +++ b/src/main/java/org/rundeck/api/parser/XmlNodeParser.java @@ -0,0 +1,35 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api.parser; + +import org.dom4j.Node; + +/** + * Interface to be implemented for parsers that handle XML {@link Node}s + * + * @author Vincent Behar + */ +public interface XmlNodeParser { + + /** + * Parse the given XML {@link Node} + * + * @param node + * @return any object holding the converted value + */ + T parseXmlNode(Node node); + +} diff --git a/src/test/java/org/rundeck/api/parser/AbortParserTest.java b/src/test/java/org/rundeck/api/parser/AbortParserTest.java index 22f64b7..cb8c17c 100644 --- a/src/test/java/org/rundeck/api/parser/AbortParserTest.java +++ b/src/test/java/org/rundeck/api/parser/AbortParserTest.java @@ -32,11 +32,11 @@ import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; public class AbortParserTest { @Test - public void parsePendingNode() throws Exception { + public void parsePendingAbort() throws Exception { InputStream input = getClass().getResourceAsStream("abort-pending.xml"); Document document = ParserHelper.loadDocument(input); - RundeckAbort abort = new AbortParser("result/abort").parseNode(document); + RundeckAbort abort = new AbortParser("result/abort").parseXmlNode(document); RundeckExecution execution = abort.getExecution(); Assert.assertEquals(AbortStatus.PENDING, abort.getStatus()); @@ -46,11 +46,11 @@ public class AbortParserTest { } @Test - public void parseFailedNode() throws Exception { + public void parseFailedAbort() throws Exception { InputStream input = getClass().getResourceAsStream("abort-failed.xml"); Document document = ParserHelper.loadDocument(input); - RundeckAbort abort = new AbortParser("result/abort").parseNode(document); + RundeckAbort abort = new AbortParser("result/abort").parseXmlNode(document); RundeckExecution execution = abort.getExecution(); Assert.assertEquals(AbortStatus.FAILED, abort.getStatus()); diff --git a/src/test/java/org/rundeck/api/parser/ExecutionParserTest.java b/src/test/java/org/rundeck/api/parser/ExecutionParserTest.java index 0c6720a..8743e89 100644 --- a/src/test/java/org/rundeck/api/parser/ExecutionParserTest.java +++ b/src/test/java/org/rundeck/api/parser/ExecutionParserTest.java @@ -32,11 +32,11 @@ import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; public class ExecutionParserTest { @Test - public void parseRunningNode() throws Exception { + public void parseRunningExecution() throws Exception { InputStream input = getClass().getResourceAsStream("execution-running.xml"); Document document = ParserHelper.loadDocument(input); - RundeckExecution execution = new ExecutionParser("result/executions/execution").parseNode(document); + RundeckExecution execution = new ExecutionParser("result/executions/execution").parseXmlNode(document); RundeckJob job = execution.getJob(); Assert.assertEquals(new Long(1), execution.getId()); @@ -56,11 +56,11 @@ public class ExecutionParserTest { } @Test - public void parseSucceededNode() throws Exception { + public void parseSucceededExecution() throws Exception { InputStream input = getClass().getResourceAsStream("execution-succeeded.xml"); Document document = ParserHelper.loadDocument(input); - RundeckExecution execution = new ExecutionParser("result/executions/execution").parseNode(document); + RundeckExecution execution = new ExecutionParser("result/executions/execution").parseXmlNode(document); RundeckJob job = execution.getJob(); Assert.assertEquals(new Long(1), execution.getId()); @@ -80,11 +80,11 @@ public class ExecutionParserTest { } @Test - public void parseAdhocNode() throws Exception { + public void parseAdhocExecution() throws Exception { InputStream input = getClass().getResourceAsStream("execution-adhoc.xml"); Document document = ParserHelper.loadDocument(input); - RundeckExecution execution = new ExecutionParser("result/executions/execution").parseNode(document); + RundeckExecution execution = new ExecutionParser("result/executions/execution").parseXmlNode(document); RundeckJob job = execution.getJob(); Assert.assertEquals(new Long(1), execution.getId()); @@ -100,11 +100,11 @@ public class ExecutionParserTest { } @Test - public void parseMinimalistNode() throws Exception { + public void parseMinimalistExecution() throws Exception { InputStream input = getClass().getResourceAsStream("execution-minimalist.xml"); Document document = ParserHelper.loadDocument(input); - RundeckExecution execution = new ExecutionParser("result/execution").parseNode(document); + RundeckExecution execution = new ExecutionParser("result/execution").parseXmlNode(document); RundeckJob job = execution.getJob(); Assert.assertEquals(new Long(1), execution.getId()); diff --git a/src/test/java/org/rundeck/api/parser/ExecutionsParserTest.java b/src/test/java/org/rundeck/api/parser/ExecutionsParserTest.java index 95b503f..3827c45 100644 --- a/src/test/java/org/rundeck/api/parser/ExecutionsParserTest.java +++ b/src/test/java/org/rundeck/api/parser/ExecutionsParserTest.java @@ -33,11 +33,11 @@ import org.rundeck.api.domain.RundeckExecution.ExecutionStatus; public class ExecutionsParserTest { @Test - public void parseNode() throws Exception { + public void parseExecutions() throws Exception { InputStream input = getClass().getResourceAsStream("executions.xml"); Document document = ParserHelper.loadDocument(input); - List executions = new ExecutionsParser("result/executions/execution").parseNode(document); + List executions = new ExecutionsParser("result/executions/execution").parseXmlNode(document); Assert.assertEquals(2, executions.size()); RundeckExecution exec1 = executions.get(0); diff --git a/src/test/java/org/rundeck/api/parser/JobParserTest.java b/src/test/java/org/rundeck/api/parser/JobParserTest.java index 572633c..9882570 100644 --- a/src/test/java/org/rundeck/api/parser/JobParserTest.java +++ b/src/test/java/org/rundeck/api/parser/JobParserTest.java @@ -29,11 +29,11 @@ import org.rundeck.api.domain.RundeckJob; public class JobParserTest { @Test - public void parseNode() throws Exception { + public void parseJob() throws Exception { InputStream input = getClass().getResourceAsStream("job.xml"); Document document = ParserHelper.loadDocument(input); - RundeckJob job = new JobParser("joblist/job").parseNode(document); + RundeckJob job = new JobParser("joblist/job").parseXmlNode(document); Assert.assertEquals("1", job.getId()); Assert.assertEquals("job-name", job.getName()); diff --git a/src/test/java/org/rundeck/api/parser/JobsParserTest.java b/src/test/java/org/rundeck/api/parser/JobsParserTest.java index 71f8797..b314541 100644 --- a/src/test/java/org/rundeck/api/parser/JobsParserTest.java +++ b/src/test/java/org/rundeck/api/parser/JobsParserTest.java @@ -30,11 +30,11 @@ import org.rundeck.api.domain.RundeckJob; public class JobsParserTest { @Test - public void parseNode() throws Exception { + public void parseJobs() throws Exception { InputStream input = getClass().getResourceAsStream("jobs.xml"); Document document = ParserHelper.loadDocument(input); - List jobs = new JobsParser("result/jobs/job").parseNode(document); + List jobs = new JobsParser("result/jobs/job").parseXmlNode(document); Assert.assertEquals(2, jobs.size()); RundeckJob job1 = jobs.get(0); diff --git a/src/test/java/org/rundeck/api/parser/NodeParserTest.java b/src/test/java/org/rundeck/api/parser/NodeParserTest.java new file mode 100644 index 0000000..873f4e4 --- /dev/null +++ b/src/test/java/org/rundeck/api/parser/NodeParserTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api.parser; + +import java.io.InputStream; +import java.util.Arrays; +import org.dom4j.Document; +import org.junit.Assert; +import org.junit.Test; +import org.rundeck.api.domain.RundeckNode; + +/** + * Test the {@link NodeParser} + * + * @author Vincent Behar + */ +public class NodeParserTest { + + @Test + public void parseNode() throws Exception { + InputStream input = getClass().getResourceAsStream("resources.xml"); + Document document = ParserHelper.loadDocument(input); + + RundeckNode node = new NodeParser("project/node").parseXmlNode(document); + + Assert.assertEquals("strongbad", node.getName()); + Assert.assertEquals("Node", node.getType()); + Assert.assertEquals("a development host", node.getDescription()); + Assert.assertEquals(Arrays.asList("dev"), node.getTags()); + Assert.assertEquals("strongbad.local", node.getHostname()); + Assert.assertEquals("i386", node.getOsArch()); + Assert.assertEquals("unix", node.getOsFamily()); + Assert.assertEquals("Linux", node.getOsName()); + Assert.assertEquals("2.6.35-30-generic-pae", node.getOsVersion()); + Assert.assertEquals("rundeck", node.getUsername()); + Assert.assertEquals(null, node.getEditUrl()); + Assert.assertEquals(null, node.getRemoteUrl()); + } + +} diff --git a/src/test/java/org/rundeck/api/parser/NodesParserTest.java b/src/test/java/org/rundeck/api/parser/NodesParserTest.java new file mode 100644 index 0000000..f15cb7f --- /dev/null +++ b/src/test/java/org/rundeck/api/parser/NodesParserTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2011 Vincent Behar + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.rundeck.api.parser; + +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; +import org.dom4j.Document; +import org.junit.Assert; +import org.junit.Test; +import org.rundeck.api.domain.RundeckNode; + +/** + * Test the {@link NodesParser} + * + * @author Vincent Behar + */ +public class NodesParserTest { + + @Test + public void parseNodes() throws Exception { + InputStream input = getClass().getResourceAsStream("resources.xml"); + Document document = ParserHelper.loadDocument(input); + + List nodes = new NodesParser("project/node").parseXmlNode(document); + Assert.assertEquals(1, nodes.size()); + + RundeckNode node1 = nodes.get(0); + Assert.assertEquals("strongbad", node1.getName()); + Assert.assertEquals("Node", node1.getType()); + Assert.assertEquals("a development host", node1.getDescription()); + Assert.assertEquals(Arrays.asList("dev"), node1.getTags()); + Assert.assertEquals("strongbad.local", node1.getHostname()); + Assert.assertEquals("i386", node1.getOsArch()); + Assert.assertEquals("unix", node1.getOsFamily()); + Assert.assertEquals("Linux", node1.getOsName()); + Assert.assertEquals("2.6.35-30-generic-pae", node1.getOsVersion()); + Assert.assertEquals("rundeck", node1.getUsername()); + Assert.assertEquals(null, node1.getEditUrl()); + Assert.assertEquals(null, node1.getRemoteUrl()); + } + +} diff --git a/src/test/java/org/rundeck/api/parser/ProjectParserTest.java b/src/test/java/org/rundeck/api/parser/ProjectParserTest.java index d985697..91f7eee 100644 --- a/src/test/java/org/rundeck/api/parser/ProjectParserTest.java +++ b/src/test/java/org/rundeck/api/parser/ProjectParserTest.java @@ -29,11 +29,11 @@ import org.rundeck.api.domain.RundeckProject; public class ProjectParserTest { @Test - public void parseNode() throws Exception { + public void parseProject() throws Exception { InputStream input = getClass().getResourceAsStream("project.xml"); Document document = ParserHelper.loadDocument(input); - RundeckProject project = new ProjectParser("result/projects/project").parseNode(document); + RundeckProject project = new ProjectParser("result/projects/project").parseXmlNode(document); Assert.assertEquals("test", project.getName()); Assert.assertEquals("test project", project.getDescription()); diff --git a/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java b/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java index 48688c0..a9ebf9b 100644 --- a/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java +++ b/src/test/java/org/rundeck/api/parser/ProjectsParserTest.java @@ -30,11 +30,11 @@ import org.rundeck.api.domain.RundeckProject; public class ProjectsParserTest { @Test - public void parseNode() throws Exception { + public void parseProjects() throws Exception { InputStream input = getClass().getResourceAsStream("projects.xml"); Document document = ParserHelper.loadDocument(input); - List projects = new ProjectsParser("result/projects/project").parseNode(document); + List projects = new ProjectsParser("result/projects/project").parseXmlNode(document); Assert.assertEquals(2, projects.size()); RundeckProject project1 = projects.get(0); diff --git a/src/test/resources/org/rundeck/api/parser/resources.xml b/src/test/resources/org/rundeck/api/parser/resources.xml new file mode 100644 index 0000000..62d29ce --- /dev/null +++ b/src/test/resources/org/rundeck/api/parser/resources.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file