Use builder based arguments for methods

* Replace triggerAdhocScript/Command/Job
* Add RunAdhocX interfaces
* Add RunJob interface and builder
This commit is contained in:
Greg Schueler 2013-07-09 10:22:57 -07:00
parent 239b9ea0ee
commit bbcfd1cff7
13 changed files with 853 additions and 44 deletions

View file

@ -0,0 +1,54 @@
package org.rundeck.api;
import java.util.Properties;
/**
* Base class for adhoc requests
*/
class DefaultRunAdhoc implements RunAdhoc{
private String project;
private Properties nodeFilters;
private Integer nodeThreadcount;
private Boolean nodeKeepgoing;
private String asUser;
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public Properties getNodeFilters() {
return nodeFilters;
}
public void setNodeFilters(Properties nodeFilters) {
this.nodeFilters = nodeFilters;
}
public Integer getNodeThreadcount() {
return nodeThreadcount;
}
public void setNodeThreadcount(Integer nodeThreadcount) {
this.nodeThreadcount = nodeThreadcount;
}
public Boolean getNodeKeepgoing() {
return nodeKeepgoing;
}
public void setNodeKeepgoing(Boolean nodeKeepgoing) {
this.nodeKeepgoing = nodeKeepgoing;
}
public String getAsUser() {
return asUser;
}
public void setAsUser(String asUser) {
this.asUser = asUser;
}
}

View file

@ -0,0 +1,18 @@
package org.rundeck.api;
/**
* Implementation of {@link RunAdhocCommand}
*/
class DefaultRunAdhocCommand extends DefaultRunAdhoc implements RunAdhocCommand {
private String command;
@Override
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
}

View file

@ -0,0 +1,28 @@
package org.rundeck.api;
import java.io.InputStream;
/**
* Bean implementing {@link RunAdhocScript}
*/
class DefaultRunAdhocScript extends DefaultRunAdhoc implements RunAdhocScript {
private InputStream script;
private String argString;
@Override
public InputStream getScript() {
return script;
}
public void setScript(InputStream script) {
this.script = script;
}
public String getArgString() {
return argString;
}
public void setArgString(String argString) {
this.argString = argString;
}
}

View file

@ -0,0 +1,50 @@
package org.rundeck.api;
import java.util.Properties;
/**
* impl of {@link RunJob}
*/
class DefaultRunJob implements RunJob {
private String jobId;
private Properties options;
private Properties nodeFilters;
private String asUser;
@Override
public String getJobId() {
return jobId;
}
@Override
public Properties getOptions() {
return options;
}
@Override
public Properties getNodeFilters() {
return nodeFilters;
}
@Override
public String getAsUser() {
return asUser;
}
public void setJobId(String jobId) {
this.jobId = jobId;
}
public void setOptions(Properties options) {
this.options = options;
}
public void setNodeFilters(Properties nodeFilters) {
this.nodeFilters = nodeFilters;
}
public void setAsUser(String asUser) {
this.asUser = asUser;
}
}

View file

@ -0,0 +1,38 @@
package org.rundeck.api;
import java.util.Properties;
/**
* Super interface for adhoc executions
*/
public interface RunAdhoc {
/**
* Project name
* @return
*/
String getProject();
/**
* Node filters
* @return
*/
Properties getNodeFilters();
/**
* Threadcount
* @return
*/
Integer getNodeThreadcount();
/**
* Keepgoing
* @return
*/
Boolean getNodeKeepgoing();
/**
* As User
* @return
*/
String getAsUser();
}

View file

@ -0,0 +1,14 @@
package org.rundeck.api;
/**
* A command to execute
*/
public interface RunAdhocCommand extends RunAdhoc {
/**
* Command to execute
* @return
*/
String getCommand();
}

View file

@ -0,0 +1,55 @@
package org.rundeck.api;
import java.util.Properties;
/**
* $INTERFACE is ... User: greg Date: 7/9/13 Time: 10:38 AM
*/
public class RunAdhocCommandBuilder {
private DefaultRunAdhocCommand command;
public RunAdhocCommandBuilder() {
command = new DefaultRunAdhocCommand();
}
public static RunAdhocCommandBuilder builder() {
return new RunAdhocCommandBuilder();
}
public RunAdhocCommandBuilder setProject(String project) {
command.setProject(project);
return this;
}
public RunAdhocCommandBuilder setCommand(String commandString) {
command.setCommand(commandString);
return this;
}
public RunAdhocCommandBuilder setNodeFilters(Properties nodeFilters) {
command.setNodeFilters(nodeFilters);
return this;
}
public RunAdhocCommandBuilder setNodeThreadcount(Integer nodeThreadcount) {
command.setNodeThreadcount(nodeThreadcount);
return this;
}
public RunAdhocCommandBuilder setNodeKeepgoing(Boolean nodeKeepgoing) {
command.setNodeKeepgoing(nodeKeepgoing);
return this;
}
public RunAdhocCommandBuilder setAsUser(String asUser) {
command.setAsUser(asUser);
return this;
}
public RunAdhocCommand create() {
DefaultRunAdhocCommand built = command;
command = new DefaultRunAdhocCommand();
return built;
}
}

View file

@ -0,0 +1,22 @@
package org.rundeck.api;
import java.io.InputStream;
/**
* An adhoc script to be executed by Rundeck
*/
public interface RunAdhocScript extends RunAdhoc {
/**
* Stream containing script
* @return
*/
InputStream getScript();
/**
* Arguments to the script
* @return
*/
String getArgString();
}

