提交 b21ab921 编写于 作者: P Pavel Janousek 提交者: Oliver Gondža

[JENKINS-35570] List of computer's names extracted to Util (#2408)

* [JENKINS-35570] List of computer's names extracted to Util
上级 e750cd35
......@@ -24,9 +24,7 @@
package hudson;
import jenkins.util.SystemProperties;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import hudson.Proc.LocalProc;
......@@ -54,8 +52,6 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
......@@ -70,7 +66,6 @@ import java.nio.charset.CharsetEncoder;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.NotLinkException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
......@@ -95,8 +90,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
/**
* Various utility methods that don't have more proper home.
......
......@@ -26,13 +26,12 @@ package hudson.cli;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Computer;
import org.acegisecurity.AccessDeniedException;
import hudson.model.ComputerSet;
import hudson.util.EditDistance;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
......@@ -75,10 +74,7 @@ public class ConnectNodeCommand extends CLICommand {
if(computer == null) {
if(names == null) {
names = new ArrayList<String>();
for (Computer c : jenkins.getComputers())
if (!c.getName().isEmpty())
names.add(c.getName());
names = ComputerSet.getComputerNames();
}
String adv = EditDistance.findNearest(node_s, names);
throw new IllegalArgumentException(adv == null ?
......
......@@ -26,12 +26,12 @@ package hudson.cli;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.ComputerSet;
import hudson.util.EditDistance;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
......@@ -73,12 +73,7 @@ public class DisconnectNodeCommand extends CLICommand {
if (computer == null) {
if (names == null) {
names = new ArrayList<String>();
for (Computer c : jenkins.getComputers()) {
if (!c.getName().isEmpty()) {
names.add(c.getName());
}
}
names = ComputerSet.getComputerNames();
}
String adv = EditDistance.findNearest(node_s, names);
throw new IllegalArgumentException(adv == null ?
......
......@@ -27,16 +27,14 @@ package hudson.cli;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.ComputerSet;
import hudson.util.EditDistance;
import jenkins.model.Jenkins;
import org.acegisecurity.AccessDeniedException;
import org.kohsuke.args4j.Argument;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
/**
* @author pjanouse
......@@ -67,12 +65,7 @@ public class OnlineNodeCommand extends CLICommand {
computer = jenkins.getComputer(node_s);
if (computer == null) {
if (names == null) {
names = new ArrayList<String>();
for (Computer c : jenkins.getComputers()) {
if (!c.getName().isEmpty()) {
names.add(c.getName());
}
}
names = ComputerSet.getComputerNames();
}
String adv = EditDistance.findNearest(node_s, names);
throw new IllegalArgumentException(adv == null ?
......
......@@ -1528,11 +1528,11 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces
Jenkins h = Jenkins.getInstance();
Computer item = h.getComputer(name);
if (item==null) {
List<String> names = new ArrayList<String>();
for (Computer c : h.getComputers())
if (c.getName().length()>0)
names.add(c.getName());
throw new CmdLineException(null,Messages.Computer_NoSuchSlaveExists(name,EditDistance.findNearest(name,names)));
List<String> names = ComputerSet.getComputerNames();
String adv = EditDistance.findNearest(name, names);
throw new IllegalArgumentException(adv == null ?
hudson.model.Messages.Computer_NoSuchSlaveExistsWithoutAdvice(name) :
hudson.model.Messages.Computer_NoSuchSlaveExists(name, adv));
}
return item;
}
......
......@@ -49,6 +49,7 @@ import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
......@@ -410,6 +411,21 @@ public final class ComputerSet extends AbstractModelObject implements Describabl
}, 10, TimeUnit.SECONDS);
}
/**
* @return The list of strings of computer names (excluding master)
* @since TODO
*/
@Nonnull
public static List<String> getComputerNames() {
final ArrayList<String> names = new ArrayList<String>();
for (Computer c : Jenkins.getInstance().getComputers()) {
if (!c.getName().isEmpty()) {
names.add(c.getName());
}
}
return names;
}
private static final Logger LOGGER = Logger.getLogger(ComputerSet.class.getName());
static {
......
......@@ -23,6 +23,12 @@
*/
package hudson.model;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
import hudson.cli.CLI;
......@@ -76,4 +82,13 @@ public class ComputerSetTest {
cli.close();
}
}
@Test
public void getComputerNames() throws Exception {
assertThat(ComputerSet.getComputerNames(), is(empty()));
j.createSlave("aNode", "", null);
assertThat(ComputerSet.getComputerNames(), contains("aNode"));
j.createSlave("anAnotherNode", "", null);
assertThat(ComputerSet.getComputerNames(), containsInAnyOrder("aNode", "anAnotherNode"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册