7 Replies Latest reply on Apr 5, 2018 12:46 PM by balsz

    Get VM name from request ID

    Uridium454 Novice

      Is it possible to retrieve the VM name of a newly provisioned vm from the catalog item request.id?  I know you can get the value from a known machine with the following:


      System.getModule("com.vmware.library.vcac").getPropertyFromVirtualMachine(host, objVCACVirtualMachine, "__Cafe.Root.Request.Id")


      I am hoping for something along the lines of (poorly formed query, but giving the idea):

           Server.findForType("vCAC:VirtualMachine","__Cafe.Root.Request.Id" == requestID);


      At the moment, the only known is the request ID, however if there is a better way to retrieve the name of the newly created VM I would love to know.  From there I can perform the rest of the post provisioning tasks I need to take.


      Any help would be greatly appreciated!

        • 1. Re: Get VM name from request ID
          jasnyder Expert

          One way is to do this, assuming you have the vCACCAFEHost in the vcachost variable and the request id in a variable called requestId:


          var client = vcachost.createCatalogClient();


          var response = client.get("/consumer/requests/" + req.id + "/resources").getBodyAsJson();

          var resources = new Array();


          for(var x in response.content)


              var resource = response.content[x];


              if(resource.resourceTypeRef.id == "Infrastructure.Virtual") {

                  resources.push(vCACCAFEEntitiesFinder.getCatalogResource(vcachost, resource.id));

                  var moRef = "";

                  for(var k in resource.resourceData.entries) {

                      var property = resource.resourceData.entries[k];


                      if(property.key == "EXTERNAL_REFERENCE_ID") moRef = property.value.value;


                  System.log(resource.name + " - " + resource.id + " ; moRef = " + moRef);




          for(var r in resources) {

              System.log(resources[r].name + " - " + resources[r].id);




          This is getting an array of vCACCAFECatalogResource.  I also included how to extract the managed object reference for a vSphere VM because I don't believe that is available on the resource objects returned by the finder without then retrieving entity properties, which would be an unnecessary step given that you already get them from the REST call.


          This is scoped to only find resources that are vSphere VMs, so it will not grab software components, for example.  That is what the "if(resource.resourceTypeRef.id == "Infrastructure.Virtual")" check is for.  You could remove that condition and it will grab all resources regardless of type.


          Sample Output:

          [2017-12-13 20:22:56.825] [I] MBG1-73 - 2ba2caf1-afe5-4eed-93c4-6b2a65b1aaa3 ; moRef = vm-12291

          [2017-12-13 20:22:56.826] [I] MBG1-73 - 2ba2caf1-afe5-4eed-93c4-6b2a65b1aaa3

          • 2. Re: Get VM name from request ID
            Uridium454 Novice

            Justin, this is simply brilliant.  I have been messing around with the code you provided, but seem to be running into a snag.  I keep getting the following error from the line var response = client.get("/consumer/requests/" + req.id + "/resources").getBodyAsJson();:


            Scriptable task (item1)#2) SyntaxError: Unexcpected character in string: '\''


            I have peeled back each of the bits of this line and it seems to be hanging up on the final bit of the string for the request ( "/resources" ).  I have tried hard coding in a request ID as follows, and get the same error.  Example with the request ID hard coded in: var response = client.get("/consumer/requests/e68f83bf-205d-41dc-8f24-4c37ea2fe6ae/resources").getBodyAsJson();





            • 3. Re: Get VM name from request ID
              jasnyder Expert

              The server may be responding with an error of some kind.  The call to client.get should be returning a vCACCAFEServiceResponse object. You can try inspecting the properties on that object and see if that yields any info.


              Try logging the result as string, either:


              System.log(client.get("/consumer/requests/" + req.id + "/resources"));




              System.log(client.get("/consumer/requests/" + req.id + "/resources").getBodyAsString());


              Just to check - what version of vRA/vRO are you using?

              • 4. Re: Get VM name from request ID
                Uridium454 Novice

                I am currently running version


                I was able to successfully get a response back with both of the below system.log options.  The exact request ID and the desired name value is shown below.


                         "@type": "CatalogResource",

                         "id": "ab5ea1f9-cd65-4c71-b2ef-1aa5e9adf1f4",

                         "iconId": "Infrastructure.CatalogItem.Machine.Virtual.vSphere",

                         "resourceTypeRef": {

                            "id": "Infrastructure.Virtual",

                            "label": "Virtual Machine"


                         "name": "entdoaeis0053",

                         "description": "PRD - Wireshark Packet Capture Server",

                         "status": "ACTIVE",

                         "catalogItem": null,

                         "requestId": "e68f83bf-205d-41dc-8f24-4c37ea2fe6ae",

                         "providerBinding": {

                            "bindingId": "fdabc0c8-2c49-49fb-b7ba-c99039129f60",

                            "providerRef": {

                               "id": "5493e8fd-db07-4541-9ac4-d748c7c200e8",

                               "label": "Infrastructure Service"




                I can get these results until I add the .getBodyAsJson() onto the end of the client.get() request.  When I look at the type being returned without this, I am getting a type of function which makes parsing a bit interesting.

                • 5. Re: Get VM name from request ID
                  jasnyder Expert

                  So the error seems to indicate that there is an invalid character (backslash - '\') in the JSON preventing it from parsing correctly.  It's not clear why the server would be responding with malformed JSON.  Sounds like maybe an improperly escaped string or something.


                  You can take the output of the getBodyAsString() call and put it into a JSON.parse call:


                  var response = JSON.parse(client.get("/consumer/requests/" + req.id + "/resources").getBodyAsString());


                  I suspect you will get the same error, but it's worth a shot.


                  You can also try to scan the JSON contents for the offending backslash and see if it's something you can easily fix by replacing it before parsing, etc.


                  Beyond that, you can try copying and pasting the full JSON contents into a JSON validator like this one - https://jsonformatter.curiousconcept.com/


                  It makes it a little easier to find the JSON formatting issues.  Be sure to scrub anything sensitive out of whatever you put in there - never know what they might be doing with the info.

                  • 6. Re: Get VM name from request ID
                    Uridium454 Novice

                    I found the culprit!  In one of my actions descriptions there was a you'll and the ' was being escaped with a \.  I greatly appreciate your help, and would gladly buy you dinner if you were here.  You have saved me a great deal of headache.  Two thumbs up! 

                    • 7. Re: Get VM name from request ID
                      balsz Novice

                      @Uridium454Same problem for me as well. How did you remove the "\"? I am getting error something like below when I replace after stringify


                      TypeError: Cannot find function replace in object DynamicWrapper (Instance) :