VMware Cloud Community
ggibson9
VMware Employee
VMware Employee
Jump to solution

Get VM Guest Information From vCD 1.5 Plugin

I’m trying to get guest information from the vCD 1.5 plugin without much luck.  I’m trying to get the guest host name, IP, etc.  What is the best way to get this information?  I’m looking at the module: com.vmware.library.vCloud.vApp.VM, but I can’t find that.

Also, as another question, I want get a list Vcloud.VMs from a vApp.  I found the code in the documentation to get a list of VMs, but how do I cast what is returned as a vCloud VM?

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

     }

     resultSet = resultSet.getNextPage();

}

Reply
0 Kudos
1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee
Jump to solution

When exploring vCO to get information that is not readily available in the library of workflows and actions, look for the object in question in the API Explorer and see what kind of properties and methods are avialable.

For example, the vCloud:VApp object has a method called .getChildrenVms() that returns an array of (vCloud:VM) objects.

From there, look at the vCloud:VM object in the api to see what properties are available.

I won't make you work too hard here: Assuming your code is using "vm" as the variable name for a vCloud:VM object, the following will give you some good info:

var curVMName = vm.name;
var OSSection = vm.getOperatingSystemSection();
var osName = OSSection.description.value;
System.log("OS: "+osName);
var isWindows = (osName.toLowerCase().indexOf("windows") != -1);
System.log("Is Windows? "+isWindows);
System.log("CPU Count: "+vm.getCpu().noOfCpus);
System.log("Memory (MB): "+vm.getMemory().memorySize);
var disks = vm.getDisks();
for each (disk in disks){
    if (disk.isHardDisk()){
        System.log("Name: "+disk.itemResource.elementName.value);
        System.log("Type: "+disk.itemResource.resourceType.value);
        System.log("Id: "+disk.itemResource.instanceID.value);
        System.log("Disk Size (MB): "+disk.hardDiskSize);
    }
}

System.log("\n==== NETWORK CONNECTION INFO ====");
var network = vm.getNetworkConnectionSection();
System.log("Primary Network Connection Index: "+network.primaryNetworkConnectionIndex);
var nets = network.networkConnection.enumerate();
for each (net in nets){
    System.log("External IP Address: "+net.externalIpAddress);
    System.log("IP Address: "+net.ipAddress);
    System.log("IP Address Allocation Mode: "+net.ipAddressAllocationMode);
    System.log("isConnected: "+net.isConnected);
    System.log("mACAddress: "+net.mACAddress);
    System.log("needs Customization: "+net.needsCustomization);
    System.log("Network: "+net.network);
    System.log("Network Connection Index: "+net.networkConnectionIndex);
    var otherAttribs = net.otherAttributes.keys;
}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

Reply
0 Kudos
5 Replies
Burke-
VMware Employee
VMware Employee
Jump to solution

When exploring vCO to get information that is not readily available in the library of workflows and actions, look for the object in question in the API Explorer and see what kind of properties and methods are avialable.

For example, the vCloud:VApp object has a method called .getChildrenVms() that returns an array of (vCloud:VM) objects.

From there, look at the vCloud:VM object in the api to see what properties are available.

I won't make you work too hard here: Assuming your code is using "vm" as the variable name for a vCloud:VM object, the following will give you some good info:

var curVMName = vm.name;
var OSSection = vm.getOperatingSystemSection();
var osName = OSSection.description.value;
System.log("OS: "+osName);
var isWindows = (osName.toLowerCase().indexOf("windows") != -1);
System.log("Is Windows? "+isWindows);
System.log("CPU Count: "+vm.getCpu().noOfCpus);
System.log("Memory (MB): "+vm.getMemory().memorySize);
var disks = vm.getDisks();
for each (disk in disks){
    if (disk.isHardDisk()){
        System.log("Name: "+disk.itemResource.elementName.value);
        System.log("Type: "+disk.itemResource.resourceType.value);
        System.log("Id: "+disk.itemResource.instanceID.value);
        System.log("Disk Size (MB): "+disk.hardDiskSize);
    }
}

System.log("\n==== NETWORK CONNECTION INFO ====");
var network = vm.getNetworkConnectionSection();
System.log("Primary Network Connection Index: "+network.primaryNetworkConnectionIndex);
var nets = network.networkConnection.enumerate();
for each (net in nets){
    System.log("External IP Address: "+net.externalIpAddress);
    System.log("IP Address: "+net.ipAddress);
    System.log("IP Address Allocation Mode: "+net.ipAddressAllocationMode);
    System.log("isConnected: "+net.isConnected);
    System.log("mACAddress: "+net.mACAddress);
    System.log("needs Customization: "+net.needsCustomization);
    System.log("Network: "+net.network);
    System.log("Network Connection Index: "+net.networkConnectionIndex);
    var otherAttribs = net.otherAttributes.keys;
}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
Reply
0 Kudos
ggibson9
VMware Employee
VMware Employee
Jump to solution

Thanks for the quick reply, it is much appreciated.

For the VM, I’m specifically looking for the guest host name from VM tools as it is possible a user can change the guest name and the vCD name will not get updated.  How would I get this?

I did look at getChildrenVMs, but I got the error below from that.

Code
  var vAppChildren=new Array();
  var vAppChildren = vApp.getChildrenVMs();

Result:
  [2011-11-22 15:40:12.850] [I] Property named 'getChildrenVMs' not found on object : VclVApp


Code
  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("VM: " + records[i].name);
  }
  resultSet = resultSet.getNextPage();
  }

Result:
  [2011-11-22 15:41:32.721] [I] 3 records found
  [2011-11-22 15:41:32.721] [I] VM: testc
  [2011-11-22 15:41:32.721] [I] VM: testa
  [2011-11-22 15:41:32.722] [I] VM: testb

Thanks again for any help!

Reply
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

First look at the vApp methods & properties:

Screen shot 2011-11-23 at 11.23.48 AM.png

You can see it is vApp.getChildrenVms();

To avoid doing typos press ctrl espace after the "." : This will bring the completion window like this:

Screen shot 2011-11-23 at 11.27.50 AM.png

Now that you have got the VMs in the vApp you need to get their guestCustomization:

Screen shot 2011-11-23 at 11.29.33 AM.png

Once you have identified the right method to do it, you need to check VclGuestCustomizationSection to know what properties are available.

Screen shot 2011-11-23 at 11.21.20 AM.png

In your case computerName.

To get guest information:

var vms = vApp.getChildrenVms();

for each (var vm in vms) { //iterating the vms
     var getGuestCustomizationSection = vm.getGuestCustomizationSection();
     System.log(getGuestCustomizationSection.computerName);
}

The query service can be useful to filter some objects out of all objects of a given type. It will not return vCD objects but it can return references or IDs to these that can be resolved to vCD objects using vclHost.getEntityByReference or vclHost.getEntityById. I will certainly write a dedicated article for this.

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
ggibson9
VMware Employee
VMware Employee
Jump to solution

Thanks Burke and Christophe for your help!

Reply
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

You are welcome.

i took the opportunity to turn this message into a blog article so it can help others.

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
Reply
0 Kudos