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.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

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;
  }
}

This entry was posted in vSphere API and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.