Create a sample script that uses Python to call the vRealize Automation Project API.
Prerequisites
Verify that you have installed Python 3 and the PIP package installer. See Python Downloads page and Python Package Index.
Procedure
- On your local machine, open a command-line shell.
- Create a
vro-python-vra
folder.mkdir vro-python-vra
- Navigate to the
vro-python-vra
folder.cd vro-python-vra
- Create a Python script called handler.py.
touch handler.py
The handler.py script must define one function that accepts two arguments, the context of the vRealize Orchestrator workflow run, and the bound vRealize Orchestrator inputs.def handler(context, inputs): print('Hello, your inputs were ' + inputs) return None
Note: Using standard logging libraries, everything you log in the action that uses your script is also shown in the workflow log. The inputs and the return of your script must have corresponding input parameters and return types configured in the vRealize Orchestrator Client. For example, thevRAUrl
input, in your script must have a corresponding input parameter calledvRAUrl
in the vRealize Orchestrator Client. Similarly, if your script returns a string value, the return type configured in the vRealize Orchestrator Client must also be a string type. If your action returns a complex object, you can useProperties
orComposite Type
return type. - Install the Python requests module.
Important: Third-party dependency modules must be installed in a root level folder in your main
vro-python-vra
script folder. For this use case, you create a lib folder for your requests module.- Create a lib folder.
mkdir lib
- Install the requests module.
pip3 install requests -t lib/
- Create a lib folder.
- Add the requests module to the handler.py script.
import requests def handler(context, inputs): print('Hello, your inputs were ' + inputs) return None
- Create a GET request to the vRealize Automation Project API.
token = '' vRAUrl = '' r = requests.get(vRAUrl + '/iaas/api/projects', headers={'Authorization': 'Bearer ' + token}) print('Got response ' + r.text)
- Define the
token
andvRAUrl
values.- Retrieve the access token by using the vRealize Automation Identity Service API. See Get Your Access Token for the vRealize Automation API
- For the
vRAUrl
value, define the script so it uses a vRealize Orchestrator input parameter with the same name.vRAUrl = inputs["vRAUrl"]
- Add the new values to the handler.py file.
import requests def handler(context, inputs): token = 'ACCESS_TOKEN' vRAUrl = inputs["vRAUrl"] r = requests.get(vRAUrl + '/iaas/api/projects', headers={'Authorization': 'Bearer ' + token}) print('Got response ' + r.text) return r.json()
Note: Because the response from the vRealize Automation Project API is returned in a JSON format, use aProperties
orComposite Type
return type for your vRealize Orchestrator action.
- Create a ZIP package that contains the handler.py file and lib folder of your request module.
zip -r --exclude=*.zip -X vro-python-vra.zip .
What to do next
Import the PowerShell script into a vRealize Orchestrator action. See Create Actions in the vRealize Orchestrator Client.