Home > vSphere API > New Licensing APIs Since VMware vSphere 4: Swap Licenses

New Licensing APIs Since VMware vSphere 4: Swap Licenses

Days ago I introduced the new licensing APIs since vSphere 4 and a sample that prints license expiration dates. Here is yet another sample that replaces an old license with a new license.

You may be wondering why anyone would do this. This is in fact not a typical use case. You probably know that vSphere Client does not treat license keys like passwords which are not visually displayed as dots or asterisks. As a result, anyone who can access a vSphere Client can write down license keys and use them elsewhere.

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.

Normally this is not an issue at all. What about a training lab? To avoid this case, you want to have license keys with short expiration dates just after a lab session. Then issue becomes how to swap an old license out with a new license for next session. For sure, you don’t want to manually change them as the assignment of a license may require a lot of clicks.

If that happens to you, this sample can be very handy. It not only replaces licenses but also assigns the new license exactly the same way as the old. As with most of VI Java API samples, you want to change the IP address, username/password before running it in your environment. In this sample, you also need to change the old and new license keys as well. I used X and Y strings as placeholder in the sample because I cannot show my licenses here. :-)

Note: If you don’t see new license key after running this sample successfully, don’t worry – the data in server should have changed. Restarting vSphere Client will show you new license key.

There may be other use cases for this sample. Please feel free to share your thoughts.

package com.vmware.vim25.mo.samples.lic;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.vmware.vim25.LicenseAssignmentManagerLicenseAssignment;
import com.vmware.vim25.LicenseManagerLicenseInfo;
import com.vmware.vim25.mo.LicenseAssignmentManager;
import com.vmware.vim25.mo.LicenseManager;
import com.vmware.vim25.mo.ServiceInstance;
/**
* @author Steve Jin (sjin at vmware.com)
*/

public class SwapLicenseV4
{
  public static void main(String[] args) throws Exception
  {
    ServiceInstance si = new ServiceInstance(new URL("https://8.8.8.8/sdk"),
        "administrator", "vmware", true);
    LicenseManager lm = si.getLicenseManager();
    LicenseAssignmentManager lam = lm.getLicenseAssignmentManager();

    LicenseAssignmentManagerLicenseAssignment[] las = lam.queryAssignedLicenses(null);

    String newLic = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX";
    String oldLic = "YYYYY-YYYYY-YYYYY-YYYYY-YYYYY";

    List entities = new ArrayList();

    for(LicenseAssignmentManagerLicenseAssignment la : las)
    {
      LicenseManagerLicenseInfo licInfo = la.getAssignedLicense();
      if(oldLic.equals(licInfo.getLicenseKey()))
      {
        entities.add(new LicEntity(la.getEntityId(), la.getEntityDisplayName()));
        System.out.println("entityId:" + la.getEntityId());

      }
    }

    lm.addLicense(newLic, null);

    for(LicEntity e : entities)
    {
      lam.updateAssignedLicense(e.entityId, newLic, e.displayName);
    }
    lm.removeLicense(oldLic);
  }
}

class LicEntity
{
  String entityId;
  String displayName;

  public LicEntity(String entityId, String displayName)
  {
    this.entityId = entityId;
    this.displayName = displayName;
  }
}

  1. No comments yet.
  1. No trackbacks yet.