Skip to content

Commit

Permalink
Merge pull request #10 from rundeck/apiv18
Browse files Browse the repository at this point in the history
API v18 support
  • Loading branch information
gschueler authored Nov 14, 2016
2 parents 8b05752 + ba08efa commit 6141555
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 25 deletions.
22 changes: 19 additions & 3 deletions src/main/java/org/rundeck/client/Rundeck.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class Rundeck {
public static final int API_VERS = 16;
public static final Pattern API_VERS_PATTERN = Pattern.compile("^(.*)(/api/\\d+/?)$");
public static final Pattern API_VERS_PATTERN = Pattern.compile("^(.*)(/api/(\\d+)/?)$");

/**
* Create a client using the specified, or default version
Expand Down Expand Up @@ -79,6 +79,7 @@ public static Client<RundeckApi> client(
{
String appBaseUrl = buildBaseAppUrlForVersion(baseUrl);
String base = buildApiUrlForVersion(baseUrl, apiVers);
int usedApiVers = apiVersionForUrl(baseUrl, apiVers);

OkHttpClient.Builder callFactory = new OkHttpClient.Builder()
.addInterceptor(new StaticHeaderInterceptor("X-Rundeck-Auth-Token", authToken));
Expand Down Expand Up @@ -109,7 +110,7 @@ public static Client<RundeckApi> client(
))
.build();

return new Client<>(build.create(RundeckApi.class), build);
return new Client<>(build.create(RundeckApi.class), build, usedApiVers);
}

/**
Expand All @@ -134,6 +135,7 @@ public static Client<RundeckApi> client(

String appBaseUrl = buildBaseAppUrlForVersion(baseUrl);
String apiBaseUrl = buildApiUrlForVersion(baseUrl, apiVers);
int usedApiVers = apiVersionForUrl(baseUrl, apiVers);

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
Expand Down Expand Up @@ -196,7 +198,7 @@ public static Client<RundeckApi> client(
))
.build();

return new Client<>(build.create(RundeckApi.class), build);
return new Client<>(build.create(RundeckApi.class), build, usedApiVers);
}

/**
Expand All @@ -212,6 +214,20 @@ private static String buildApiUrlForVersion(String baseUrl, final int apiVers) {
return normalizeUrlPath(baseUrl);
}

/**
* @param baseUrl input url
* @param apiVers api VERSION to append if /api/VERS is not present
*
* @return URL for API by appending /api/VERS if it is not present
*/
private static int apiVersionForUrl(String baseUrl, final int apiVers) {
Matcher matcher = API_VERS_PATTERN.matcher(baseUrl);
if (matcher.matches()) {
return Integer.parseInt(matcher.group(3));
}
return apiVers;
}

private static String normalizeUrlPath(String baseUrl) {
if (!baseUrl.matches(".*/$")) {
return baseUrl + "/";
Expand Down
22 changes: 21 additions & 1 deletion 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 All @@ -33,6 +35,17 @@ Call<List<JobItem>> listJobs(
@Query("idlist") String idlist
);

/**
* new api
* @param jobid
* @return
*/
@Headers("Accept: application/json")
@GET("job/{jobid}/info")
Call<ScheduledJobItem> getJobInfo(
@Path("jobid") String jobid
);


@GET("project/{project}/jobs/export")
Call<ResponseBody> exportJobs(
Expand Down Expand Up @@ -241,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 Expand Up @@ -445,7 +466,6 @@ Call<ApiToken> createToken(
* <a href="http://rundeck.org/docs/api/index.html#delete-a-token">Delete a Token</a>
*
* @param id token
*
*/
@Headers("Accept: application/json")
@DELETE("token/{id}")
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
27 changes: 27 additions & 0 deletions src/main/java/org/rundeck/client/api/model/JobItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Created by greg on 3/28/16.
*/
Expand All @@ -25,6 +29,9 @@ public class JobItem {
@Element(required = false)
private String permalink;

@Attribute(required = false)
private Long averageDuration;

public String getId() {
return id;
}
Expand Down Expand Up @@ -94,7 +101,27 @@ public String toString() {
'}';
}

public Map<Object, Object> toMap() {
HashMap<Object, Object> map = new LinkedHashMap<>();
map.put("id", getId());
map.put("name", getName());
map.put("group", getGroup());
map.put("project", getProject());
if(null!=getAverageDuration()){
map.put("averageDuration", getAverageDuration());
}
return map;
}

public String toBasicString() {
return String.format("[%s] %s%s", id, group != null ? group + "/" : "", name);
}

public Long getAverageDuration() {
return averageDuration;
}

public void setAverageDuration(Long averageDuration) {
this.averageDuration = averageDuration;
}
}
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;
}
}
28 changes: 18 additions & 10 deletions src/main/java/org/rundeck/client/api/model/ScheduledJobItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ScheduledJobItem extends JobItem {
private boolean scheduled;
private boolean scheduleEnabled;
private boolean enabled;
private boolean serverOwner;
private Boolean serverOwner;


public String getServerNodeUUID() {
Expand Down Expand Up @@ -51,23 +51,31 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public boolean isServerOwner() {
public Boolean isServerOwner() {
return serverOwner;
}

public void setServerOwner(boolean serverOwner) {
public void setServerOwner(Boolean serverOwner) {
this.serverOwner = serverOwner;
}

public Map<Object, Object> toMap() {
HashMap<Object, Object> map = new LinkedHashMap<>();
map.put("job", toBasicString());
map.put("project", getProject());
map.put("serverNodeUUID", getServerNodeUUID());
map.put("scheduled", scheduled);
map.put("scheduleEnabled", scheduleEnabled);
map.put("enabled", enabled);
map.put("serverOwner", serverOwner);


HashMap<Object, Object> detail = new LinkedHashMap<>(super.toMap());
detail.put("scheduled", scheduled);
detail.put("scheduleEnabled", scheduleEnabled);
detail.put("enabled", enabled);

map.put("job", detail);

if (null != serverOwner && null != getServerNodeUUID()) {
HashMap<Object, Object> schedule = new LinkedHashMap<>();
schedule.put("serverNodeUUID", getServerNodeUUID());
schedule.put("serverOwner", serverOwner);
map.put("scheduler", schedule);
}
return map;
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/rundeck/client/tool/commands/Jobs.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.rundeck.client.tool.commands;

import com.lexicalscope.jewel.cli.CommandLineInterface;
import com.lexicalscope.jewel.cli.Option;
import com.simplifyops.toolbelt.Command;
import com.simplifyops.toolbelt.CommandOutput;
import okhttp3.RequestBody;
Expand All @@ -18,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by greg on 3/28/16.
Expand Down Expand Up @@ -186,6 +188,18 @@ public void list(ListOpts options, CommandOutput output) throws IOException {
}
}

@CommandLineInterface(application = "info") interface InfoOpts {

@Option(shortName = "i", longName = "id", description = "Job ID")
String getId();
}

@Command(description = "Get info about a Job by ID (API v18)")
public void info(InfoOpts options, CommandOutput output) throws IOException {
ScheduledJobItem body = client.checkError(client.getService().getJobInfo(options.getId()));
output.output(body.toMap());
}

/**
* Split a job group/name into group then name parts
*
Expand Down
Loading

0 comments on commit 6141555

Please sign in to comment.