How to script a vRealize Automation 7 REST API request

As you may or may not know, vRealize Automation features a RESTful API to programmatically interface with the Cloud Management Portal. In this guide I will show you how to create a shell script to request a catalog item. You can then use this as a building block for more advanced REST calls or integrations.

Pre-Reqs:

  • A working vRA 7 Blueprint
  • A linux system with a command line that you can run curl from to connect to the vRealize Appliance API
  • An account with access to vRA 7 that can provision a blueprint

Before we script this, let’s just make sure everything is in working order and create our Request JSON file, on your Linux system with Curl we will need to set some environment variables to save us time. Let’s export the vRA appliance host name.

export VRA=vra-01a.corp.local

Create variable for the HTTP Accept header, which tells the server what format you want the response to be in.

export ACCEPT="application/json"

Here we will authenticate by requesting a bearer token via a POST against /identity/api/tokens API, and we will store the bearer token in a variable so we can use it on future API invocations. This example passes Tony’s credentials (vsphere.local tenant) with inline JSON, but you could also create variables or store this JSON in a file and use the file instead.

curl --insecure -H "Accept: application/json" -H 'Content-Type: application/json' --data '{"username":"tony@corp.local","password":"VMware1!","tenant":"vsphere.local"}' https://$VRA/identity/api/tokens

Now let’s grab the bearer token from the response above. Highlight all the text between the quotes as shown and copy it to the clip board

Now we will export the token to a variable like below

export AUTH="Bearer MTQ0OTYwMjc2MDI4NToyODlmZWM2MjZlNzNkMDAwZmRmYjp0ZW5hbnQ6dnNwaGVyZS5sb2NhbHVzZXJuYW1lOnRvbnlAY29ycC5sb2NhbGV4cGlyYXRpb246MTQ0OTYzMTU2MDAwMDo4MzE3NThmNjhjZTNjNzg5MDNiMWM3ODdlODBmMDQ3NGYzNmYwMzBkYWFjNGM4Y2I3YTk5YmY1ODBmN2E4N2IyNzM4MTRhM2M3NmQzNzM3ZTUzMmZjZDI4OGEwZTI0OTA4NTZjMDg3YTQ2NzRjMjczZTIyOTQyZGJlOWQwNDU2NQ=="

Ok, now let’s list the catalog items and locate the link to make a request. Enter the following to bring up the requests view

Notice I add a pipe on the end of the command. This makes the JSON output more readable.  | python -m json.tool

curl --insecure -H "Accept: $ACCEPT" -H "Authorization: $AUTH" https://$VRA/catalog-service/api/consumer/entitledCatalogItemViews | python -m json.tool

Now let’s locate the URL to get the request form for the CentOS6 blueprint. Highlight the URL as shown and copy to the clipboard.

Now let’s get the required JSON content to create our request by submitting a curl request and sending the output to a json file

curl --insecure -H "Accept: $ACCEPT" -H "Authorization: $AUTH" https://vra-01a.corp.local/catalog-service/api/consumer/entitledCatalogItems/00608f44-312a-476b-ad78-116b9aea661e/requests/template | python -m json.tool > /tmp/CentOS6.json

view the contents of the output json file by typing cat /tmp/CentOS6.json

Now let’s create the request. We need the request URL so enter the following again and locate the request URL highlight it and copy to the clipboard

curl --insecure -H "Accept: $ACCEPT" -H "Authorization: $AUTH" https://$VRA/catalog-service/api/consumer/entitledCatalogItemViews | python -m json.tool

Now let’s create and invoke the request using the following and press enter Note: below the items in red that you will need to input for your environment.

Note: below the items in red that you will need to input for your environment.

curl --insecure -H "Accept: application/json" -H "Authorization: $AUTH" https://vra-01a.corp.local/catalog-service/api/consumer/entitledCatalogItems/e2cd2087-bb96-444c-96d8-42c90ef00cef/requests --data @/tmp/CentOS6.json --verbose -H "Content-Type: application/json" | python -m json.tool