View file

@ -0,0 +1,62 @@
package org.rundeck.api;
import java.io.InputStream;
import java.util.Properties;
/**
* A builder to create a {@link RunAdhocScript}, use the {@link #builder()} to create a builder, then {@link #create()} to
* build an RunAdhocScript
*/
public class RunAdhocScriptBuilder {
private DefaultRunAdhocScript script;
public RunAdhocScriptBuilder() {
script = new DefaultRunAdhocScript();
}
public static RunAdhocScriptBuilder builder() {
return new RunAdhocScriptBuilder();
}
public RunAdhocScriptBuilder setProject(String project) {
script.setProject(project);
return this;
}
public RunAdhocScriptBuilder setScript(InputStream stream) {
script.setScript(stream);
return this;
}
public RunAdhocScriptBuilder setArgString(String argString) {
script.setArgString(argString);
return this;
}
public RunAdhocScriptBuilder setNodeFilters(Properties nodeFilters) {
script.setNodeFilters(nodeFilters);
return this;
}
public RunAdhocScriptBuilder setNodeThreadcount(Integer nodeThreadcount) {
script.setNodeThreadcount(nodeThreadcount);
return this;
}
public RunAdhocScriptBuilder setNodeKeepgoing(Boolean nodeKeepgoing) {
script.setNodeKeepgoing(nodeKeepgoing);
return this;
}
public RunAdhocScriptBuilder setAsUser(String asUser) {
script.setAsUser(asUser);
return this;
}
public RunAdhocScript create() {
DefaultRunAdhocScript built = script;
script = new DefaultRunAdhocScript();
return built;
}
}

View file

@ -0,0 +1,32 @@
package org.rundeck.api;
import java.util.Properties;
/**
* Job run request
*/
public interface RunJob {
/**
* Job ID
* @return
*/
String getJobId();
/**
* Options to job
* @return
*/
Properties getOptions();
/**
* Node filters
* @return
*/
Properties getNodeFilters();
/**
* as User
* @return
*/
String getAsUser();
}

View file

@ -0,0 +1,44 @@
package org.rundeck.api;
import java.util.Properties;
/**
* Job run request builder
*/
public class RunJobBuilder {
private DefaultRunJob jobRun;
public static RunJobBuilder builder() {
return new RunJobBuilder();
}
public RunJobBuilder() {
jobRun = new DefaultRunJob();
}
public RunJobBuilder setJobId(String jobId) {
jobRun.setJobId(jobId);
return this;
}
public RunJobBuilder setOptions(Properties options) {
jobRun.setOptions(options);
return this;
}
public RunJobBuilder setNodeFilters(Properties nodeFilters) {
jobRun.setNodeFilters(nodeFilters);
return this;
}
public RunJobBuilder setAsUser(String asUser) {
jobRun.setAsUser(asUser);
return this;
}
public RunJob create() {
RunJob built = jobRun;
jobRun = new DefaultRunJob();
return built;
}
}

View file

