Home > vSphere API > Really Easy Way to Clone a Session: Yet Another Contribution from NetApp

Really Easy Way to Clone a Session: Yet Another Contribution from NetApp

April 26th, 2010 Leave a comment Go to comments

Recently there were questions in the vSphere Java API forum on how to clone a new session from an existing one. Although vSphere Java API wraps around the basic Web Services API cloneSession() method, simply calling the method doesn’t get what you want.

Why? The signature of cloneSession() is as follows. It returns UserSession, which is embedded inside ServiceInstance managed object in vSphere Java API. While using the API, you always starts from the ServiceInstance.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

public UserSession cloneSession(String cloneTicket) throws InvalidLogin, RuntimeFault, RemoteException;

In vSphere Java API, the ServiceInstance and UserSession have one to one mapping. Without a new ServiceInstance around the new UserSession, the UserSession is not much helpful.

Luckily, we got yet another contribution from Eric Forgette who works at NetApp. The contributed code includes a new overloaded method cloneSession() method to return a ServiceInstance object.

As I described before, we’ve been trying very hard to keep the vSphere Java API as close to the basic Web Services as possible. In this case, we decided to break the rule a little bit for good reasons, mainly for usability.

  /**
   * Copyright 2009 NetApp, contribution by Eric Forgette
   * Modified by Steve Jin (sjin at vmware.com)
   *
   * This constructor builds a new ServiceInstance based on a ServiceInstance.
   * The new ServiceInstance is effectively a clone of the first.  This clone will
   * NOT become invalid when the first is logged out.
   *
   * @author Eric Forgette (forgette at netapp.com)
   * @throws RemoteException
   * @throws RuntimeFault
   * @throws InvalidLogin
   * @throws MalformedURLException
   *
   */
      public ServiceInstance cloneSession(boolean ignoreCert) throws InvalidLogin, RuntimeFault, RemoteException, MalformedURLException
      {
        ServiceInstance oldsi = getServerConnection().getServiceInstance();
        ServerConnection oldsc = oldsi.getServerConnection();
        String ticket = oldsi.getSessionManager().acquireCloneTicket();
        VimPortType vimService = new VimPortType(oldsc.getUrl().toString(), ignoreCert);
        vimService.getWsc().setVimNameSpace(oldsc.getVimService().getWsc().getVimNameSpace());
        ServerConnection newsc = new ServerConnection(oldsc.getUrl(), vimService, null);
        ServiceInstance newsi = new ServiceInstance(newsc);
        newsc.setServiceInstance(newsi);
       UserSession userSession = newsi.getSessionManager().cloneSession(ticket); 
       newsc.setUserSession(userSession);
       return newsi;
} 

 When you use the new API, you can just call the following to have your sessions cloned:

ServiceInstance newInstance = Si.getSessionManager().cloneSession(true);

The fix is only available in the code repository. You can download the following two files (ServerConnection.java, SessionManager.java) until the next release.

Thanks Eric and NetApp for the contributions! For the VMware community, please don’t hesitate to contact me (sjin at users.sourceforge.net) if you want to contribute any code that are useful to others in the community.

Categories: vSphere API Tags: ,
  1. Eric Forgette
    April 30th, 2010 at 10:45 | #1

    Hi Steve, I’m happy to help.
    Cheers,
    -Eric

  1. No trackbacks yet.