vCenter 6.5 REST API w/ Python

Introduction:

So one of the new features in vSphere 6.5 is the REST API introduced into vCenter. This is particularly interesting to me as I've always liked automating things but have been pained by the windows requirement for PowerCLI (I'm also not a huge fan of powershell, despite it's usefulness).

While there is a python SDK for vCenter, it doesn't quite present the same appeal as a REST API! Anyway, I thought I'd make a post on how to start making basic requests to the API with python. It also gives me a valid use case for the code syntax highlighting I added to this blog a while back but barely use!

An excellent new feature of the vCenter API is the fact it actually comes with it's own API explorer at https://vcenterip/apiexplorer which is absolutely fantastic! Using this you can essentially grab the exact URIs and request headers required for the implementation, as well as test the request to see the construct of the JSON response, what better!

In this implementation we connect to vCenter using Basic HTTP auth, this is done in the auth_vcenter function. The authentication process is slightly strange, but essentially you send a post request with the authentication information contained, this then sends back a session ID which can be used in subsequent requests. I've not looked into the time it takes for the session ID to expire, so simply make the auth request before each API call. Obviously this is rather inefficient given the need for an additional request and response before each call but it avoids possible re-authentication situations. In theory I should also close the session after making the call to avoid re-use, but this is still proof of concept - so you can do that yourself!

Implementation:

All we're doing here is authenticating, and retrieving the health of the vCenter server.

Code:

#!/usr/local/bin/python3

import requests  
from requests.auth import HTTPBasicAuth  
import json  
from requests.packages.urllib3.exceptions import InsecureRequestWarning

api_url = 'https://10.53.228.11/rest'  
api_user = 'administrator@vsphere.local'  
api_pass = 'Testpw123!'

def main():  
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning) #Disable SSL warnings
    get_vcenter_health_status()

def get_vcenter_health_status():  
    resp = get_api_data('{}/appliance/health/system'.format(api_url))
    j = resp.json()
    print('vCenter Health: {}'.format(j['value']))

def auth_vcenter(username,password):  
    print('Authenticating to vCenter, user: {}'.format(username))
    resp = requests.post('{}/com/vmware/cis/session'.format(api_url),auth=(api_user,api_pass),verify=False)
    if resp.status_code != 200:
        print('Error! API responded with: {}'.format(resp.status_code))
        return
    return resp.json()['value']

def get_api_data(req_url):  
    sid = auth_vcenter(api_user,api_pass)
    print('Requesting Page: {}'.format(req_url))
    resp = requests.get(req_url,verify=False,headers={'vmware-api-session-id':sid})
    if resp.status_code != 200:
        print('Error! API responded with: {}'.format(resp.status_code))
        return
    return resp

main()  

Output:

dchidell@dchidell-mac:Desktop$ ./vmware_rest.py  
Authenticating to vCenter, user: administrator@vsphere.local  
Requesting Page: https://10.53.228.11/rest/appliance/health/system  
vCenter Health: green  
dchidell@dchidell-mac:Desktop$