add mappings for /projects and /project/NAME

This commit is contained in:
Vincent Behar 2011-07-01 18:18:26 +02:00
parent 4aab43d4ed
commit 1dfaa8555a
8 changed files with 242 additions and 0 deletions

View file

@ -7,10 +7,13 @@ import org.apache.commons.lang.StringUtils;
import org.rundeck.api.RundeckApiException.RundeckApiLoginException;
import org.rundeck.api.domain.RundeckExecution;
import org.rundeck.api.domain.RundeckJob;
import org.rundeck.api.domain.RundeckProject;
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.ProjectParser;
import org.rundeck.api.parser.ProjectsParser;
import org.rundeck.api.util.ArgsUtil;
import org.rundeck.api.util.AssertUtil;
@ -174,6 +177,32 @@ public class RundeckClient implements Serializable {
return new ApiCall(this).get(apiPath, new ExecutionParser("result/executions/execution"));
}
/**
* List all projects
*
* @return a {@link List} of {@link RundeckProject} : 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<RundeckProject> getProjects() throws RundeckApiException, RundeckApiLoginException {
return new ApiCall(this).get("/projects", new ProjectsParser("result/projects/project"));
}
/**
* Get the definition of a single project, identified by the given name
*
* @param projectName name of the project - mandatory
* @return a {@link RundeckProject} instance
* @throws RundeckApiException in case of error when calling the API
* @throws RundeckApiLoginException if the login failed
* @throws IllegalArgumentException if the projectName is blank (null, empty or whitespace)
*/
public RundeckProject getProject(String projectName) throws RundeckApiException, RundeckApiLoginException,
IllegalArgumentException {
AssertUtil.notBlank(projectName, "projectName is mandatory to get the details of a project !");
return new ApiCall(this).get("/project/" + projectName, new ProjectParser("result/projects/project"));
}
public String getUrl() {
return url;
}

View file

@ -0,0 +1,70 @@
package org.rundeck.api.domain;
import java.io.Serializable;
/**
* Represents a RunDeck project
*
* @author Vincent Behar
*/
public class RundeckProject implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "RundeckProject [name=" + name + ", description=" + description + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((name == null) ? 0 : name.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;
RundeckProject other = (RundeckProject) obj;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View file

@ -0,0 +1,40 @@
package org.rundeck.api.parser;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Node;
import org.rundeck.api.domain.RundeckProject;
/**
* Parser for a single {@link RundeckProject}
*
* @author Vincent Behar
*/
public class ProjectParser implements NodeParser<RundeckProject> {
private String xpath;
public ProjectParser() {
super();
}
/**
* @param xpath of the project element if it is not the root node
*/
public ProjectParser(String xpath) {
super();
this.xpath = xpath;
}
@Override
public RundeckProject parseNode(Node node) {
Node projectNode = xpath != null ? node.selectSingleNode(xpath) : node;
RundeckProject project = new RundeckProject();
project.setName(StringUtils.trimToNull(projectNode.valueOf("name")));
project.setDescription(StringUtils.trimToNull(projectNode.valueOf("description")));
return project;
}
}

View file

@ -0,0 +1,40 @@
package org.rundeck.api.parser;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Node;
import org.rundeck.api.domain.RundeckProject;
/**
* Parser for a {@link List} of {@link RundeckProject}
*
* @author Vincent Behar
*/
public class ProjectsParser implements NodeParser<List<RundeckProject>> {
private final String xpath;
/**
* @param xpath of the projects elements
*/
public ProjectsParser(String xpath) {
super();
this.xpath = xpath;
}
@Override
public List<RundeckProject> parseNode(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);
projects.add(project);
}
return projects;
}
}

View file

@ -0,0 +1,27 @@
package org.rundeck.api.parser;
import java.io.InputStream;
import org.dom4j.Document;
import org.junit.Assert;
import org.junit.Test;
import org.rundeck.api.domain.RundeckProject;
/**
* Test the {@link ProjectParser}
*
* @author Vincent Behar
*/
public class ProjectParserTest {
@Test
public void parseNode() throws Exception {
InputStream input = getClass().getResourceAsStream("project.xml");
Document document = ParserHelper.loadDocument(input);
RundeckProject project = new ProjectParser("result/projects/project").parseNode(document);
Assert.assertEquals("test", project.getName());
Assert.assertEquals("test project", project.getDescription());
}
}

View file

@ -0,0 +1,34 @@
package org.rundeck.api.parser;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.junit.Assert;
import org.junit.Test;
import org.rundeck.api.domain.RundeckProject;
/**
* Test the {@link ProjectsParser}
*
* @author Vincent Behar
*/
public class ProjectsParserTest {
@Test
public void parseNode() throws Exception {
InputStream input = getClass().getResourceAsStream("projects.xml");
Document document = ParserHelper.loadDocument(input);
List<RundeckProject> projects = new ProjectsParser("result/projects/project").parseNode(document);
Assert.assertEquals(2, projects.size());
RundeckProject project1 = projects.get(0);
Assert.assertEquals("test", project1.getName());
Assert.assertEquals("test project", project1.getDescription());
RundeckProject project2 = projects.get(1);
Assert.assertEquals("other", project2.getName());
Assert.assertEquals(null, project2.getDescription());
}
}

View file

@ -0,0 +1 @@
<result success='true' apiversion='1'><projects count='1'><project><name>test</name><description>test project</description></project></projects></result>

View file

@ -0,0 +1 @@
<result success='true' apiversion='1'><projects count='2'><project><name>test</name><description>test project</description></project><project><name>other</name><description></description></project></projects></result>