@ -40,7 +40,11 @@ import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
* Main entry point to talk to a RunDeck instance.<br> * Main entry point to talk to a RunDeck instance.
* <br>
* <em>Deprecation Warning:</em> The "triggerAdhocScript" methods which take multiple arguments are deprecated in
* favor of {@link #triggerAdhocScript(RunAdhocScript)}. The methods will be removed in version 10 of this library.
* <br>
* You have 2 methods for authentication : login-based or token-based. If you want to use the first, you need to provide * You have 2 methods for authentication : login-based or token-based. If you want to use the first, you need to provide
* both a "login" and a "password". Otherwise, just provide a "token" (also called "auth-token"). See the RunDeck * both a "login" and a "password". Otherwise, just provide a "token" (also called "auth-token"). See the RunDeck
* documentation for generating such a token.<br> * documentation for generating such a token.<br>
@ -176,7 +180,7 @@ public class RundeckClient implements Serializable {
* @param sessionID to use for session 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 * @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) * @throws IllegalArgumentException if the url or token is blank (null, empty or whitespace)
* @deprecated Use the builder {@link RundeckClientBuilder} * @deprecated Use the builder {@link RundeckClientBuilder}, this method will not be public in version 10 of this library.
*/ */
public RundeckClient(String url, String token, String sessionID, boolean useToken) throws IllegalArgumentException { public RundeckClient(String url, String token, String sessionID, boolean useToken) throws IllegalArgumentException {
this(url); this(url);
@ -872,6 +876,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties, Properties) * @see #triggerJob(String, Properties, Properties)
* @see #runJob(String) * @see #runJob(String)
* @deprecated use {@link #triggerJob(RunJob)}, this method will be removed in version 10 of this library
*/ */
public RundeckExecution triggerJob(String jobId) throws RundeckApiException, RundeckApiLoginException, public RundeckExecution triggerJob(String jobId) throws RundeckApiException, RundeckApiLoginException,
RundeckApiTokenException, IllegalArgumentException { RundeckApiTokenException, IllegalArgumentException {
@ -891,6 +896,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties, Properties) * @see #triggerJob(String, Properties, Properties)
* @see #runJob(String, Properties) * @see #runJob(String, Properties)
* @deprecated use {@link #triggerJob(RunJob)}, this method will be removed in version 10 of this library
*/ */
public RundeckExecution triggerJob(String jobId, Properties options) throws RundeckApiException, public RundeckExecution triggerJob(String jobId, Properties options) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -912,6 +918,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String) * @see #triggerJob(String)
* @see #runJob(String, Properties, Properties) * @see #runJob(String, Properties, Properties)
* @deprecated use {@link #triggerJob(RunJob)}, this method will be removed in version 10 of this library
*/ */
public RundeckExecution triggerJob(String jobId, Properties options, Properties nodeFilters) public RundeckExecution triggerJob(String jobId, Properties options, Properties nodeFilters)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -933,15 +940,42 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String) * @see #triggerJob(String)
* @see #runJob(String, Properties, Properties) * @see #runJob(String, Properties, Properties)
* @deprecated use {@link #triggerJob(RunJob)}, this method will be removed in version 10 of this library
*/ */
public RundeckExecution triggerJob(String jobId, Properties options, Properties nodeFilters, String asUser) public RundeckExecution triggerJob(String jobId, Properties options, Properties nodeFilters, String asUser)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(jobId, "jobId is mandatory to trigger a job !"); return triggerJob(RunJobBuilder.builder()
ApiPathBuilder apiPath = new ApiPathBuilder("/job/", jobId, "/run").param("argString", .setJobId(jobId)
ParametersUtil.generateArgString(options)) .setOptions(options)
.nodeFilters(nodeFilters); .setNodeFilters(nodeFilters)
if(null!=asUser) { .setAsUser(asUser)
apiPath.param("asUser", asUser); .create());
}
/**
* Trigger the execution of a RunDeck job (identified by the given ID), and return immediately (without waiting the
* end of the job execution)
*
* @param jobId identifier of the job - mandatory
* @param options of the job - optional. See {@link OptionsBuilder}.
* @param nodeFilters for overriding the nodes on which the job will be executed - optional. See
* {@link NodeFiltersBuilder}
* @param asUser specify a user name to run the job as, must have 'runAs' permission
* @return a {@link RundeckExecution} instance for the newly created (and running) execution - won't be null
* @throws RundeckApiException in case of error when calling the API (non-existent job with this ID)
* @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)
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String)
* @see #runJob(String, Properties, Properties)
*/
public RundeckExecution triggerJob(RunJob jobRun)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(jobRun.getJobId(), "jobId is mandatory to trigger a job !");
ApiPathBuilder apiPath = new ApiPathBuilder("/job/", jobRun.getJobId(), "/run").param("argString",
ParametersUtil.generateArgString(jobRun.getOptions()))
.nodeFilters(jobRun.getNodeFilters());
if(null!=jobRun.getAsUser()) {
apiPath.param("asUser", jobRun.getAsUser());
} }
return new ApiCall(this).get(apiPath, new ExecutionParser("result/executions/execution")); return new ApiCall(this).get(apiPath, new ExecutionParser("result/executions/execution"));
} }
@ -959,6 +993,8 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String) * @see #triggerJob(String)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)},
* this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId) throws RundeckApiException, RundeckApiLoginException, public RundeckExecution runJob(String jobId) throws RundeckApiException, RundeckApiLoginException,
RundeckApiTokenException, IllegalArgumentException { RundeckApiTokenException, IllegalArgumentException {
@ -979,6 +1015,8 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties) * @see #triggerJob(String, Properties)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)},
* this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId, Properties options) throws RundeckApiException, public RundeckExecution runJob(String jobId, Properties options) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1001,12 +1039,34 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties, Properties) * @see #triggerJob(String, Properties, Properties)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)},
* this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters) public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
return runJob(jobId, options, nodeFilters, DEFAULT_POOLING_INTERVAL, DEFAULT_POOLING_UNIT); return runJob(jobId, options, nodeFilters, DEFAULT_POOLING_INTERVAL, DEFAULT_POOLING_UNIT);
} }
/**
* Run a RunDeck job (identified by the given ID), and wait until its execution is finished (or aborted) to return.
* We will poll the RunDeck server at regular interval (every 5 seconds) to know if the execution is finished (or
* aborted) or is still running.
*
* @param runJob the job run parameters
*
* @return a {@link RundeckExecution} instance for the (finished/aborted) execution - won't be null
*
* @throws RundeckApiException in case of error when calling the API (non-existent job with this ID)
* @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)
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(RunJob)
* @see #runJob(RunJob, long, TimeUnit)
*/
public RundeckExecution runJob(final RunJob runJob) throws RundeckApiException, RundeckApiLoginException,
RundeckApiTokenException, IllegalArgumentException {
return runJob(runJob, DEFAULT_POOLING_INTERVAL, DEFAULT_POOLING_UNIT);
}
/** /**
* Run a RunDeck job (identified by the given ID), and wait until its execution is finished (or aborted) to return. * Run a RunDeck job (identified by the given ID), and wait until its execution is finished (or aborted) to return.
* We will poll the RunDeck server at regular interval (configured by the poolingInterval/poolingUnit couple) to * We will poll the RunDeck server at regular interval (configured by the poolingInterval/poolingUnit couple) to
@ -1023,6 +1083,8 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties) * @see #triggerJob(String, Properties)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)},
* this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId, Properties options, long poolingInterval, TimeUnit poolingUnit) public RundeckExecution runJob(String jobId, Properties options, long poolingInterval, TimeUnit poolingUnit)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1047,6 +1109,8 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties) * @see #triggerJob(String, Properties)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)},
* this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters, long poolingInterval, public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters, long poolingInterval,
TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException,
@ -1072,10 +1136,44 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(String, Properties) * @see #triggerJob(String, Properties)
* @see #runJob(String, Properties, Properties, long, TimeUnit) * @see #runJob(String, Properties, Properties, long, TimeUnit)
* @deprecated use {@link #runJob(RunJob, long, java.util.concurrent.TimeUnit)}, this method will be removed in version 10 of this library
*/ */
public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters, String asUser, long poolingInterval, public RundeckExecution runJob(String jobId, Properties options, Properties nodeFilters, String asUser, long poolingInterval,
TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException,
IllegalArgumentException { IllegalArgumentException {
return runJob(RunJobBuilder.builder()
.setJobId(jobId)
.setOptions(options)
.setNodeFilters(nodeFilters)
.setAsUser(asUser)
.create(), poolingInterval, poolingUnit);
}
/**
* Run a RunDeck job (identified by the given ID), and wait until its execution is finished (or aborted) to return.
* We will poll the RunDeck server at regular interval (configured by the poolingInterval/poolingUnit couple) to
* know if the execution is finished (or aborted) or is still running.
*
* @param jobId identifier of the job - mandatory
* @param options of the job - optional. See {@link OptionsBuilder}.
* @param nodeFilters for overriding the nodes on which the job will be executed - optional. See {@link
* NodeFiltersBuilder}
* @param asUser specify a user name to run the job as, must have 'runAs' permission
* @param poolingInterval for checking the status of the execution. Must be > 0.
* @param poolingUnit unit (seconds, milli-seconds, ...) of the interval. Default to seconds.
*
* @return a {@link RundeckExecution} instance for the (finished/aborted) execution - won't be null
*
* @throws RundeckApiException in case of error when calling the API (non-existent job with this ID)
* @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)
* @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace)
* @see #triggerJob(RunJob)
*/
public RundeckExecution runJob(final RunJob jobRun, long poolingInterval,
TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException,
IllegalArgumentException {
if (poolingInterval <= 0) { if (poolingInterval <= 0) {
poolingInterval = DEFAULT_POOLING_INTERVAL; poolingInterval = DEFAULT_POOLING_INTERVAL;
poolingUnit = DEFAULT_POOLING_UNIT; poolingUnit = DEFAULT_POOLING_UNIT;
@ -1084,7 +1182,7 @@ public class RundeckClient implements Serializable {
poolingUnit = DEFAULT_POOLING_UNIT; poolingUnit = DEFAULT_POOLING_UNIT;
} }
RundeckExecution execution = triggerJob(jobId, options, nodeFilters, asUser); RundeckExecution execution = triggerJob(jobRun);
while (ExecutionStatus.RUNNING.equals(execution.getStatus())) { while (ExecutionStatus.RUNNING.equals(execution.getStatus())) {
try { try {
Thread.sleep(poolingUnit.toMillis(poolingInterval)); Thread.sleep(poolingUnit.toMillis(poolingInterval));
@ -1113,6 +1211,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace) * @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace)
* @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean) * @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean)
* @see #runAdhocCommand(String, String) * @see #runAdhocCommand(String, String)
* @deprecated use {@link #triggerAdhocCommand(RunAdhocCommand)}, will be removed in version 10 of this library
*/ */
public RundeckExecution triggerAdhocCommand(String project, String command) throws RundeckApiException, public RundeckExecution triggerAdhocCommand(String project, String command) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1133,6 +1232,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace) * @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace)
* @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean) * @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean)
* @see #runAdhocCommand(String, String, Properties) * @see #runAdhocCommand(String, String, Properties)
* @deprecated use {@link #triggerAdhocCommand(RunAdhocCommand)}, will be removed in version 10 of this library
*/ */
public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters) public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1155,6 +1255,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace) * @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace)
* @see #triggerAdhocCommand(String, String) * @see #triggerAdhocCommand(String, String)
* @see #runAdhocCommand(String, String, Properties) * @see #runAdhocCommand(String, String, Properties)
* @deprecated use {@link #triggerAdhocCommand(RunAdhocCommand)}, will be removed in version 10 of this library
*/ */
public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters, public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters,
Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException, RundeckApiLoginException, Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException, RundeckApiLoginException,
@ -1178,21 +1279,51 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace) * @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace)
* @see #triggerAdhocCommand(String, String) * @see #triggerAdhocCommand(String, String)
* @see #runAdhocCommand(String, String, Properties) * @see #runAdhocCommand(String, String, Properties)
* @deprecated use {@link #triggerAdhocCommand(RunAdhocCommand)}, will be removed in version 10 of this library
*/ */
public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters, public RundeckExecution triggerAdhocCommand(String project, String command, Properties nodeFilters,
Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException, RundeckApiLoginException, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException, RundeckApiLoginException,
RundeckApiTokenException, IllegalArgumentException { RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(project, "project is mandatory to trigger an ad-hoc command !"); return triggerAdhocCommand(RunAdhocCommandBuilder.builder()
AssertUtil.notBlank(command, "command is mandatory to trigger an ad-hoc command !"); .setProject(project)
ApiPathBuilder apiPath = new ApiPathBuilder("/run/command").param("project", project) .setCommand(command)
.param("exec", command) .setNodeFilters(nodeFilters)
.setNodeThreadcount(nodeThreadcount)
.setNodeKeepgoing(nodeKeepgoing)
.setAsUser(asUser)
.create());
}
/**
* Trigger the execution of an ad-hoc command, and return immediately (without waiting the end of the execution).
* The command will be dispatched to nodes, accordingly to the nodeFilters parameter.
*
* @param project name of the project - mandatory
* @param command to be executed - mandatory
* @param nodeFilters for selecting nodes on which the command will be executed. See {@link NodeFiltersBuilder}
* @param nodeThreadcount thread count to use (for parallelizing when running on multiple nodes) - optional
* @param nodeKeepgoing if true, continue executing on other nodes even if some fail - optional
* @param asUser specify a user name to run the job as, must have 'runAs' permission
* @return a {@link RundeckExecution} instance for the newly created (and running) execution - 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 fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
* @throws IllegalArgumentException if the project or command is blank (null, empty or whitespace)
* @see #triggerAdhocCommand(String, String)
* @see #runAdhocCommand(String, String, Properties)
*/
public RundeckExecution triggerAdhocCommand(RunAdhocCommand command) throws RundeckApiException, RundeckApiLoginException,
RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(command.getProject(), "project is mandatory to trigger an ad-hoc command !");
AssertUtil.notBlank(command.getCommand(), "command is mandatory to trigger an ad-hoc command !");
ApiPathBuilder apiPath = new ApiPathBuilder("/run/command").param("project", command.getProject())
.param("exec", command.getCommand())
.param("nodeThreadcount", .param("nodeThreadcount",
nodeThreadcount) command.getNodeThreadcount())
.param("nodeKeepgoing", .param("nodeKeepgoing",
nodeKeepgoing) command.getNodeKeepgoing())
.nodeFilters(nodeFilters); .nodeFilters(command.getNodeFilters());
if(null!=asUser) { if(null!= command.getAsUser()) {
apiPath.param("asUser", asUser); apiPath.param("asUser", command.getAsUser());
} }
RundeckExecution execution = new ApiCall(this).get(apiPath, new ExecutionParser("result/execution")); RundeckExecution execution = new ApiCall(this).get(apiPath, new ExecutionParser("result/execution"));
// the first call just returns the ID of the execution, so we need another call to get a "real" execution // the first call just returns the ID of the execution, so we need another call to get a "real" execution
@ -1404,6 +1535,7 @@ public class RundeckClient implements Serializable {
* @throws IOException if we failed to read the file * @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String) * @see #runAdhocScript(String, String)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, String scriptFilename) throws RundeckApiException, public RundeckExecution triggerAdhocScript(String project, String scriptFilename) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException {
@ -1425,6 +1557,7 @@ public class RundeckClient implements Serializable {
* @throws IOException if we failed to read the file * @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String, Properties) * @see #runAdhocScript(String, String, Properties)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options) public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException,
@ -1448,6 +1581,7 @@ public class RundeckClient implements Serializable {
* @throws IOException if we failed to read the file * @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String, Properties, Properties) * @see #runAdhocScript(String, String, Properties, Properties)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options, public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options,
Properties nodeFilters) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, Properties nodeFilters) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException,
@ -1473,6 +1607,7 @@ public class RundeckClient implements Serializable {
* @throws IOException if we failed to read the file * @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String, Properties, Properties, Integer, Boolean, long, TimeUnit) * @see #runAdhocScript(String, String, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options, public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException,
@ -1497,15 +1632,50 @@ public class RundeckClient implements Serializable {
* @throws IOException if we failed to read the file * @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String, Properties, Properties, Integer, Boolean, long, TimeUnit) * @see #runAdhocScript(String, String, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options, public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing,String asUser) throws RundeckApiException, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing,String asUser) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException {
return triggerAdhocScript(project, scriptFilename, ParametersUtil.generateArgString(options), nodeFilters,
nodeThreadcount, nodeKeepgoing, asUser);
}
/**
* Trigger the execution of an ad-hoc script, and return immediately (without waiting the end of the execution). The
* script will be dispatched to nodes, accordingly to the nodeFilters parameter.
*
* @param project name of the project - mandatory
* @param scriptFilename filename of the script to be executed - mandatory
* @param options of the script - optional. See {@link OptionsBuilder}.
* @param nodeFilters for selecting nodes on which the command will be executed. See {@link NodeFiltersBuilder}
* @param nodeThreadcount thread count to use (for parallelizing when running on multiple nodes) - optional
* @param nodeKeepgoing if true, continue executing on other nodes even if some fail - optional
* @return a {@link RundeckExecution} instance for the newly created (and running) execution - 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 fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
* @throws IllegalArgumentException if the project or scriptFilename is blank (null, empty or whitespace)
* @throws IOException if we failed to read the file
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, String, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/
public RundeckExecution triggerAdhocScript(String project, String scriptFilename, String argString,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing,String asUser) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException {
AssertUtil.notBlank(scriptFilename, "scriptFilename is mandatory to trigger an ad-hoc script !"); AssertUtil.notBlank(scriptFilename, "scriptFilename is mandatory to trigger an ad-hoc script !");
FileInputStream stream = null; FileInputStream stream = null;
try { try {
stream = FileUtils.openInputStream(new File(scriptFilename)); stream = FileUtils.openInputStream(new File(scriptFilename));
return triggerAdhocScript(project, stream, options, nodeFilters, nodeThreadcount, nodeKeepgoing, asUser); return triggerAdhocScript(RunAdhocScriptBuilder.builder()
.setProject(project)
.setScript(stream)
.setNodeFilters(nodeFilters)
.setArgString(argString)
.setNodeThreadcount(nodeThreadcount)
.setNodeKeepgoing(nodeKeepgoing)
.setAsUser(asUser)
.create());
} finally { } finally {
IOUtils.closeQuietly(stream); IOUtils.closeQuietly(stream);
} }
@ -1524,6 +1694,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream) * @see #runAdhocScript(String, InputStream)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, InputStream script) throws RundeckApiException, public RundeckExecution triggerAdhocScript(String project, InputStream script) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1544,6 +1715,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties) * @see #runAdhocScript(String, InputStream, Properties)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options) public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options)
throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
@ -1565,6 +1737,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties, Properties) * @see #runAdhocScript(String, InputStream, Properties, Properties)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options, public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options,
Properties nodeFilters) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, Properties nodeFilters) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException,
@ -1589,6 +1762,7 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit) * @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options, public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing) throws RundeckApiException,
@ -1612,24 +1786,79 @@ public class RundeckClient implements Serializable {
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null * @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit) * @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/ */
public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options, public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(project, "project is mandatory to trigger an ad-hoc script !"); return triggerAdhocScript(project, script, ParametersUtil.generateArgString(options), nodeFilters,
AssertUtil.notNull(script, "script is mandatory to trigger an ad-hoc script !"); nodeThreadcount, nodeKeepgoing, asUser);
ApiPathBuilder apiPath = new ApiPathBuilder("/run/script").param("project", project) }
/**
* Trigger the execution of an ad-hoc script, and return immediately (without waiting the end of the execution). The
* script will be dispatched to nodes, accordingly to the nodeFilters parameter.
*
* @param project name of the project - mandatory
* @param script inputStream for reading the script to be executed - mandatory
* @param options of the script - optional. See {@link OptionsBuilder}.
* @param nodeFilters for selecting nodes on which the command will be executed. See {@link NodeFiltersBuilder}
* @param nodeThreadcount thread count to use (for parallelizing when running on multiple nodes) - optional
* @param nodeKeepgoing if true, continue executing on other nodes even if some fail - optional
* @return a {@link RundeckExecution} instance for the newly created (and running) execution - 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 fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit)
* @deprecated use {@link #triggerAdhocScript(RunAdhocScript)}
*/
public RundeckExecution triggerAdhocScript(String project, InputStream script, String argString,
Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
return triggerAdhocScript(RunAdhocScriptBuilder.builder()
.setProject(project)
.setScript(script)
.setNodeFilters(nodeFilters)
.setArgString(argString)
.setNodeThreadcount(nodeThreadcount)
.setNodeKeepgoing(nodeKeepgoing)
.setAsUser(asUser)
.create());
}
/**
* Trigger the execution of an ad-hoc script, and return immediately (without waiting the end of the execution). The
* script will be dispatched to nodes, accordingly to the nodeFilters parameter.
*
* @param project name of the project - mandatory
* @param script inputStream for reading the script to be executed - mandatory
* @param options of the script - optional. See {@link OptionsBuilder}.
* @param nodeFilters for selecting nodes on which the command will be executed. See {@link NodeFiltersBuilder}
* @param nodeThreadcount thread count to use (for parallelizing when running on multiple nodes) - optional
* @param nodeKeepgoing if true, continue executing on other nodes even if some fail - optional
* @return a {@link RundeckExecution} instance for the newly created (and running) execution - 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 fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
* @throws IllegalArgumentException if the project is blank (null, empty or whitespace) or the script is null
* @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean)
* @see #runAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean, long, TimeUnit)
*/
public RundeckExecution triggerAdhocScript(RunAdhocScript adhocScript) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException {
AssertUtil.notBlank(adhocScript.getProject(), "project is mandatory to trigger an ad-hoc script !");
AssertUtil.notNull(adhocScript.getScript(), "script is mandatory to trigger an ad-hoc script !");
ApiPathBuilder apiPath = new ApiPathBuilder("/run/script").param("project", adhocScript.getProject())
.attach("scriptFile", .attach("scriptFile",
script) adhocScript.getScript())
.param("argString", .param("argString",adhocScript.getArgString())
ParametersUtil.generateArgString(options))
.param("nodeThreadcount", .param("nodeThreadcount",
nodeThreadcount) adhocScript.getNodeThreadcount())
.param("nodeKeepgoing", .param("nodeKeepgoing",
nodeKeepgoing) adhocScript.getNodeKeepgoing())
.nodeFilters(nodeFilters); .nodeFilters(adhocScript.getNodeFilters());
if(null!=asUser) { if(null!=adhocScript.getAsUser()) {
apiPath.param("asUser", asUser); apiPath.param("asUser", adhocScript.getAsUser());
} }
RundeckExecution execution = new ApiCall(this).post(apiPath, new ExecutionParser("result/execution")); RundeckExecution execution = new ApiCall(this).post(apiPath, new ExecutionParser("result/execution"));
// the first call just returns the ID of the execution, so we need another call to get a "real" execution // the first call just returns the ID of the execution, so we need another call to get a "real" execution

View file

@ -28,10 +28,7 @@ import org.rundeck.api.query.ExecutionQuery;
import org.rundeck.api.util.PagedResults; import org.rundeck.api.util.PagedResults;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/** /**
@ -347,13 +344,13 @@ public class RundeckClientTest {
final RundeckJobDelete delete = deleteTest.getResults().get(0); final RundeckJobDelete delete = deleteTest.getResults().get(0);
Assert.assertFalse(delete.isSuccessful()); Assert.assertFalse(delete.isSuccessful());
Assert.assertNotNull(delete.getError()); Assert.assertNotNull(delete.getError());
Assert.assertEquals("unauthorized",delete.getErrorCode()); Assert.assertEquals("unauthorized", delete.getErrorCode());
Assert.assertNull(delete.getMessage()); Assert.assertNull(delete.getMessage());
Assert.assertEquals("3a6d16be-4268-4d26-86a9-cebc1781f768", delete.getId()); Assert.assertEquals("3a6d16be-4268-4d26-86a9-cebc1781f768", delete.getId());
} }
@Test @Test
@Betamax(tape = "trigger_job_basic") @Betamax(tape = "trigger_job_basic")
public void triggerJobBasic() throws Exception { public void triggerJobDeprecatedBasic() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test final RundeckExecution test
@ -366,6 +363,38 @@ public class RundeckClientTest {
Assert.assertEquals("admin", test.getStartedBy()); Assert.assertEquals("admin", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus()); Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus());
}
@Test
@Betamax(tape = "trigger_job_basic")
public void triggerJobBasic() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test
= client.triggerJob(RunJobBuilder.builder().setJobId("3170ba0e-6093-4b58-94d2-52988aefbfc9").create());
Assert.assertEquals((Long) 19L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("echo hi there ${job.username} ; sleep 90", test.getDescription());
Assert.assertEquals("admin", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus());
}
@Test
@Betamax(tape = "trigger_job_as_user")
public void triggerJobDeprecatedAsUser() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test
= client.triggerJob("3170ba0e-6093-4b58-94d2-52988aefbfc9", null, null, "api-java-client-user-test1");
Assert.assertEquals((Long) 20L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("echo hi there ${job.username} ; sleep 90", test.getDescription());
Assert.assertEquals("api-java-client-user-test1", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus());
} }
@Test @Test
@Betamax(tape = "trigger_job_as_user") @Betamax(tape = "trigger_job_as_user")
@ -373,7 +402,10 @@ public class RundeckClientTest {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test final RundeckExecution test
= client.triggerJob("3170ba0e-6093-4b58-94d2-52988aefbfc9",null,null,"api-java-client-user-test1"); = client.triggerJob(RunJobBuilder.builder()
.setJobId("3170ba0e-6093-4b58-94d2-52988aefbfc9")
.setAsUser("api-java-client-user-test1")
.create());
Assert.assertEquals((Long)20L, test.getId()); Assert.assertEquals((Long)20L, test.getId());
Assert.assertEquals(null, test.getArgstring()); Assert.assertEquals(null, test.getArgstring());
@ -385,7 +417,7 @@ public class RundeckClientTest {
} }
@Test @Test
@Betamax(tape = "trigger_job_as_user_unauthorized") @Betamax(tape = "trigger_job_as_user_unauthorized")
public void triggerJobAsUserUnauthorized() throws Exception { public void triggerJobDeprecatedAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test; final RundeckExecution test;
@ -396,10 +428,26 @@ public class RundeckClientTest {
Assert.assertEquals("Not authorized for action \"Run as User\" for Job ID 3170ba0e-6093-4b58-94d2-52988aefbfc9", e.getMessage()); Assert.assertEquals("Not authorized for action \"Run as User\" for Job ID 3170ba0e-6093-4b58-94d2-52988aefbfc9", e.getMessage());
} }
} }
@Test
@Betamax(tape = "trigger_job_as_user_unauthorized")
public void triggerJobAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test;
try {
test = client.triggerJob(RunJobBuilder.builder()
.setJobId("3170ba0e-6093-4b58-94d2-52988aefbfc9")
.setAsUser("api-java-client-user-test2")
.create());
Assert.fail("should not succeed");
} catch (RundeckApiException e) {
Assert.assertEquals("Not authorized for action \"Run as User\" for Job ID 3170ba0e-6093-4b58-94d2-52988aefbfc9", e.getMessage());
}
}
@Test @Test
@Betamax(tape = "trigger_adhoc_command") @Betamax(tape = "trigger_adhoc_command")
public void triggerAdhocCommand() throws Exception { public void triggerAdhocCommandDeprecated() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test final RundeckExecution test
@ -413,9 +461,28 @@ public class RundeckClientTest {
Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus()); Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus());
} }
@Test
@Betamax(tape = "trigger_adhoc_command")
public void triggerAdhocCommand() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test
= client.triggerAdhocCommand(RunAdhocCommandBuilder.builder()
.setProject("test")
.setCommand("echo test trigger_adhoc_command")
.create());
Assert.assertEquals((Long) 23L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("echo test trigger_adhoc_command", test.getDescription());
Assert.assertEquals("admin", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus());
}
@Test @Test
@Betamax(tape = "trigger_adhoc_command_as_user") @Betamax(tape = "trigger_adhoc_command_as_user")
public void triggerAdhocCommandAsUser() throws Exception { public void triggerAdhocCommandDeprecatedAsUser() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test final RundeckExecution test
@ -429,8 +496,29 @@ public class RundeckClientTest {
Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus()); Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus());
} }
@Test @Test
@Betamax(tape = "trigger_adhoc_command_as_user")
public void triggerAdhocCommandAsUser() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test
= client.triggerAdhocCommand(
RunAdhocCommandBuilder.builder()
.setProject("test")
.setCommand("echo test trigger_adhoc_command_as_user")
.setAsUser("api-java-client-test-run-command-as-user1")
.create()
);
Assert.assertEquals((Long) 24L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("echo test trigger_adhoc_command_as_user", test.getDescription());
Assert.assertEquals("api-java-client-test-run-command-as-user1", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus());
}
@Test
@Betamax(tape = "trigger_adhoc_command_as_user_unauthorized") @Betamax(tape = "trigger_adhoc_command_as_user_unauthorized")
public void triggerAdhocCommandAsUserUnauthorized() throws Exception { public void triggerAdhocCommandDeprecatedAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test; final RundeckExecution test;
@ -441,7 +529,44 @@ public class RundeckClientTest {
Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage()); Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage());
} }
} }
@Test
@Betamax(tape = "trigger_adhoc_command_as_user_unauthorized")
public void triggerAdhocCommandAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
final RundeckExecution test;
try {
test = client.triggerAdhocCommand(
RunAdhocCommandBuilder.builder()
.setProject("test")
.setCommand("echo test trigger_adhoc_command_as_user")
.setAsUser("api-java-client-test-run-command-as-user1")
.create()
);
Assert.fail("should not succeed");
} catch (RundeckApiException e) {
Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage());
}
}
@Test
@Betamax(tape = "trigger_adhoc_script")
public void triggerAdhocScriptDeprecated() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
String script = "#!/bin/bash\n" +
"echo test trigger_adhoc_script\n";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes());
final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream,(Properties) null, null, null, null, null);
Assert.assertEquals((Long) 25L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("#!/bin/bash\necho test trigger_adhoc_script", test.getDescription());
Assert.assertEquals("admin", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus());
}
@Test @Test
@Betamax(tape = "trigger_adhoc_script") @Betamax(tape = "trigger_adhoc_script")
public void triggerAdhocScript() throws Exception { public void triggerAdhocScript() throws Exception {
@ -451,7 +576,8 @@ public class RundeckClientTest {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes()); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes());
final RundeckExecution test final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream, null, null, null, null, null); = client.triggerAdhocScript(RunAdhocScriptBuilder.builder().setProject("test").setScript
(byteArrayInputStream).create());
Assert.assertEquals((Long) 25L, test.getId()); Assert.assertEquals((Long) 25L, test.getId());
Assert.assertEquals(null, test.getArgstring()); Assert.assertEquals(null, test.getArgstring());
@ -462,6 +588,24 @@ public class RundeckClientTest {
} }
@Test @Test
@Betamax(tape = "trigger_adhoc_script_as_user") @Betamax(tape = "trigger_adhoc_script_as_user")
public void triggerAdhocScriptDeprecatedAsUser() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
String script = "#!/bin/bash\n" +
"echo test trigger_adhoc_script\n";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes());
final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream, (Properties) null, null, null, null, "api-java-client-test-adhoc-script-as-user1");
Assert.assertEquals((Long) 26L, test.getId());
Assert.assertEquals(null, test.getArgstring());
Assert.assertEquals(null, test.getAbortedBy());
Assert.assertEquals("#!/bin/bash\necho test trigger_adhoc_script", test.getDescription());
Assert.assertEquals("api-java-client-test-adhoc-script-as-user1", test.getStartedBy());
Assert.assertEquals(RundeckExecution.ExecutionStatus.RUNNING, test.getStatus());
}
@Test
@Betamax(tape = "trigger_adhoc_script_as_user")
public void triggerAdhocScriptAsUser() throws Exception { public void triggerAdhocScriptAsUser() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
String script = "#!/bin/bash\n" + String script = "#!/bin/bash\n" +
@ -469,7 +613,8 @@ public class RundeckClientTest {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes()); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes());
final RundeckExecution test final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream, null, null, null, null, "api-java-client-test-adhoc-script-as-user1"); = client.triggerAdhocScript(RunAdhocScriptBuilder.builder().setProject("test").setScript
(byteArrayInputStream).setAsUser("api-java-client-test-adhoc-script-as-user1").create());
Assert.assertEquals((Long) 26L, test.getId()); Assert.assertEquals((Long) 26L, test.getId());
Assert.assertEquals(null, test.getArgstring()); Assert.assertEquals(null, test.getArgstring());
@ -480,6 +625,23 @@ public class RundeckClientTest {
} }
@Test @Test
@Betamax(tape = "trigger_adhoc_script_as_user_unauthorized") @Betamax(tape = "trigger_adhoc_script_as_user_unauthorized")
public void triggerAdhocScriptDeprecatedAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3);
String script = "#!/bin/bash\n" +
"echo test trigger_adhoc_script\n";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes());
try{
final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream, (Properties) null, null, null, null, "api-java-client-test-adhoc-script-as-user1");
Assert.fail("should not succeed");
} catch (RundeckApiException e) {
Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage());
}
}
@Test
@Betamax(tape = "trigger_adhoc_script_as_user_unauthorized")
public void triggerAdhocScriptAsUserUnauthorized() throws Exception { public void triggerAdhocScriptAsUserUnauthorized() throws Exception {
RundeckClient client = createClient(TEST_TOKEN_3); RundeckClient client = createClient(TEST_TOKEN_3);
String script = "#!/bin/bash\n" + String script = "#!/bin/bash\n" +
@ -488,7 +650,8 @@ public class RundeckClientTest {
try{ try{
final RundeckExecution test final RundeckExecution test
= client.triggerAdhocScript("test", byteArrayInputStream, null, null, null, null, "api-java-client-test-adhoc-script-as-user1"); = client.triggerAdhocScript(RunAdhocScriptBuilder.builder().setProject("test").setScript
(byteArrayInputStream).setAsUser("api-java-client-test-adhoc-script-as-user1").create());
Assert.fail("should not succeed"); Assert.fail("should not succeed");
} catch (RundeckApiException e) { } catch (RundeckApiException e) {
Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage()); Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage());