5 Replies Latest reply on Nov 23, 2011 7:27 AM by cdecanini_

    Get VM Guest Information From vCD 1.5 Plugin

    ggibson9 Enthusiast
    VMware Employees

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

      }

        • 1. Re: Get VM Guest Information From vCD 1.5 Plugin
          Burke- Virtuoso
          VMware Employees

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

          • 2. Re: Get VM Guest Information From vCD 1.5 Plugin
            ggibson9 Enthusiast
            VMware Employees

            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!

            • 3. Re: Get VM Guest Information From vCD 1.5 Plugin
              cdecanini_ Virtuoso
              VMware Employees

              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.

              1 person found this helpful
              • 4. Re: Get VM Guest Information From vCD 1.5 Plugin
                ggibson9 Enthusiast
                VMware Employees

                Thanks Burke and Christophe for your help!

                • 5. Re: Get VM Guest Information From vCD 1.5 Plugin
                  cdecanini_ Virtuoso
                  VMware Employees

                  You are welcome.

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

                   

                  Christophe.