You can use the following to view the requests

curl --insecure -H "Accept: $ACCEPT" -H "Authorization: $AUTH" https://$VRA/catalog-service/api/consumer/requests | python -m json.tool

Or go to the portal

vRA7Request

Cool, now we verified we can make the request and we have the Request json file so let’s script it.

Type vi vrarequest.sh and press enter to create a new shell file then press i to enter insert mode and enter the following:

Notice the items in Red in the copy and paste code below that will need to be unique to your environment.

export VRA=vra-01a.corp.local
export ACCEPT="application/json"
export DATA=`curl --insecure -H "Accept: application/json" -H 'Content-Type: application/json' --data '{"username":"tony@corp.local","password":"VMware1!","tenant":"vsphere.local"}' https://$VRA/identity/api/tokens`
export TOKEN=`echo $DATA | grep -o -P '(?<="id":")[\w\d=]*(?=")'`
echo $TOKEN
export AUTH="Bearer $TOKEN"
curl --insecure -H "Accept: application/json" -H "Authorization: $AUTH" https://vra-01a.corp.local/catalog-service/api/consumer/entitledCatalogItems/e2cd2087-bb96-444c-96d8-42c90ef00cef/requests --data @/tmp/CentOS6.json --verbose -H "Content-Type: application/json" | python -m json.tool
 You can also download the file here

Hold shift and pres ZZ to save the file then execute it by typing sh vrarequest.sh and press enter

Go to the portal again to see your request in progress

You now have the building blocks to script a request. From here you may want to create input variables for the user and password or the location of the request json file.

See this Guide to get the IP adress of the provisioned VM from the API

Happy Coding!! Enjoy!

Remember, sharing is caring ->Tweet about this on Twitter2Share on Facebook0Share on LinkedIn21Share on Google+0Email this to someone

9 thoughts on “How to script a vRealize Automation 7 REST API request

  1. Pingback: How to get the IP address of a VM in vRealize Automation 7 REST API – VMtoCloud.com

  2. Hi,
    When trying to list all the catalog items, I get a response of:
    Error reportHTTP Status 401 – Authentication required

    The process was working fine yesterday, and I’ve even requested new tokens a few times using different valid credentials. Has anyone run into this? I did see a similar problem in the Feb 2015 thread for this process.

    • Did you also export the new token to the auth header again? example export AUTH=”Bearer MTQ0OTYwMjc2MDI4NToyODlmZWM2MjZlNzNkMDAwZmRmYjp0ZW5hbnQ6dnNwaGVyZS5sb2NhbHVzZXJuYW1lOnRvbnlAY29ycC5sb2NhbGV4cGlyYXRpb246MTQ0OTYzMTU2MDAwMDo4MzE3NThmNjhjZTNjNzg5MDNiMWM3ODdlODBmMDQ3NGYzNmYwMzBkYWFjNGM4Y2I3YTk5YmY1ODBmN2E4N2IyNzM4MTRhM2M3NmQzNzM3ZTUzMmZjZDI4OGEwZTI0OTA4NTZjMDg3YTQ2NzRjMjczZTIyOTQyZGJlOWQwNDU2NQ==”

  3. Hi,

    How could we find a ASD catalogs template that has to be filled so it could be used for requesting the ASD catalog?
    The above method you specified is not working for either a ASD catalog or an IaaS catalog.
    I am using vRA 6.2.4
    Any help would be appreciated…

  4. Hi,

    Great guide and thanks.
    I have an issue/question.
    How can you request multiple instances in a single deployment if “_number_of_instances”:1 is set to 2, this gives 2 deployments with 1 VM in each not 2 instances in a single deployment. VRA 7.1.

  5. Pingback: How to Delete a Deployment with the vRA 7 REST API – VMtoCloud.com

  6. Pingback: Executing Day 2 Actions with the vRA 7 REST API – VMtoCloud.com

  7. Thank you very much. The details are very well documented. I’m able follow it and all worked as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *

*