Skip to content

Commit

Permalink
Fixed #67
Browse files Browse the repository at this point in the history
Added ComputerSet and Executor informations.
  • Loading branch information
khmarbaise committed Aug 15, 2015
1 parent 0dc5330 commit 95ade9f
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 44 deletions.
71 changes: 48 additions & 23 deletions src/main/java/com/offbytwo/jenkins/JenkinsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.common.net.UrlEscapers;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Computer;
import com.offbytwo.jenkins.model.ComputerSet;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobConfiguration;
import com.offbytwo.jenkins.model.JobWithDetails;
Expand Down Expand Up @@ -40,7 +41,8 @@ public class JenkinsServer {
/**
* Create a new Jenkins server reference given only the server address
*
* @param serverUri address of jenkins server (ex. http://localhost:8080/jenkins)
* @param serverUri
* address of jenkins server (ex. http://localhost:8080/jenkins)
*/
public JenkinsServer(URI serverUri) {
this(new JenkinsHttpClient(serverUri));
Expand All @@ -49,9 +51,12 @@ public JenkinsServer(URI serverUri) {
/**
* Create a new Jenkins server reference given the address and credentials
*
* @param serverUri address of jenkins server (ex. http://localhost:8080/jenkins)
* @param username username to use when connecting
* @param passwordOrToken password (not recommended) or token (recommended)
* @param serverUri
* address of jenkins server (ex. http://localhost:8080/jenkins)
* @param username
* username to use when connecting
* @param passwordOrToken
* password (not recommended) or token (recommended)
*/
public JenkinsServer(URI serverUri, String username, String passwordOrToken) {
this(new JenkinsHttpClient(serverUri, username, passwordOrToken));
Expand All @@ -60,7 +65,8 @@ public JenkinsServer(URI serverUri, String username, String passwordOrToken) {
/**
* Create a new Jenkins server directly from an HTTP client (ADVANCED)
*
* @param client Specialized client to use.
* @param client
* Specialized client to use.
*/
public JenkinsServer(JenkinsHttpClient client) {
this.client = client;
Expand Down Expand Up @@ -109,17 +115,17 @@ public Map<String, View> getViews() throws IOException {
@Override
public String apply(View view) {
view.setClient(client);
//return view.getName().toLowerCase();
// return view.getName().toLowerCase();
return view.getName();
}
});
}


/**
* Get a single view object from the server
*
* @param name name of the view in Jenkins
* @param name
* name of the view in Jenkins
* @return the view object
* @throws IOException
*/
Expand All @@ -128,7 +134,8 @@ public View getView(String name) throws IOException {
}

/**
* Get a list of all the defined jobs on the server (at the specified view level)
* Get a list of all the defined jobs on the server (at the specified view
* level)
*
* @return list of defined jobs (view level, for details @see Job#details
* @throws IOException
Expand Down Expand Up @@ -188,7 +195,7 @@ public MavenJobWithDetails getMavenJob(String jobName) throws IOException {
public void createJob(String jobName, String jobXml) throws IOException {
client.post_xml("/createItem?name=" + encodeParam(jobName), jobXml);
}

public void createJob(String jobName, String jobXml, Boolean crumbFlag) throws IOException {
client.post_xml("/createItem?name=" + encodeParam(jobName), jobXml, crumbFlag);
}
Expand All @@ -213,11 +220,11 @@ public LabelWithDetails getLabel(String labelName) throws IOException {
return client.get("/label/" + encode(labelName), LabelWithDetails.class);
}


/**
* Get a list of all the computers on the server (at the summary level)
*
* @return list of defined computers (summary level, for details @see Computer#details
* @return list of defined computers (summary level, for details @see
* Computer#details
* @throws IOException
*/
public Map<String, Computer> getComputers() throws IOException {
Expand All @@ -231,6 +238,18 @@ public String apply(Computer computer) {
});
}

/**
* The ComputerSet class will give informations
* like {@link ComputerSet#getBusyExecutors()} or
* the {@link ComputerSet#getTotalExecutors()}.
*
* @return {@link ComputerSet}
* @throws IOException
*/
public ComputerSet getComputerSet() throws IOException {
return client.get("computer/", ComputerSet.class);
}

/**
* Update the xml description of an existing job
*
Expand All @@ -245,7 +264,8 @@ public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws I
client.post_xml("/job/" + encode(jobName) + "/config.xml", jobXml, crumbFlag);
}

public void addStringParam(String jobName, String name, String description, String defaultValue) throws IOException, JAXBException, DocumentException {
public void addStringParam(String jobName, String name, String description, String defaultValue)
throws IOException, JAXBException, DocumentException {
String jobXml = this.getJobXml(jobName);
JobConfiguration jobConf = new JobConfiguration(jobXml);
jobXml = jobConf.addStringParam(name, description, defaultValue).asXml();
Expand All @@ -254,27 +274,27 @@ public void addStringParam(String jobName, String name, String description, Stri

/**
* Sends the Quiet Down (Prepare for shutdown) message
*
* @throws IOException
*/
public void quietDown() throws IOException {
try {
client.get("/quietDown/");
}
catch (org.apache.http.client.ClientProtocolException e) {
} catch (org.apache.http.client.ClientProtocolException e) {
e.printStackTrace();
}

}

/**
* Cancels the Quiet Down (Prepare for shutdown) message
*
* @throws IOException
*/
public void cancelQuietDown() throws IOException {
try {
client.post("/cancelQuietDown/");
}
catch (org.apache.http.client.ClientProtocolException e) {
} catch (org.apache.http.client.ClientProtocolException e) {
e.printStackTrace();
}
}
Expand All @@ -290,9 +310,13 @@ public void deleteJob(String jobName) throws IOException {

/**
* Delete a job from Jenkins.
* @param jobName The name of the job to be deleted.
* @param crumbFlag The crumFlag.
* @throws IOException In case of an failure.
*
* @param jobName
* The name of the job to be deleted.
* @param crumbFlag
* The crumFlag.
* @throws IOException
* In case of an failure.
*/
public void deleteJob(String jobName, boolean crumbFlag) throws IOException {
client.post("/job/" + encode(jobName) + "/doDelete", crumbFlag);
Expand All @@ -303,9 +327,10 @@ public void deleteJob(String jobName, boolean crumbFlag) throws IOException {
*
* This is similar to running groovy scripts using the script console.
*
* In the instance where your script causes an exception, the server still returns a 200 status,
* so detecting errors is very challenging. It is recommended to use heuristics to check your return
* string for stack traces by detecting strings like "groovy.lang.(something)Exception".
* In the instance where your script causes an exception, the server still
* returns a 200 status, so detecting errors is very challenging. It is
* recommended to use heuristics to check your return string for stack
* traces by detecting strings like "groovy.lang.(something)Exception".
*
* @param script
* @return results
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/com/offbytwo/jenkins/model/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.google.common.net.UrlEscapers;


public class Computer extends BaseModel {

private String displayName;
Expand All @@ -26,7 +25,8 @@ public void setComputer(List<Computer> computer) {

List<Computer> computer;

public Computer() {}
public Computer() {
}

public Computer(String displayName) {
this();
Expand All @@ -41,32 +41,40 @@ public ComputerWithDetails details() throws IOException {
String name;
if ("master".equals(displayName)) {
name = "(master)";
}
else {
} else {
name = UrlEscapers.urlPathSegmentEscaper().escape(displayName);
}
return client.get("/computer/" + name, ComputerWithDetails.class);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Computer computer1 = (Computer) o;

if (computer != null ? !computer.equals(computer1.computer) : computer1.computer != null)
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (displayName != null ? !displayName.equals(computer1.displayName) : computer1.displayName != null)
if (getClass() != obj.getClass())
return false;
Computer other = (Computer) obj;
if (computer == null) {
if (other.computer != null)
return false;
} else if (!computer.equals(other.computer))
return false;
if (displayName == null) {
if (other.displayName != null)
return false;
} else if (!displayName.equals(other.displayName))
return false;

return true;
}

@Override
public int hashCode() {
int result = displayName != null ? displayName.hashCode() : 0;
result = 31 * result + (computer != null ? computer.hashCode() : 0);
final int prime = 31;
int result = 1;
result = prime * result + ((computer == null) ? 0 : computer.hashCode());
result = prime * result + ((displayName == null) ? 0 : displayName.hashCode());
return result;
}

Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/offbytwo/jenkins/model/ComputerSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.offbytwo.jenkins.model;

import java.util.List;

public class ComputerSet extends BaseModel {
private int busyExecutors;
List<ComputerWithDetails> computer;
private String displayName;
private int totalExecutors;

public int getBusyExecutors() {
return busyExecutors;
}

public void setBusyExecutors(int busyExecutors) {
this.busyExecutors = busyExecutors;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public int getTotalExecutors() {
return totalExecutors;
}

public void setTotalExecutors(int totalExecutors) {
this.totalExecutors = totalExecutors;
}

public List<ComputerWithDetails> getComputer() {
return computer;
}

public void setComputer(List<ComputerWithDetails> computers) {
this.computer = computers;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + busyExecutors;
result = prime * result + ((computer == null) ? 0 : computer.hashCode());
result = prime * result + ((displayName == null) ? 0 : displayName.hashCode());
result = prime * result + totalExecutors;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ComputerSet other = (ComputerSet) obj;
if (busyExecutors != other.busyExecutors)
return false;
if (computer == null) {
if (other.computer != null)
return false;
} else if (!computer.equals(other.computer))
return false;
if (displayName == null) {
if (other.displayName != null)
return false;
} else if (!displayName.equals(other.displayName))
return false;
if (totalExecutors != other.totalExecutors)
return false;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ComputerWithDetails extends Computer {

String displayName;
List actions;
List executors;
List<Executor> executors;
Boolean idle;
Boolean jnlp;
Boolean launchSupported;
Expand All @@ -37,7 +37,7 @@ public List<Map> getActions() {
return actions;
}

public List<Map> getExecutors() {
public List<Executor> getExecutors() {
return executors;
}

Expand Down
Loading

0 comments on commit 95ade9f

Please sign in to comment.