mirror of
https://github.com/Fishwaldo/rundeck-api-java-client.git
synced 2025-07-07 13:38:23 +00:00
add support for nodes (resources)
This commit is contained in:
parent
d6b6ec3405
commit
28c3eb20ae
23 changed files with 623 additions and 52 deletions
|
@ -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> T get(ApiPathBuilder apiPath, NodeParser<T> parser) throws RundeckApiException, RundeckApiLoginException {
|
||||
public <T> T get(ApiPathBuilder apiPath, XmlNodeParser<T> 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 {
|
||||
|
|
|
@ -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<RundeckExecution> 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<RundeckNode> getNodes() throws RundeckApiException, RundeckApiLoginException {
|
||||
List<RundeckNode> nodes = new ArrayList<RundeckNode>();
|
||||
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<RundeckNode> 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<RundeckNode> 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;
|
||||
}
|
||||
|
|
261
src/main/java/org/rundeck/api/domain/RundeckNode.java
Normal file
261
src/main/java/org/rundeck/api/domain/RundeckNode.java
Normal file
|
@ -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<String> 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<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(List<String> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -26,7 +26,7 @@ import org.rundeck.api.domain.RundeckAbort.AbortStatus;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class AbortParser implements NodeParser<RundeckAbort> {
|
||||
public class AbortParser implements XmlNodeParser<RundeckAbort> {
|
||||
|
||||
private String xpath;
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class AbortParser implements NodeParser<RundeckAbort> {
|
|||
}
|
||||
|
||||
@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<RundeckAbort> {
|
|||
|
||||
Node execNode = abortNode.selectSingleNode("execution");
|
||||
if (execNode != null) {
|
||||
RundeckExecution execution = new ExecutionParser().parseNode(execNode);
|
||||
RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode);
|
||||
abort.setExecution(execution);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ import org.rundeck.api.domain.RundeckExecution.ExecutionStatus;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class ExecutionParser implements NodeParser<RundeckExecution> {
|
||||
public class ExecutionParser implements XmlNodeParser<RundeckExecution> {
|
||||
|
||||
private String xpath;
|
||||
|
||||
|
@ -44,7 +44,7 @@ public class ExecutionParser implements NodeParser<RundeckExecution> {
|
|||
}
|
||||
|
||||
@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<RundeckExecution> {
|
|||
|
||||
Node jobNode = execNode.selectSingleNode("job");
|
||||
if (jobNode != null) {
|
||||
RundeckJob job = new JobParser().parseNode(jobNode);
|
||||
RundeckJob job = new JobParser().parseXmlNode(jobNode);
|
||||
execution.setJob(job);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckExecution;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class ExecutionsParser implements NodeParser<List<RundeckExecution>> {
|
||||
public class ExecutionsParser implements XmlNodeParser<List<RundeckExecution>> {
|
||||
|
||||
private final String xpath;
|
||||
|
||||
|
@ -38,14 +38,14 @@ public class ExecutionsParser implements NodeParser<List<RundeckExecution>> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<RundeckExecution> parseNode(Node node) {
|
||||
public List<RundeckExecution> parseXmlNode(Node node) {
|
||||
List<RundeckExecution> executions = new ArrayList<RundeckExecution>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Node> execNodes = node.selectNodes(xpath);
|
||||
|
||||
for (Node execNode : execNodes) {
|
||||
RundeckExecution execution = new ExecutionParser().parseNode(execNode);
|
||||
RundeckExecution execution = new ExecutionParser().parseXmlNode(execNode);
|
||||
executions.add(execution);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ import org.rundeck.api.domain.RundeckJob;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class JobParser implements NodeParser<RundeckJob> {
|
||||
public class JobParser implements XmlNodeParser<RundeckJob> {
|
||||
|
||||
private String xpath;
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class JobParser implements NodeParser<RundeckJob> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public RundeckJob parseNode(Node node) {
|
||||
public RundeckJob parseXmlNode(Node node) {
|
||||
Node jobNode = xpath != null ? node.selectSingleNode(xpath) : node;
|
||||
|
||||
RundeckJob job = new RundeckJob();
|
||||
|
|
|
@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckJob;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class JobsParser implements NodeParser<List<RundeckJob>> {
|
||||
public class JobsParser implements XmlNodeParser<List<RundeckJob>> {
|
||||
|
||||
private final String xpath;
|
||||
|
||||
|
@ -38,14 +38,14 @@ public class JobsParser implements NodeParser<List<RundeckJob>> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<RundeckJob> parseNode(Node node) {
|
||||
public List<RundeckJob> parseXmlNode(Node node) {
|
||||
List<RundeckJob> jobs = new ArrayList<RundeckJob>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Node> jobNodes = node.selectNodes(xpath);
|
||||
|
||||
for (Node jobNode : jobNodes) {
|
||||
RundeckJob job = new JobParser().parseNode(jobNode);
|
||||
RundeckJob job = new JobParser().parseXmlNode(jobNode);
|
||||
jobs.add(job);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<T> {
|
||||
public class NodeParser implements XmlNodeParser<RundeckNode> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
55
src/main/java/org/rundeck/api/parser/NodesParser.java
Normal file
55
src/main/java/org/rundeck/api/parser/NodesParser.java
Normal file
|
@ -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<List<RundeckNode>> {
|
||||
|
||||
private final String xpath;
|
||||
|
||||
/**
|
||||
* @param xpath of the rundeck-nodes elements
|
||||
*/
|
||||
public NodesParser(String xpath) {
|
||||
super();
|
||||
this.xpath = xpath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RundeckNode> parseXmlNode(Node node) {
|
||||
List<RundeckNode> rundeckNodes = new ArrayList<RundeckNode>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Node> rundeckNodeNodes = node.selectNodes(xpath);
|
||||
|
||||
for (Node rundeckNodeNode : rundeckNodeNodes) {
|
||||
RundeckNode rundeckNode = new NodeParser().parseXmlNode(rundeckNodeNode);
|
||||
rundeckNodes.add(rundeckNode);
|
||||
}
|
||||
|
||||
return rundeckNodes;
|
||||
}
|
||||
|
||||
}
|
|
@ -24,7 +24,7 @@ import org.rundeck.api.domain.RundeckProject;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class ProjectParser implements NodeParser<RundeckProject> {
|
||||
public class ProjectParser implements XmlNodeParser<RundeckProject> {
|
||||
|
||||
private String xpath;
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class ProjectParser implements NodeParser<RundeckProject> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public RundeckProject parseNode(Node node) {
|
||||
public RundeckProject parseXmlNode(Node node) {
|
||||
Node projectNode = xpath != null ? node.selectSingleNode(xpath) : node;
|
||||
|
||||
RundeckProject project = new RundeckProject();
|
||||
|
|
|
@ -25,7 +25,7 @@ import org.rundeck.api.domain.RundeckProject;
|
|||
*
|
||||
* @author Vincent Behar
|
||||
*/
|
||||
public class ProjectsParser implements NodeParser<List<RundeckProject>> {
|
||||
public class ProjectsParser implements XmlNodeParser<List<RundeckProject>> {
|
||||
|
||||
private final String xpath;
|
||||
|
||||
|
@ -38,14 +38,14 @@ public class ProjectsParser implements NodeParser<List<RundeckProject>> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<RundeckProject> parseNode(Node node) {
|
||||
public List<RundeckProject> parseXmlNode(Node node) {
|
||||
List<RundeckProject> projects = new ArrayList<RundeckProject>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Node> projectNodes = node.selectNodes(xpath);
|
||||
|
||||
for (Node projectNode : projectNodes) {
|
||||
RundeckProject project = new ProjectParser().parseNode(projectNode);
|
||||
RundeckProject project = new ProjectParser().parseXmlNode(projectNode);
|
||||
projects.add(project);
|
||||
}
|
||||
|
||||
|
|
35
src/main/java/org/rundeck/api/parser/XmlNodeParser.java
Normal file
35
src/main/java/org/rundeck/api/parser/XmlNodeParser.java
Normal file
|
@ -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<T> {
|
||||
|
||||
/**
|
||||
* Parse the given XML {@link Node}
|
||||
*
|
||||
* @param node
|
||||
* @return any object holding the converted value
|
||||
*/
|
||||
T parseXmlNode(Node node);
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<RundeckExecution> executions = new ExecutionsParser("result/executions/execution").parseNode(document);
|
||||
List<RundeckExecution> executions = new ExecutionsParser("result/executions/execution").parseXmlNode(document);
|
||||
Assert.assertEquals(2, executions.size());
|
||||
|
||||
RundeckExecution exec1 = executions.get(0);
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<RundeckJob> jobs = new JobsParser("result/jobs/job").parseNode(document);
|
||||
List<RundeckJob> jobs = new JobsParser("result/jobs/job").parseXmlNode(document);
|
||||
Assert.assertEquals(2, jobs.size());
|
||||
|
||||
RundeckJob job1 = jobs.get(0);
|
||||
|
|
53
src/test/java/org/rundeck/api/parser/NodeParserTest.java
Normal file
53
src/test/java/org/rundeck/api/parser/NodeParserTest.java
Normal file
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
56
src/test/java/org/rundeck/api/parser/NodesParserTest.java
Normal file
56
src/test/java/org/rundeck/api/parser/NodesParserTest.java
Normal file
|
@ -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<RundeckNode> 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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
|
|
|
@ -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<RundeckProject> projects = new ProjectsParser("result/projects/project").parseNode(document);
|
||||
List<RundeckProject> projects = new ProjectsParser("result/projects/project").parseXmlNode(document);
|
||||
Assert.assertEquals(2, projects.size());
|
||||
|
||||
RundeckProject project1 = projects.get(0);
|
||||
|
|
5
src/test/resources/org/rundeck/api/parser/resources.xml
Normal file
5
src/test/resources/org/rundeck/api/parser/resources.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project>
|
||||
<node name="strongbad" type="Node" description="a development host" tags="dev" hostname="strongbad.local" osArch="i386" osFamily="unix" osName="Linux" osVersion="2.6.35-30-generic-pae" username="rundeck" editUrl="" remoteUrl=""/>
|
||||
</project>
|
Loading…
Add table
Add a link
Reference in a new issue