*
* // 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 = 5;
/** 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;
private String sessionID;
/**
* 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 or session-based authentication. Either token or sessionID must be valid
*
* @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
* @param sessionID to use for session authentication on the RunDeck instance
* @param useToken should be true if using token, false if using sessionID
* @throws IllegalArgumentException if the url or token is blank (null, empty or whitespace)
*/
public RundeckClient(String url, String token, String sessionID, boolean useToken) throws IllegalArgumentException {
super();
AssertUtil.notBlank(url, "The RunDeck URL is mandatory !");
if(useToken){
AssertUtil.notBlank(token, "Token is mandatory!");
this.token = token;
this.sessionID = null;
}
else {
AssertUtil.notBlank(sessionID, "sessionID is mandatory!");
this.sessionID = sessionID;
this.token = null;
}
this.url = url;
this.login = null;
this.password = null;
}
public RundeckClient(String url, String token) throws IllegalArgumentException {
this(url, token, null, true);
}
/**
* Try to "ping" the RunDeck instance to see if it is alive
*
* @throws RundeckApiException if the ping fails
*/
public void ping() throws RundeckApiException {
new ApiCall(this).ping();
}
/**
* Test the authentication on the RunDeck instance.
*
* @return sessionID if doing username+password login and it succeeded
* @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 String testAuth() throws RundeckApiLoginException, RundeckApiTokenException {
return (new ApiCall(this)).testAuth();
}
/**
* @deprecated Use {@link #testAuth()}
* @see #testAuth()
*/
@Deprecated
public void testCredentials() throws RundeckApiLoginException, RundeckApiTokenException {
testAuth();
}
/*
* 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