VMware Cloud Community
anoopvb
Enthusiast
Enthusiast

Is there an example of a workflow that reaches out to an API?

Thus far, I've been building workflows that call out to powershell commands that reach out to a restful API that accepts and responds with JSON.

and those workflows work very well as is with a powershell module but there's some overhead there that comes with the use of the powershell module and reaching out to a powershell host locally or remotely.

So that got me thinking about being able to communicate directly with the API from within vRO. Are there any examples of how to make http/https calls directly?

thank you.

6 Replies
sbeaver
Leadership
Leadership

Start looking around at the REST and SOAP plugin and you should be able to find some examples for connection to all sorts of different systems from CMDB to IP ect

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
anoopvb
Enthusiast
Enthusiast

Thank you.

Is there no way to do this natively?

Is there an HTTP/S library built in that might allow me to get/post requests and then parse out the JSON that's received?

thank you again.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

Well, there is a built-in scripting class URL which allows you to send basic GET/PUT/POST/DELETE requests, but it is quite primitive (eg. you don't have access to request headers). Take a look at it in vRO API Explorer to check if it suits your needs.

As for the parsing out the received JSON - you don't need this; Javascript works natively with JSON. Take a look at eg. JSON.stringify() and JSON.parse()

sushantghige
Contributor
Contributor

For calling REST APIs, you will need to add the REST HOST using REST plugin and then you can perform the operations like below:

var request = this.host.createRequest(method, requestUrl, body);

if (headers) {

for (var key in headers) {

if (headers.hasOwnProperty(key))

request.setHeader(key, headers[key]);

}

}

var response = request.execute();

 

Where host will be your REST HOST object, which you can obtain by iterating all the rest hosts

var registeredHosts = Server.findAllForType('REST:RESTHost');

for (var i=0; i<registeredHosts.length; i++) {

if (registeredHosts[i].name == <YOURHOSTNAME>){

registeredHost = registeredHosts[i];

}

}

var request = this.host.createRequest(method, requestUrl, body || '');
    if (headers) {
        for (var key in headers) {
            if (headers.hasOwnProperty(key))
                request.setHeader(key, headers[key]);
        }
    }
    var response = request.execute();
sushantghige
Contributor
Contributor

Adding to previous post. You can ignore the headers part it was specific for me. Also for adding a REST HOST there is workflow called add Rest host
anoopvb
Enthusiast
Enthusiast

Thank you for that information. I would definitely need to modify the headers as part of this API access! I'll check out the Rest plugin.

Reply
0 Kudos