From bbcfd1cff736dfd289ba61d5359a3b5594a14338 Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Tue, 9 Jul 2013 10:22:57 -0700 Subject: [PATCH] Use builder based arguments for methods * Replace triggerAdhocScript/Command/Job * Add RunAdhocX interfaces * Add RunJob interface and builder --- .../java/org/rundeck/api/DefaultRunAdhoc.java | 54 ++++ .../rundeck/api/DefaultRunAdhocCommand.java | 18 ++ .../rundeck/api/DefaultRunAdhocScript.java | 28 ++ .../java/org/rundeck/api/DefaultRunJob.java | 50 +++ src/main/java/org/rundeck/api/RunAdhoc.java | 38 +++ .../java/org/rundeck/api/RunAdhocCommand.java | 14 + .../rundeck/api/RunAdhocCommandBuilder.java | 55 ++++ .../java/org/rundeck/api/RunAdhocScript.java | 22 ++ .../rundeck/api/RunAdhocScriptBuilder.java | 62 ++++ src/main/java/org/rundeck/api/RunJob.java | 32 ++ .../java/org/rundeck/api/RunJobBuilder.java | 44 +++ .../java/org/rundeck/api/RundeckClient.java | 289 ++++++++++++++++-- .../org/rundeck/api/RundeckClientTest.java | 191 +++++++++++- 13 files changed, 853 insertions(+), 44 deletions(-) create mode 100644 src/main/java/org/rundeck/api/DefaultRunAdhoc.java create mode 100644 src/main/java/org/rundeck/api/DefaultRunAdhocCommand.java create mode 100644 src/main/java/org/rundeck/api/DefaultRunAdhocScript.java create mode 100644 src/main/java/org/rundeck/api/DefaultRunJob.java create mode 100644 src/main/java/org/rundeck/api/RunAdhoc.java create mode 100644 src/main/java/org/rundeck/api/RunAdhocCommand.java create mode 100644 src/main/java/org/rundeck/api/RunAdhocCommandBuilder.java create mode 100644 src/main/java/org/rundeck/api/RunAdhocScript.java create mode 100644 src/main/java/org/rundeck/api/RunAdhocScriptBuilder.java create mode 100644 src/main/java/org/rundeck/api/RunJob.java create mode 100644 src/main/java/org/rundeck/api/RunJobBuilder.java diff --git a/src/main/java/org/rundeck/api/DefaultRunAdhoc.java b/src/main/java/org/rundeck/api/DefaultRunAdhoc.java new file mode 100644 index 0000000..8e1897a --- /dev/null +++ b/src/main/java/org/rundeck/api/DefaultRunAdhoc.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/DefaultRunAdhocCommand.java b/src/main/java/org/rundeck/api/DefaultRunAdhocCommand.java new file mode 100644 index 0000000..33ae58a --- /dev/null +++ b/src/main/java/org/rundeck/api/DefaultRunAdhocCommand.java @@ -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; + } + +} diff --git a/src/main/java/org/rundeck/api/DefaultRunAdhocScript.java b/src/main/java/org/rundeck/api/DefaultRunAdhocScript.java new file mode 100644 index 0000000..bd6c0e6 --- /dev/null +++ b/src/main/java/org/rundeck/api/DefaultRunAdhocScript.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/DefaultRunJob.java b/src/main/java/org/rundeck/api/DefaultRunJob.java new file mode 100644 index 0000000..fb3ca49 --- /dev/null +++ b/src/main/java/org/rundeck/api/DefaultRunJob.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/RunAdhoc.java b/src/main/java/org/rundeck/api/RunAdhoc.java new file mode 100644 index 0000000..7fe2ddf --- /dev/null +++ b/src/main/java/org/rundeck/api/RunAdhoc.java @@ -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(); +} diff --git a/src/main/java/org/rundeck/api/RunAdhocCommand.java b/src/main/java/org/rundeck/api/RunAdhocCommand.java new file mode 100644 index 0000000..83c77d1 --- /dev/null +++ b/src/main/java/org/rundeck/api/RunAdhocCommand.java @@ -0,0 +1,14 @@ +package org.rundeck.api; + +/** + * A command to execute + */ +public interface RunAdhocCommand extends RunAdhoc { + + /** + * Command to execute + * @return + */ + String getCommand(); + +} diff --git a/src/main/java/org/rundeck/api/RunAdhocCommandBuilder.java b/src/main/java/org/rundeck/api/RunAdhocCommandBuilder.java new file mode 100644 index 0000000..745b345 --- /dev/null +++ b/src/main/java/org/rundeck/api/RunAdhocCommandBuilder.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/RunAdhocScript.java b/src/main/java/org/rundeck/api/RunAdhocScript.java new file mode 100644 index 0000000..98c8d66 --- /dev/null +++ b/src/main/java/org/rundeck/api/RunAdhocScript.java @@ -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(); + +} diff --git a/src/main/java/org/rundeck/api/RunAdhocScriptBuilder.java b/src/main/java/org/rundeck/api/RunAdhocScriptBuilder.java new file mode 100644 index 0000000..39ba5fc --- /dev/null +++ b/src/main/java/org/rundeck/api/RunAdhocScriptBuilder.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/RunJob.java b/src/main/java/org/rundeck/api/RunJob.java new file mode 100644 index 0000000..3bc7c85 --- /dev/null +++ b/src/main/java/org/rundeck/api/RunJob.java @@ -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(); +} diff --git a/src/main/java/org/rundeck/api/RunJobBuilder.java b/src/main/java/org/rundeck/api/RunJobBuilder.java new file mode 100644 index 0000000..83d9e87 --- /dev/null +++ b/src/main/java/org/rundeck/api/RunJobBuilder.java @@ -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; + } +} diff --git a/src/main/java/org/rundeck/api/RundeckClient.java b/src/main/java/org/rundeck/api/RundeckClient.java index 6a64a26..1ae3bb1 100644 --- a/src/main/java/org/rundeck/api/RundeckClient.java +++ b/src/main/java/org/rundeck/api/RundeckClient.java @@ -40,7 +40,11 @@ import java.util.Properties; import java.util.concurrent.TimeUnit; /** - * Main entry point to talk to a RunDeck instance.
+ * Main entry point to talk to a RunDeck instance. + *
+ * Deprecation Warning: 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. + *
* 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 * documentation for generating such a token.
@@ -176,7 +180,7 @@ public class RundeckClient implements Serializable { * @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) - * @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 { this(url); @@ -872,6 +876,7 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String, Properties, Properties) * @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, RundeckApiTokenException, IllegalArgumentException { @@ -891,6 +896,7 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String, Properties, 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, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { @@ -912,6 +918,7 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String) * @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) 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) * @see #triggerJob(String) * @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) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { - AssertUtil.notBlank(jobId, "jobId is mandatory to trigger a job !"); - ApiPathBuilder apiPath = new ApiPathBuilder("/job/", jobId, "/run").param("argString", - ParametersUtil.generateArgString(options)) - .nodeFilters(nodeFilters); - if(null!=asUser) { - apiPath.param("asUser", asUser); + return triggerJob(RunJobBuilder.builder() + .setJobId(jobId) + .setOptions(options) + .setNodeFilters(nodeFilters) + .setAsUser(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")); } @@ -959,6 +993,8 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String) * @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, RundeckApiTokenException, IllegalArgumentException { @@ -979,6 +1015,8 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String, Properties) * @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, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { @@ -1001,12 +1039,34 @@ public class RundeckClient implements Serializable { * @throws IllegalArgumentException if the jobId is blank (null, empty or whitespace) * @see #triggerJob(String, Properties, Properties) * @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) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { 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. * 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) * @see #triggerJob(String, Properties) * @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) 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) * @see #triggerJob(String, Properties) * @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, 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) * @see #triggerJob(String, Properties) * @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, TimeUnit poolingUnit) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, 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) { poolingInterval = DEFAULT_POOLING_INTERVAL; poolingUnit = DEFAULT_POOLING_UNIT; @@ -1084,7 +1182,7 @@ public class RundeckClient implements Serializable { poolingUnit = DEFAULT_POOLING_UNIT; } - RundeckExecution execution = triggerJob(jobId, options, nodeFilters, asUser); + RundeckExecution execution = triggerJob(jobRun); while (ExecutionStatus.RUNNING.equals(execution.getStatus())) { try { 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) * @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean) * @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, 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) * @see #triggerAdhocCommand(String, String, Properties, Integer, Boolean) * @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) 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) * @see #triggerAdhocCommand(String, String) * @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, 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) * @see #triggerAdhocCommand(String, String) * @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, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { - AssertUtil.notBlank(project, "project is mandatory to trigger an ad-hoc command !"); - AssertUtil.notBlank(command, "command is mandatory to trigger an ad-hoc command !"); - ApiPathBuilder apiPath = new ApiPathBuilder("/run/command").param("project", project) - .param("exec", command) + return triggerAdhocCommand(RunAdhocCommandBuilder.builder() + .setProject(project) + .setCommand(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", - nodeThreadcount) + command.getNodeThreadcount()) .param("nodeKeepgoing", - nodeKeepgoing) - .nodeFilters(nodeFilters); - if(null!=asUser) { - apiPath.param("asUser", asUser); + command.getNodeKeepgoing()) + .nodeFilters(command.getNodeFilters()); + if(null!= command.getAsUser()) { + apiPath.param("asUser", command.getAsUser()); } 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 @@ -1404,6 +1535,7 @@ public class RundeckClient implements Serializable { * @throws IOException if we failed to read the file * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, String) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, String scriptFilename) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, IOException { @@ -1425,6 +1557,7 @@ public class RundeckClient implements Serializable { * @throws IOException if we failed to read the file * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, String, Properties) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException, @@ -1448,6 +1581,7 @@ public class RundeckClient implements Serializable { * @throws IOException if we failed to read the file * @see #triggerAdhocScript(String, String, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, String, Properties, Properties) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, String scriptFilename, Properties options, Properties nodeFilters) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, @@ -1473,6 +1607,7 @@ public class RundeckClient implements Serializable { * @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, Properties options, 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 * @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, Properties options, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing,String asUser) throws RundeckApiException, 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 !"); FileInputStream stream = null; try { 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 { 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 * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, InputStream) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, InputStream script) throws RundeckApiException, 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 * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, InputStream, Properties) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options) 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 * @see #triggerAdhocScript(String, InputStream, Properties, Properties, Integer, Boolean) * @see #runAdhocScript(String, InputStream, Properties, Properties) + * @deprecated use {@link #triggerAdhocScript(RunAdhocScript)} */ public RundeckExecution triggerAdhocScript(String project, InputStream script, Properties options, 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 * @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, Properties options, 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 * @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, Properties options, Properties nodeFilters, Integer nodeThreadcount, Boolean nodeKeepgoing, String asUser) throws RundeckApiException, RundeckApiLoginException, RundeckApiTokenException, IllegalArgumentException { - AssertUtil.notBlank(project, "project is mandatory to trigger an ad-hoc script !"); - AssertUtil.notNull(script, "script is mandatory to trigger an ad-hoc script !"); - ApiPathBuilder apiPath = new ApiPathBuilder("/run/script").param("project", project) + return triggerAdhocScript(project, script, 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 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", - script) - .param("argString", - ParametersUtil.generateArgString(options)) + adhocScript.getScript()) + .param("argString",adhocScript.getArgString()) .param("nodeThreadcount", - nodeThreadcount) + adhocScript.getNodeThreadcount()) .param("nodeKeepgoing", - nodeKeepgoing) - .nodeFilters(nodeFilters); - if(null!=asUser) { - apiPath.param("asUser", asUser); + adhocScript.getNodeKeepgoing()) + .nodeFilters(adhocScript.getNodeFilters()); + if(null!=adhocScript.getAsUser()) { + apiPath.param("asUser", adhocScript.getAsUser()); } 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 diff --git a/src/test/java/org/rundeck/api/RundeckClientTest.java b/src/test/java/org/rundeck/api/RundeckClientTest.java index f5de6ae..5a35dab 100644 --- a/src/test/java/org/rundeck/api/RundeckClientTest.java +++ b/src/test/java/org/rundeck/api/RundeckClientTest.java @@ -28,10 +28,7 @@ import org.rundeck.api.query.ExecutionQuery; import org.rundeck.api.util.PagedResults; import java.io.ByteArrayInputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; +import java.util.*; /** @@ -347,13 +344,13 @@ public class RundeckClientTest { final RundeckJobDelete delete = deleteTest.getResults().get(0); Assert.assertFalse(delete.isSuccessful()); Assert.assertNotNull(delete.getError()); - Assert.assertEquals("unauthorized",delete.getErrorCode()); + Assert.assertEquals("unauthorized", delete.getErrorCode()); Assert.assertNull(delete.getMessage()); Assert.assertEquals("3a6d16be-4268-4d26-86a9-cebc1781f768", delete.getId()); } @Test @Betamax(tape = "trigger_job_basic") - public void triggerJobBasic() throws Exception { + public void triggerJobDeprecatedBasic() throws Exception { RundeckClient client = createClient(TEST_TOKEN_3); final RundeckExecution test @@ -366,6 +363,38 @@ public class RundeckClientTest { Assert.assertEquals("admin", test.getStartedBy()); 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 @Betamax(tape = "trigger_job_as_user") @@ -373,7 +402,10 @@ public class RundeckClientTest { RundeckClient client = createClient(TEST_TOKEN_3); 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(null, test.getArgstring()); @@ -385,7 +417,7 @@ public class RundeckClientTest { } @Test @Betamax(tape = "trigger_job_as_user_unauthorized") - public void triggerJobAsUserUnauthorized() throws Exception { + public void triggerJobDeprecatedAsUserUnauthorized() throws Exception { RundeckClient client = createClient(TEST_TOKEN_3); 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()); } } + @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 @Betamax(tape = "trigger_adhoc_command") - public void triggerAdhocCommand() throws Exception { + public void triggerAdhocCommandDeprecated() throws Exception { RundeckClient client = createClient(TEST_TOKEN_3); final RundeckExecution test @@ -413,9 +461,28 @@ public class RundeckClientTest { 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 @Betamax(tape = "trigger_adhoc_command_as_user") - public void triggerAdhocCommandAsUser() throws Exception { + public void triggerAdhocCommandDeprecatedAsUser() throws Exception { RundeckClient client = createClient(TEST_TOKEN_3); final RundeckExecution test @@ -429,8 +496,29 @@ public class RundeckClientTest { Assert.assertEquals(RundeckExecution.ExecutionStatus.SUCCEEDED, test.getStatus()); } @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") - public void triggerAdhocCommandAsUserUnauthorized() throws Exception { + public void triggerAdhocCommandDeprecatedAsUserUnauthorized() throws Exception { RundeckClient client = createClient(TEST_TOKEN_3); final RundeckExecution test; @@ -441,7 +529,44 @@ public class RundeckClientTest { 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 @Betamax(tape = "trigger_adhoc_script") public void triggerAdhocScript() throws Exception { @@ -451,7 +576,8 @@ public class RundeckClientTest { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes()); 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(null, test.getArgstring()); @@ -462,6 +588,24 @@ public class RundeckClientTest { } @Test @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 { RundeckClient client = createClient(TEST_TOKEN_3); String script = "#!/bin/bash\n" + @@ -469,7 +613,8 @@ public class RundeckClientTest { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(script.getBytes()); 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(null, test.getArgstring()); @@ -480,6 +625,23 @@ public class RundeckClientTest { } @Test @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 { RundeckClient client = createClient(TEST_TOKEN_3); String script = "#!/bin/bash\n" + @@ -488,7 +650,8 @@ public class RundeckClientTest { try{ 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"); } catch (RundeckApiException e) { Assert.assertEquals("Not authorized for action \"Run as User\" for Run Adhoc", e.getMessage());