This is actually a script I built to test out various things at once. I was testing (and learning) how the vRealize Automation REST API calls work. There are other good resources on the web for getting started. While not a vRealize Automation example, I enjoyed this blog entry on Python and vSphere REST API calls with Python. Between that, and the official documention, and some Postman experimentation, I was able to get things going.
The script itself as I use it is currently called within a simple Flask application, and served up by a NGINX container.
Some other notes:
- Import Credentials requires you to make a simple python file with a dictionary I called “login.”
1234login = {'user': "marcus@vsphere.local",'password': "SuperSecretPW"} - You need a bearer token to authenticate with vRA. So the first function grabs one.
- Main work is done in the “get_resource_views,” where we grab all of the VMs created, get the information we desire, and then sort it.
- “get_csv” creates a .csv file that I then save. I link to the .csv file on the internal web page so that the report can be downloaded by those that need it.
The code can probably be tweaked further, but this should be enough to get most up and running. I welcome any and all comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/usr/local/bin/python3 import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) import json import csv from app import credentials api_user = credentials.login['user'] api_pass = credentials.login['password'] api_tenant = "vsphere.local" api_url = "https://ausvra7app.us.dell.com" def get_bearer_token(): payload = {'username':'{}'.format(api_user),'password':'{}'.format(api_pass),'tenant':'{}'.format(api_tenant)} response = requests.post('{}/identity/api/tokens'.format(api_url), json=payload,verify=False) return response.json()['id'] def get_resource_views(): api_token = get_bearer_token() #for query below proper format of filter in api test was --> resourceType/id eq 'Infrastructure.Virtual' query_url = "/catalog-service/api/consumer/resourceViews?withExtendedData=true&page=1&limit=60&%24filter=resourceType%2Fid%20eq%20'Infrastructure.Virtual'" response = requests.get(api_url+query_url,headers={'authorization' : 'Bearer %s' %api_token},verify=False) #convert to dict data_dict = response.json() #only concerned with everything under the 'content' key vm_list = data_dict['content'] #sort by vm name for now vm_list.sort(key=lambda e: e['name']) get_csv(vm_list) return vm_list def get_csv(a_list): with open('./app/static/csv/vra_vms.csv','w',newline='') as csvfile: reportwriter = csv.writer(csvfile,delimiter=',', quoting=csv.QUOTE_MINIMAL) reportwriter.writerow(['VM Name','Owner','CPU','Memory','OS','Provisioned On','Lease Ends']) for vm in a_list: reportwriter.writerow([vm['name'],vm['owners'],vm['data']['MachineCPU'],vm['data']['MachineMemory'],vm['data']['MachineGuestOperatingSystem'],vm['lease']['start'][:10],vm['data']['MachineExpirationDate']]) if __name__ == "__main__": get_resource_views() |