Wednesday, April 22, 2020

Search vRealize Operations for a resource using Python

This write-up will show how to use REST to search for a virtual machine in vRealize Operations.  I'm using Python 3.7.3 running on a Linux platform.

The assumptions made in the script:


  • Search is for a virtual machine
  • Use the standard VMWARE adapter
  • You are using vRealize Operations Advanced or higher

Using your instance of vRealize Operations, you can access the REST documentation at https://<your vrops ip or url>/suite-api/docs/rest/index.html#getResourcePropertiesList

I used 'getResourcePropertiesList' as the link.  Below the link results you'll see "GET /api/resources".  The /api/resources will show you the multitude of search criteria you can specify.  In the sample script below, you will see that I'm using "resourceKind" and "adapterKind" as well as the "name".

Publicly, you can see the API documentation here.

If you enter an invalid name or a name that does not exist for a virtual machine, the script will let you know the search item does not exist.  If you enter a name where more than one virtual machine matches, the script will return all that match and suggest a more precise search.

When a search criteria is matched, you will see the resource id.  The resource id is used in many of vRealize Operations' REST APIs.

The script can be downloaded here.

I tested the script on vRealize Operations 8.1

#!/usr/bin/python3

#imports
import sys, json, requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

#Your vROps environment parameters
srvName = "<vrops url or ip>"
baseURL = "https://" + srvName
usrName = "<local user account>"
usrPass = "<user password>"

# Authenticate (vROps local user - consider updating script to allow for IDM)
def Authenticate():
    tokenURL = baseURL + "/suite-api/api/auth/token/acquire"
    authJSON = {"username": usrName,"authSource": "Local","password": usrPass,"others": [],"otherAttributes": {}}
    authHeaders = {"Content-Type":"application/json","Accept":"application/json"}
    authResponse = requests.post(tokenURL,data=json.dumps(authJSON),headers=authHeaders,verify=False)
    if (authResponse.status_code != 200):
        print('probably invalid credentials')
        print('Returned Status Code: ' + authResponse.status_code)
        exit
    else:
        authToken = "vRealizeOpsToken " + authResponse.json()['token']
        return authToken

# Search vROps database for the VM
def SearchVROps(token, vm_name):
    Headers = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}
    vropsURL = baseURL+"/suite-api/api/resources"+"/?name="+vm_name+"&resourceKind=VirtualMachine&adapterKind=VMWARE"
    response = requests.get(vropsURL,headers=Headers,verify=False)
    if response.json()['pageInfo']['totalCount'] == 0:
        return ('Searched term not found')
    else:
        for x in range(int(response.json()['pageInfo']['totalCount'])):
            print (response.json()['resourceList'][x]['resourceKey']['name']+": "+response.json()['resourceList'][x]['identifier'])
        return ('Searched term was successful')

def Logout(token):
    releaseURL = baseURL + "/suite-api/api/auth/token/release"
    authHeaders = {"Content-Type":"application/json","Authorization":token,"Accept":"application/json"}
    authResponse = requests.post(releaseURL,headers=authHeaders,verify=False)

authToken = Authenticate()
vm_name = input("Enter VM Name to search: ")
srchResult = SearchVROps(authToken, vm_name)
print(srchResult)
Logout(authToken)

Examples of running the script:
jwhite@vm-linux:~/bin$ ./search.py 
Enter VM Name to search: badname
Searched term not found

jwhite@vm-linux:~/bin$ ./search.py 
Enter VM Name to search: win
VM-WIN10: 8d37e9eb-19fa-43c1-8e8a-902597322efb
Win2012: b9bc6577-d645-42b1-a32a-2640f2840cc9
vm-win7: c836c3bd-a7b9-4b93-91a6-200a3f974e3a
Searched term was successful

jwhite@vm-linux:~/bin$ ./search.py 
Enter VM Name to search: vm-win10
VM-WIN10: 8d37e9eb-19fa-43c1-8e8a-902597322efb
Searched term was successful

No comments:

Post a Comment