Skip to content

Commit

Permalink
For job run, use json request for apiv18
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Oct 21, 2016
1 parent 1fa888e commit 4faced7
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/rundeck/client/api/RundeckApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -252,6 +254,14 @@ Call<Execution> runJob(
@Query("asUser") String user
);

@Headers("Accept: application/json")
@POST("job/{id}/executions")
Call<Execution> runJob(
@Path("id") String id,
@Body JobRun jobRun

);

//key storage

@Headers("Accept: application/json")
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/rundeck/client/api/model/DateInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/rundeck/client/api/model/JobRun.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> getOptions() {
return options;
}

public void setOptions(Map<String, String> options) {
this.options = options;
}
}
57 changes: 50 additions & 7 deletions src/main/java/org/rundeck/client/tool/commands/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -56,13 +60,52 @@ public boolean run(RunBaseOptions options, CommandOutput out) throws IOException
throw new IllegalArgumentException("-j job or -i id is required");

}
Call<Execution> executionListCall = client.getService().runJob(
jobId,
Quoting.joinStringQuoted(options.getCommandString()),
options.getLoglevel(),
options.getFilter(),
options.getUser()
);
Call<Execution> executionListCall;
if (client.getApiVersion() >= 18) {
JobRun request = new JobRun();
request.setLoglevel(options.getLoglevel());
request.setFilter(options.getFilter());
request.setAsUser(options.getUser());
List<String> commandString = options.getCommandString();
Map<String, String> 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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<String> getCommandString();

}
Expand Down

0 comments on commit 4faced7

Please sign in to comment.