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

  1. On your local machine, open a command-line shell.
  2. Create a vro-python-vra folder.
    mkdir vro-python-vra
  3. Navigate to the vro-python-vra folder.
    cd vro-python-vra
  4. 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, the vRAUrl input, in your script must have a corresponding input parameter called vRAUrl 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 use Properties or Composite Type return type.
  5. 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.
    1. Create a lib folder.
      mkdir lib
    2. Install the requests module.
      pip3 install requests -t lib/
  6. Add the requests module to the handler.py script.
    import requests
     
    def handler(context, inputs):
        print('Hello, your inputs were ' + inputs)
        return None
  7. 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)
  8. Define the token and vRAUrl values.
    1. Retrieve the access token by using the vRealize Automation Identity Service API. See Get Your Access Token for the vRealize Automation API
    2. For the vRAUrl value, define the script so it uses a vRealize Orchestrator input parameter with the same name.
      vRAUrl = inputs["vRAUrl"]
    3. 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 a Properties or Composite Type return type for your vRealize Orchestrator action.
  9. 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.