Skip to content

Commit

Permalink
[JENKINS-25588] - Improve error checks for missing host/username
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Jan 6, 2015
1 parent 4d74a1d commit 7a73a67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/main/java/org/jenkinsci/plugins/vSphereCloud.java
Expand Up @@ -50,7 +50,7 @@ public class vSphereCloud extends Cloud {
@Deprecated
private transient String password;
private final int maxOnlineSlaves;
private VSphereConnectionConfig vsConnectionConfig;
private @CheckForNull VSphereConnectionConfig vsConnectionConfig;

private transient int currentOnlineSlaveCount = 0;
private transient Hashtable<String, String> currentOnline;
Expand Down Expand Up @@ -137,11 +137,11 @@ public String getVsDescription() {
return vsDescription;
}

public String getVsHost() {
public @CheckForNull String getVsHost() {
return vsConnectionConfig != null ? vsConnectionConfig.getVsHost(): null;
}

public VSphereConnectionConfig getVsConnectionConfig() {
public @CheckForNull VSphereConnectionConfig getVsConnectionConfig() {
return vsConnectionConfig;
}

Expand All @@ -153,7 +153,17 @@ public final int getHash() {
}

public VSphere vSphereInstance() throws VSphereException{
return VSphere.connect(getVsHost() + "/sdk", getUsername(), getPassword());
// TODO: validate configs
final String effectiveVsHost = getVsHost();
if (effectiveVsHost == null) {
throw new VSphereException("vSphere host is not specified");
}
final String effectiveUserName = getUsername();
if (effectiveUserName == null) {
throw new VSphereException("vSphere username is not specified");
}

return VSphere.connect(effectiveVsHost + "/sdk", effectiveUserName, getPassword());
}

@Override
Expand Down Expand Up @@ -269,18 +279,18 @@ else if (vsHost.endsWith("/")) {
}

final VSphereConnectionConfig config = new VSphereConnectionConfig(vsHost, credentialsId);
final String username = config.getUsername();
final String password = config.getPassword();
final String effectiveUsername = config.getUsername();
final String effectivePassword = config.getPassword();

if (StringUtils.isEmpty(username)) {
if (StringUtils.isEmpty(effectiveUsername)) {
return FormValidation.error("Username is not specified");
}

if (StringUtils.isEmpty(password)) {
if (StringUtils.isEmpty(effectivePassword)) {
return FormValidation.error("Password is not specified");
}

VSphere.connect(vsHost + "/sdk", username, password);
VSphere.connect(vsHost + "/sdk", effectiveUsername, effectivePassword);

return FormValidation.ok("Connected successfully");
} catch (Exception e) {
Expand Down
Expand Up @@ -29,12 +29,14 @@
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.mo.VirtualMachineSnapshot;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class VSphere {
private final URL url;
private final String session;

private VSphere(String url, String user, String pw) throws VSphereException{
private VSphere(@Nonnull String url, @Nonnull String user, @CheckForNull String pw) throws VSphereException{

try {
//TODO - change ignoreCert to be configurable
Expand All @@ -51,9 +53,10 @@ private ServiceInstance getServiceInstance() throws RemoteException, MalformedUR

/**
* Initiates Connection to vSphere Server
* @param server Server URL
* @throws VSphereException
*/
public static VSphere connect(String server, String user, String pw) throws VSphereException {
public static VSphere connect(@Nonnull String server, @Nonnull String user, @CheckForNull String pw) throws VSphereException {
return new VSphere(server, user, pw);
}

Expand Down

0 comments on commit 7a73a67

Please sign in to comment.