This article covers a scenario where you need to point and work with vAPI endpoint entirely with JavaScript. It wasn’t as flawless as I expected and I spent quite some time to figure it out. Let’s jump in.
A while back Angel Iliev wrote 2 very detailed articles covering vAPI and working with tags in vSphere:
vSphere 6 Automation with vRO 7 – Tags and vAPI – Part I – The Virtualist
vSphere Automation with vRO 7 – Tags and vAPI – Part II – The Virtualist
Usually, when using the vAPI endpoint from a workflow, you can add it as an attribute with a predefined value so the end users would not need to select it at run time. That’s really handy for all types of known or constant attributes. However, recently I had to work with vSphere tags only via vRO action element. As you know, you can’t have an attribute with predefined value in an action element as you do in a workflow. So basically, I needed variable pointing to my VAPIEndpoint object.
First stop of course – the API explorer. Expanding the vAPI plug in, seems like the VAPI Manager class has a function called findEndpoint, which would give me exactly what I’m looking for:
It accepts single argument of type string which should be the vAPI endpoint URL. Great, that’s easy. The vAPI endpoint URL is in the format: https://<vCenter FQDN or IP>:443/api
Now passing this as argument of the findEndpoint function and printing the variable to see what we got in return:
var myEndpoint = VAPIManager.findEndpoint("https://192.168.1.204:443/api"); System.log("My endpoint: "+myEndpoint);
And here is the result:
Well, null was not what I expected. I started different approach – reverse engineering. I added an attribute of type VAPI:VAPIEndpoint to the workflow and selected my endpoint as a value:
Looking again at the API explorer, the VAPIEndpoint class has property named endpointUrl which will return the URL as a string – exactly what the findEndpoint function needed.
I was interested what would it look like, so first just printed it out:
System.log(endpoint.endpointUrl);
Here is the result:
Exactly what I’ve passed to the findEndpoint function before without success.
After some time spent with google trying to find something relevant, I came back to vRO and printed out the entire VAPIEndpoint object:
System.log(endpoint);
And the result:
Here I noticed something in the square brackets containing the “known properties”: ENDPOINT–https___192.168.1.204_443_api
It does contain the vAPI endpoint URL in a little bit weird format, but that was enough for me to give it a try:
var myEndpoint = VAPIManager.findEndpoint("ENDPOINT--https___192.168.1.204_443_api"); System.log("My endpoint: "+myEndpoint);
And voila:
Now I got what I needed and I was able to continue working on my action element.
I find the description of the findEndpoint function misleading, as using the URL would not get you any result. My current setup is:
vRO – 7.1.0
vAPI plugin – 7.1.0
I hope this would save you the time I spent wondering what is wrong. If you notice different behavior in older or newer versions of the plugin please share!
Ivaylo Ivanov
Latest posts by Ivaylo Ivanov (see all)
- Runecast Analyzer plugin for vRO - September 11, 2017
- Unattended Deployment and Configuration of OVA - August 8, 2017
- Point to vAPI Endpoint with JavaScript in vRO - February 15, 2017
In the 7.4 version of the vAPI plugin, the string that you discovered no longer works. Now you need to specify the endpoint URL, as the documentation suggests.