You can cut, paste, and adapt the JavaScript examples to help you write scripts for common vCloud Director tasks.

For more information about scripting, see the vCenter Orchestrator Developer's Guide.

Get Records for All Enabled Organizations

The following JavaScript example uses the AdminQueryService object to get records for all enabled organizations on a given vCloud Director instance.

var host = ...
var queryService = host.toAdminObject().getAdminQueryService();
 
var expression = new VclExpression(VclQueryOrgField.ISENABLED, "true", VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);
 
var resultSet = queryService.queryOrgRecords(params);
while (resultSet != null) {
    // the records should be of type related to the query
    // in this case the type is VclQueryResultOrgRecord
    var records = resultSet.getRecords();
    System.log(records.length + " records found");
    for (var i = 0; i < records.length; i++) {
 System.log(records[i].name);
    }
    if (resultSet.hasNextPage()) {
        resultSet = resultSet.getNextPage();
    } else {
 break;
    }
}

Get References to All Enabled Organizations

The following JavaScript example uses the AdminQueryService object to get references to all enabled organizations on a given vCloud Director instance.

var host = ...
var queryService = host.toAdminObject().getAdminQueryService();

var expression = new VclExpression(VclQueryOrgField.ISENABLED, "true", VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var resultSet = queryService.queryOrgReferences(params);
while (resultSet != null) {
     // the type of the references is predefined - VclReference
     var references = resultSet.getReferences();
     System.log(references .length + " references found");
     for (i = 0; i < references.length; i++) {
          System.log(references[i].href);
     }
     if (resultSet.hasNextPage()) {
        resultSet = resultSet.getNextPage();
     } else {
        break;
     }
}

Get Records for All Virtual Machines Inside a vApp

The following JavaScript example uses the QueryService object to get records for all virtual machines inside a vApp.

var vapp = ...

var queryService = vapp.getHost().getQueryService();

var expression = new VclExpression(VclQueryVMField.CONTAINER, vapp.getReference().href, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var resultSet = queryService.queryRecords(VclQueryRecordType.ADMINVM, params);
while (resultSet != null)  {
     var records = resultSet.getRecords(new VclQueryResultAdminVMRecord());
     System.log(records.length + " records found");
     for (i = 0; i < records.length; i++) {
          System.log(records[i].name);
     }
     if (resultSet.hasNextPage()) {
        resultSet = resultSet.getNextPage();
     } else {
        break;
     }
}

Resume a Blocking Task Related to a vApp Deployment Notification

With the following JavaScript example, you can resume a blocking task related to a vApp deployment notification.

var host = ...
var message = ...

var helper = new VclNotificationHelper();
helper.setMessage(message);

if (helper.getNotificationEventType() == VclEventType.VAPP_DEPLOY) {
     var vappLink = helper.getEntityLink();
     var vapp = host.getEntityById(vappLink.type, vappLink.id);
     // do something with the vApp ...

     if (helper.isBlockingTask()) {
          var taskLink = helper.getBlockingTaskLink();
          var task = host.getEntityById(taskLink.type, taskLink.id);
          task.resume("put the resuming message here");
     }
}

Configure a DHCP Service on a vApp Network

With the following JavaScript example, you can configure a DHCP service on a vApp network.

var dhcpService = ... 
var vapp = ... 
var networkName = ... 
var networkConfigSection = vapp.getNetworkConfigSection(); 
var found = false; 
var existingNetworkConfigArray = networkConfigSection.networkConfig.enumerate(); 
for (index = 0; index < existingNetworkConfigArray.length; index++) { 
 var networkConfig = existingNetworkConfigArray[index];
 if (networkConfig.networkName == networkName) {
   var networkConfiguration = networkConfig.configuration;		
     if (networkConfiguration.fenceMode == VclFenceModeValuesType.BRIDGED.value) {
      throw 'Dhcp service cannot be applied to network "' + networkName + '"!';
      }
       if (networkConfiguration.features == null) {
        networkConfiguration.features = new VclNetworkFeatures();
        }
        var serviceSet = networkConfiguration.features.networkService;
        var services = serviceSet.find(new VclDhcpService());
         if (services.length > 0) {
          for (i = 0; i < services.length; i++) {
           serviceSet.remove(services[i]);
          }
         }
        serviceSet.add(dhcpService);
        found = true;
   }
}
if (!found) {
   throw 'Network "' + networkName + '" does not exist!';
}
task = vapp.updateSection(networkConfigSection);

Configure a DHCP Service on a Gateway

With the following JavaScript example, you can configure a DHCP service on a gateway.

var gatewayDhcpService = ...
var gateway = ....
var gatewayConfiguration = gateway.configuration;
if (gatewayConfiguration.edgeGatewayServiceConfiguration == null) {
      gatewayConfiguration.edgeGatewayServiceConfiguration = new VclGatewayFeatures();
}
var serviceSet = gatewayConfiguration.edgeGatewayServiceConfiguration.networkService;
var services = serviceSet.find(new VclGatewayDhcpService());
if (services.length > 0) {
     for (i = 0; i < services.length; i++) {
          serviceSet.remove(services[i]);
     }
}
serviceSet.add(gatewayDhcpService);
task = gateway.update();

Add a VPN Endpoint

With the following JavaScript example, you can add a VPN endpoint to a gateway.

var vpnEndpoint = ...
var gateway = ...
var gatewayConfiguration = gateway.configuration;
if (gatewayConfiguration.edgeGatewayServiceConfiguration == null) {
							gatewayConfiguration.edgeGatewayServiceConfiguration = new VclGatewayFeatures();
}
var serviceSet = gatewayConfiguration.edgeGatewayServiceConfiguration.networkService;
var services = serviceSet.find(new VclGatewayIpsecVpnService());
if (services.length == 0) {
							throw 'VPN service not found on gateway ' + gateway.name;
}
services[0].endpoint.add(vpnEndpoint);
gateway.configuration = gatewayConfiguration;
task = gateway.update();

Obtain Performance Statistics

With the following JavaScript example, you can obtain performance statistics for the vCloud Director plug-in.

VclProfiler.enableInstanceCounters(true);
...
var instances = VclProfiler.getInstanceCount("VclReference");
System.log("references: " + instances);
...
VclProfiler.enableInstanceCounters(false);
...
var host = ...
var cacheHitCount = VclProfiler.getCacheHitCount(host);
System.log("cache hits: " + cacheHitCount);
var cacheMissCount = VclProfiler.getCacheMissCount(host);
System.log("cache misses: " + cacheMissCount);
var cacheObjCount = VclProfiler.getCacheObjectCount(host);
System.log("cache objects: " + cacheObjCount);
...

Get the Plug-In Version

With the following JavaScript example, you can get the plug-in version and distinguish future plug-in versions.

var version = VclHostManager.getVersion();
if (version == "5.5") {
   System.log("Plug-in 5.5 is installed!");
}

Get Entity Instance Count

With the following JavaScript example, you can get any class instance count.

VclProfiler.enableInstanceCounters(true);
...
var className = VclProfiler.getClassNameByFinderType(VclFinderType.HOST);
System.log(VclProfiler.getInstanceCount(className));
...
VclProfiler.enableInstanceCounters(false);