*
* // using login-based authentication :
* RundeckClient rundeck = new RundeckClient("http://localhost:4440", "admin", "admin");
* // or for a token-based authentication :
* RundeckClient rundeck = new RundeckClient("http://localhost:4440", "PDDNKo5VE29kpk4prOUDr2rsKdRkEvsD");
*
* List<RundeckProject> projects = rundeck.getProjects();
*
* RundeckJob job = rundeck.findJob("my-project", "main-group/sub-group", "job-name");
* RundeckExecution execution = rundeck.triggerJob(job.getId(),
* new OptionsBuilder().addOption("version", "1.2.0").toProperties());
*
* List<RundeckExecution> runningExecutions = rundeck.getRunningExecutions("my-project");
*
* rundeck.exportJobsToFile("/tmp/jobs.xml", FileType.XML, "my-project");
* rundeck.importJobs("/tmp/jobs.xml", FileType.XML);
*
*
*
* @author Vincent Behar
*/
public class RundeckClient implements Serializable {
private static final long serialVersionUID = 1L;
/** Version of the API supported */
public static final transient int API_VERSION = 2;
/** End-point of the API */
public static final transient String API_ENDPOINT = "/api/" + API_VERSION;
/** Default value for the "pooling interval" used when running jobs/commands/scripts */
private static final transient long DEFAULT_POOLING_INTERVAL = 5;
/** Default unit of the "pooling interval" used when running jobs/commands/scripts */
private static final transient TimeUnit DEFAULT_POOLING_UNIT = TimeUnit.SECONDS;
/** URL of the RunDeck instance ("http://localhost:4440", "http://rundeck.your-compagny.com/", etc) */
private final String url;
/** Auth-token for authentication (if not using login-based auth) */
private final String token;
/** Login to use for authentication on the RunDeck instance (if not using token-based auth) */
private final String login;
/** Password to use for authentication on the RunDeck instance (if not using token-based auth) */
private final String password;
/**
* Instantiate a new {@link RundeckClient} for the RunDeck instance at the given url, using login-based
* authentication.
*
* @param url of the RunDeck instance ("http://localhost:4440", "http://rundeck.your-compagny.com/", etc)
* @param login to use for authentication on the RunDeck instance
* @param password to use for authentication on the RunDeck instance
* @throws IllegalArgumentException if the url, login or password is blank (null, empty or whitespace)
*/
public RundeckClient(String url, String login, String password) throws IllegalArgumentException {
super();
AssertUtil.notBlank(url, "The RunDeck URL is mandatory !");
AssertUtil.notBlank(login, "The RunDeck login is mandatory !");
AssertUtil.notBlank(password, "The RunDeck password is mandatory !");
this.url = url;
this.login = login;
this.password = password;
this.token = null;
}
/**
* Instantiate a new {@link RundeckClient} for the RunDeck instance at the given url, using token-based
* authentication.
*
* @param url of the RunDeck instance ("http://localhost:4440", "http://rundeck.your-compagny.com/", etc)
* @param token to use for authentication on the RunDeck instance
* @throws IllegalArgumentException if the url or token is blank (null, empty or whitespace)
*/
public RundeckClient(String url, String token) throws IllegalArgumentException {
super();
AssertUtil.notBlank(url, "The RunDeck URL is mandatory !");
AssertUtil.notBlank(token, "The RunDeck auth-token is mandatory !");
this.url = url;
this.token = token;
this.login = null;
this.password = null;
}
/**
* Try to "ping" the RunDeck instance to see if it is alive
*
* @throws RundeckApiException if the ping fails
*/
public void ping() throws RundeckApiException {
new PingRequest(this).execute();
}
/**
* Test the authentication on the RunDeck instance.
*
* @throws RundeckApiLoginException if the login fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
*/
public void testAuth() throws RundeckApiLoginException, RundeckApiTokenException {
new AuthRequest(this).execute();
}
/**
* @deprecated Use {@link #testAuth()}
* @see #testAuth()
*/
@Deprecated
public void testCredentials() throws RundeckApiLoginException, RundeckApiTokenException {
testAuth();
}
/*
* New-style API
*/
public ProjectsListingRequest newProjectsListingRequest() {
return new ProjectsListingRequest(this);
}
public ProjectDetailsRequest newProjectDetailsRequest(String projectName) {
return new ProjectDetailsRequest(this, projectName);
}
public JobsListingRequest newJobsListingRequest(String project) {
return new JobsListingRequest(this, project);
}
public JobTriggerRequest newJobTriggerRequest(String jobId) {
return new JobTriggerRequest(this, jobId);
}
public JobRunRequest newJobRunRequest(String jobId) {
return new JobRunRequest(this, jobId);
}
public HistoryRequest newHistoryRequest(String project) {
return new HistoryRequest(this, project);
}
/*
* Old-style API
*/
/*
* Projects
*/
/**
* 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 fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
*/
public List