From 4faced7da271ecc3d6f044aac61d59cd23350ada Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Fri, 21 Oct 2016 12:54:10 -0700 Subject: [PATCH] For job run, use json request for apiv18 --- .../org/rundeck/client/api/RundeckApi.java | 10 +++ .../rundeck/client/api/model/DateInfo.java | 14 +++- .../org/rundeck/client/api/model/JobRun.java | 71 +++++++++++++++++++ .../org/rundeck/client/tool/commands/Run.java | 57 +++++++++++++-- .../client/tool/options/RunBaseOptions.java | 11 ++- 5 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/rundeck/client/api/model/JobRun.java diff --git a/src/main/java/org/rundeck/client/api/RundeckApi.java b/src/main/java/org/rundeck/client/api/RundeckApi.java index d0c19bf1..3bbc7bae 100644 --- a/src/main/java/org/rundeck/client/api/RundeckApi.java +++ b/src/main/java/org/rundeck/client/api/RundeckApi.java @@ -11,7 +11,9 @@ import java.io.File; import java.net.URL; +import java.util.Date; import java.util.List; +import java.util.Map; /** * Created by greg on 3/28/16. @@ -252,6 +254,14 @@ Call runJob( @Query("asUser") String user ); + @Headers("Accept: application/json") + @POST("job/{id}/executions") + Call runJob( + @Path("id") String id, + @Body JobRun jobRun + + ); + //key storage @Headers("Accept: application/json") diff --git a/src/main/java/org/rundeck/client/api/model/DateInfo.java b/src/main/java/org/rundeck/client/api/model/DateInfo.java index 79542c46..eba7932f 100644 --- a/src/main/java/org/rundeck/client/api/model/DateInfo.java +++ b/src/main/java/org/rundeck/client/api/model/DateInfo.java @@ -16,8 +16,18 @@ public class DateInfo { public String date; public long unixtime; - Date toDate() throws ParseException { - SimpleDateFormat asdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); + public DateInfo(final String date) { + this.date = date; + } + + public DateInfo() { + } + + public Date toDate() throws ParseException { + return toDate("yyyy-MM-dd'T'HH:mm:ss'Z'"); + } + public Date toDate(final String format) throws ParseException { + SimpleDateFormat asdf = new SimpleDateFormat(format, Locale.US); return asdf.parse(date); } diff --git a/src/main/java/org/rundeck/client/api/model/JobRun.java b/src/main/java/org/rundeck/client/api/model/JobRun.java new file mode 100644 index 00000000..b3dedc63 --- /dev/null +++ b/src/main/java/org/rundeck/client/api/model/JobRun.java @@ -0,0 +1,71 @@ +package org.rundeck.client.api.model; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.Date; +import java.util.Map; + +/** + * Parameters to run a job + */ +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class JobRun { + private String asUser; + private String argString; + private String loglevel; + private String filter; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssX") + private Date runAtTime; + private Map options; + + public String getAsUser() { + return asUser; + } + + public void setAsUser(String asUser) { + this.asUser = asUser; + } + + public String getArgString() { + return argString; + } + + public void setArgString(String argString) { + this.argString = argString; + } + + public String getLoglevel() { + return loglevel; + } + + public void setLoglevel(String loglevel) { + this.loglevel = loglevel; + } + + public String getFilter() { + return filter; + } + + public void setFilter(String filter) { + this.filter = filter; + } + + public Date getRunAtTime() { + return runAtTime; + } + + public void setRunAtTime(Date runAtTime) { + this.runAtTime = runAtTime; + } + + public Map getOptions() { + return options; + } + + public void setOptions(Map options) { + this.options = options; + } +} diff --git a/src/main/java/org/rundeck/client/tool/commands/Run.java b/src/main/java/org/rundeck/client/tool/commands/Run.java index 6dff623b..278eda48 100644 --- a/src/main/java/org/rundeck/client/tool/commands/Run.java +++ b/src/main/java/org/rundeck/client/tool/commands/Run.java @@ -5,13 +5,17 @@ import org.rundeck.client.api.RundeckApi; import org.rundeck.client.api.model.Execution; import org.rundeck.client.api.model.JobItem; +import org.rundeck.client.api.model.JobRun; import org.rundeck.client.tool.options.RunBaseOptions; import org.rundeck.client.util.Client; import org.rundeck.client.util.Quoting; import retrofit2.Call; import java.io.IOException; +import java.text.ParseException; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Created by greg on 5/20/16. @@ -56,13 +60,52 @@ public boolean run(RunBaseOptions options, CommandOutput out) throws IOException throw new IllegalArgumentException("-j job or -i id is required"); } - Call executionListCall = client.getService().runJob( - jobId, - Quoting.joinStringQuoted(options.getCommandString()), - options.getLoglevel(), - options.getFilter(), - options.getUser() - ); + Call executionListCall; + if (client.getApiVersion() >= 18) { + JobRun request = new JobRun(); + request.setLoglevel(options.getLoglevel()); + request.setFilter(options.getFilter()); + request.setAsUser(options.getUser()); + List commandString = options.getCommandString(); + Map jobopts = new HashMap<>(); + String key = null; + if (null != commandString) { + for (String s : commandString) { + if (key == null && s.startsWith("-")) { + key = s.substring(1); + } else if (key != null) { + jobopts.put(key, s); + key = null; + } + } + } + if (key != null) { + throw new IllegalArgumentException( + String.format( + "Incorrect job options, expected: \"-%s value\", but saw only \"-%s\"", + key, + key + )); + } + + request.setOptions(jobopts); + if (null != options.getRunAtDate()) { + try { + request.setRunAtTime(options.getRunAtDate().toDate("yyyy-MM-dd'T'HH:mm:ssXX")); + } catch (ParseException e) { + throw new IllegalArgumentException("-@/--at date format is not valid", e); + } + } + executionListCall = client.getService().runJob(jobId, request); + } else { + executionListCall = client.getService().runJob( + jobId, + Quoting.joinStringQuoted(options.getCommandString()), + options.getLoglevel(), + options.getFilter(), + options.getUser() + ); + } Execution execution = client.checkError(executionListCall); out.output(String.format("Execution started: %s%n", execution.toBasicString())); diff --git a/src/main/java/org/rundeck/client/tool/options/RunBaseOptions.java b/src/main/java/org/rundeck/client/tool/options/RunBaseOptions.java index 8ce2d1d7..61859e75 100644 --- a/src/main/java/org/rundeck/client/tool/options/RunBaseOptions.java +++ b/src/main/java/org/rundeck/client/tool/options/RunBaseOptions.java @@ -3,7 +3,9 @@ import com.lexicalscope.jewel.cli.CommandLineInterface; import com.lexicalscope.jewel.cli.Option; import com.lexicalscope.jewel.cli.Unparsed; +import org.rundeck.client.api.model.DateInfo; +import java.util.Date; import java.util.List; /** @@ -40,7 +42,14 @@ public interface RunBaseOptions extends FollowOptions,OptionalProjectOptions { boolean isUser(); - @Unparsed(name = "-- -ARG VAL -ARG2 VAL", description = "Dispatch specified command string") + @Option(shortName = "@", + longName = "at", + description = "Run the job at the specified date/time. ISO8601 format (yyyy-MM-dd'T'HH:mm:ss'Z')") + DateInfo getRunAtDate(); + + boolean isRunAtDate(); + + @Unparsed(name = "-- -OPT VAL -OPT2 VAL", description = "Job options") List getCommandString(); }