Home > vSphere API > New Licensing APIs Since VMware vSphere 4: Check License Expiration

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.

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.

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