Often in VRA environment we have multiple catalog Items where we want to Filter on basis of Service or On basis of the name of CatalogItem .
It is an easy task using GUI as well , But some of us want to achieve using Rest API and get the required details which would help in triggering the build using API.
I have decided not to go with multiple class and I have added this to my VRA Master class to have all methods in single class.
Note: You can either apply the Filter for catalog name or Service Name
import requests
import json
from prettytable import PrettyTable
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class vraclass:
def __init__(self, vraurl, username, password, tenant):
self.username = username
self.password = password
self.vraurl = vraurl
self.tenant = tenant
def vralogin(self):
url = f"https://{self.vraurl}/identity/api/tokens"
payload = f'{{"username":"{self.username}","password":"{self.password}","tenant":"{self.tenant}"}}'
headers = {
'accept': "application/json",
'content-type': "application/json",
}
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
outp = response.json()['id']
token = "Bearer " + outp
return token
def list_tenant(self):
mainurl = f"https://{self.vraurl}/identity/api/tenants"
vraheaders = {
'accept': "application/json",
"authorization": self.vralogin()
}
output = requests.request("GET", mainurl, headers=vraheaders, verify=False)
result = output.json()['content']
resulTable = PrettyTable(['Type', 'ID', 'URLName', 'Name', 'Description', 'ContactEmail', 'DefaultTenant'])
print(resulTable)
for i in result:
resulTable.add_row(
(i['@type'], i['id'], i['urlName'], i['name'], i['description'], i['contactEmail'], i['defaultTenant']))
print(resulTable)
def create_tenant(self, tenantname, urlName, Description, ContactEmail):
url = f"https://{self.vraurl}/identity/api/tenants/{urlName}"
payload = f'{{"@type":"Tenant","id":"{tenantname}","urlName":"{urlName}","name":"{tenantname}","description":"{Description}","contactEmail":"{ContactEmail}","defaultTenant":"false"}}'
headers = {
'accept': "application/json",
'content-type': "application/json",
"authorization": self.vralogin()
}
output = requests.request("PUT", url, data=payload, headers=headers, verify=False)
def list_catalog(self, catalogname=None,ServiceName=None):
if not (catalogname is None) and (ServiceName is None):
url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews?$filter=name+eq+%27{catalogname}%27"
elif (catalogname is None) and not (ServiceName is None):
url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews?$filter=service/name+eq+%27{ServiceName}%27"
elif (catalogname is None) and (ServiceName is None):
url = f"https://{self.vraurl}/catalog-service/api/consumer/entitledCatalogItemViews"
headers = {
'accept': "application/json",
'authorization': self.vralogin(),
'content-type': "application/json"
}
output = requests.request("GET", url, headers=headers, verify=False).json()['content']
view = PrettyTable(
['TenantName', 'CatalogName', 'CatalogItemID', 'Description', 'CreationDate', 'LastModified', 'ServiceName',
'RequestTemplateLink', 'SubmitRequestLink'])
for i in output:
view.add_row(
(i['entitledOrganizations'][0]['tenantLabel'], i['name'], i['catalogItemId'], i['description'],
i['dateCreated'], i['lastUpdatedDate'], i['serviceRef']['label'], i['links'][0]['href'],
i['links'][1]['href'])
)
return print(view)
Highlighted code is the piece of code for Listing the tenant .
How to use the above code
1. Copy paste the code in your Python editor and make sure it has access to VRA URL.
2. Use the below command to Instantiating classes in Python or In simple words call the class for the first time.
obj= vraclass('vrafqdn', 'administrator@vsphere.local', 'Vrapassword', "vsphere.local")
Now we have initialize the class .We can use the define method dynamically .
Sample to call the list catalog method with No filter
As you see in below screenshot , I am calling the method with no argument and I can see all the catalog items available in my VRA environment service and all other details like Request template link and Submit request link also fetched due to small screen I have pasted another screenshot.
obj.list_catalog()



Sample to Filter the catalog item using catalogname
obj.list_catalog(catalogname='Linux-CentOS')

Sample to Filter the catalog item using Service Name:
obj.list_catalog(ServiceName='VMwareCode-Service2')
