Use command-line arguments to connect to a vSphere server and log in.
Given the arguments on the command line, this task makes an SSL connection to the vSphere server, retrieves a reference to the SessionManager, and authenticates a session with the credentials passed on the command line.
Prerequisites
For this task you need:
- The Web Services API WSDL bindings file.
- The hostname of the vSphere server.
- A username and password to authenticate with the vSphere server.
- The name of a virtual machine available to be cloned.
Procedure
- Import the vSphere Web Services API libraries and the necessary Java (and JAX-WS connection, bindings, and SOAP) libraries:
import com.vmware.vim25.*;
import java.util.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPFaultException;
- Create a class to hold data and methods.
public class cloneVMTask {
static ManagedObjectReference pCollector;
static ManagedObjectReference viewMgr;
static ServiceContent serviceContent;
static VimPortType methods;
- Define a TrustManager implementation.
Authentication is handled by using a TrustManager and supplying a host name verifier method.
Note: For the purposes of this example, this TrustManager implementation will accept all certificates. This is only appropriate for a development environment. Production code should implement certificate validation.
private static class TrustAllTrustManager
implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
String authType)
throws java.security.cert.CertificateException {
return;
}
} // end TrustAllTrustManager
- Read command line arguments.
public static void main(String [] args) throws Exception {
String serverName = args[0];
String userName = args[1];
String password = args[2];
String vmName = args[3];
String url = "https://"+serverName+"/sdk/vimService";
- Create SSL context and session context for the connection.
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
trustAllCerts[0] = tm;
// Create the SSL context
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
// Create the session context
javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
// Initialize the contexts; the session context takes the trust manager.
sslsc.setSessionTimeout(0);
sc.init(null, trustAllCerts, null);
// Use the default socket factory to create the socket for the secure connection
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- Set a host name verifier that will enable any connection.
The host name verifier is invoked during the SSL handshake.
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
- Create a VimService object to obtain a VimPort binding provider.
The BindingProvider provides access to the protocol fields in request/response messages. Retrieve the request context which will be used for processing message requests.
VimService vimService = new VimService();
methods = vimService.getVimPort();
Map<String, Object> ctxt = ((BindingProvider) methods).getRequestContext();
- Store the Server URL in the request context.
Specify true to maintain the connection between the client and server. The client API will include the Server's HTTP cookie in its requests to maintain the session. If you do not set this to true, the Server will start a new session with each request.
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
- Create a MOref for the ServiceIntsance and retrieve the ServiceContent.
// -- ManagedObjectReference for the ServiceInstance on the Server
ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
SVC_INST_REF.setType("ServiceInstance");
SVC_INST_REF.setValue("ServiceInstance");
serviceContent = methods.retrieveServiceContent(SVC_INST_REF);
- Use the SessionManager to log in.
methods.login(serviceContent.getSessionManager(),
userName,
password,
null);
- Get references to the PropertyCollector and the ViewManager.
pCollector = serviceContent.getPropertyCollector();
viewMgr = serviceContent.getViewManager();
What to do next
With an authenticated session, you can use the PropertyCollector to retrieve the MOref of the virtual machine, then create clones and monitor the
Task
objects:
ManagedObjectReference vmRef = getVmRef( vmName );
cloneVM( vmRef );
A best practice is to close the session when done:
methods.logout(serviceContent.getSessionManager());
} // end main
} // end cloneVMTask