Skip to content

Commit

Permalink
Merge pull request #18 from oleg-nenashev/JENKINS-25588
Browse files Browse the repository at this point in the history
[JENKINS-25588] - Fixed the "Test Connection" validation handler
  • Loading branch information
jswager committed Jan 6, 2015
2 parents 0f17d4b + 7a73a67 commit db8104d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
30 changes: 20 additions & 10 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(vsHost + "/sdk", username, 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 All @@ -170,7 +180,7 @@ public Collection<PlannedNode> provision(Label label, int excessWorkload) {
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("vSphereCloud");
sb.append("{Host='").append(vsHost).append('\'');
sb.append("{Host='").append(getVsHost()).append('\'');
sb.append(", Description='").append(vsDescription).append('\'');
sb.append('}');
return sb.toString();
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
Expand Up @@ -11,5 +11,5 @@
<f:textbox clazz="required number" />
</f:entry>
</f:advanced>
<f:validateButton title="${%Test Connection}" progress="${%Testing...}" method="testConnection" with="vsHost,vsDescription,username,password"/>
<f:validateButton title="${%Test Connection}" progress="${%Testing...}" method="testConnection" with="vsHost,vsDescription,credentialsId"/>
</j:jelly>

0 comments on commit db8104d

Please sign in to comment.