New Licensing APIs Since VMware vSphere 4: Check License Expiration

In my previous blog, I introduced the new licensing APIs since vSphere 4. As promised, I will write samples showing how to use the APIs.

Here is the first sample (stay tuned to next one, coming soon). What it does is to check the licenses in vCenter server for their expiration dates, and print them out in the console. You can of course save them into other format, say an CSV file so that you can use Excel to further analyze it. To run the sample, you must change the IP address to the vCenter server, the username/password, as would with most VI Java API samples.

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.

Note that a license could be an evaluation license, whose expiration date has to be retrieved from the evaluation property. For a normal license, it’s possible that it never expires. This sample handles this case as well.

package com.vmware.vim25.mo.samples.lic;
import java.net.URL;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.vmware.vim25.KeyAnyValue;
import com.vmware.vim25.LicenseManagerEvaluationInfo;
import com.vmware.vim25.LicenseManagerLicenseInfo;
import com.vmware.vim25.mo.LicenseManager;
import com.vmware.vim25.mo.ServiceInstance;
<pre>/**
* @author Steve Jin (sjin at vmware.com)
*/</pre>
public class PrintLicExpirationDateV4
{
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();
LicenseManagerLicenseInfo[] lics = lm.getLicenses();

DateFormat df = DateFormat.getInstance();
for(LicenseManagerLicenseInfo lic : lics)
{
Date expDate = null;

if("eval".equalsIgnoreCase(lic.getEditionKey()))
{
LicenseManagerEvaluationInfo evalInfo = lm.getEvaluation();
expDate = getExpirationDate(evalInfo.getProperties());
}
else
{
expDate = getExpirationDate(lic.getProperties());
}

if(expDate!=null)
{
System.out.println(lic.getLicenseKey() + " expires on " + df.format(expDate));
}
else
{
System.out.println(lic.getLicenseKey() + " expires on NEVER");
}
}
}

public static Date getExpirationDate(KeyAnyValue[] kavs)
{
if(kavs!=null)
{
for(KeyAnyValue kav : kavs)
{
if("expirationDate".equalsIgnoreCase(kav.getKey()))
{
Object val = kav.getValue();
if(val instanceof Calendar)
{
Calendar expDate = (Calendar) val;
return expDate.getTime();
}
}
}
}

return null;
}
}

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

One Trackback

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.