Jul 12 2016

How to use Python with Nutanix REST API – Part 1

Nutanix has been built with a strong set of APIs for managing all cluster aspects and AHV virtual machines. REST API’s can be called from a browser and also from pretty much any existing language out there. I have been using mostly Python Language to create my toys and prototypes, so I decided to share some instructions and code about how to use Python to connect to a Nutanix Cluster and manipulate objects. If you are not familiar with Python Language I recommend this Python For Beginners; Python is very easy to learn!

 

If you are trying to connect to Nutanix API with an untrusted SSL certificate, you will get the InsecureRequestWarning: Unverified HTTPS. Adding certificate verification is strongly advised, but if you want to bypass the SSL certificate you will need to execute following code snippet.

def __supress_security():
    # supress the security warnings
    requests.packages.urllib3.disable_warnings()

 

In the below function we define a standard http request with the input parameter being the base_url request. Note that I am passing username and password to the Nutanix cluster inside the function. I recommend using a pre-defined global variable or opt for entering them also as parameters. The resulting set of the API call is then parsed into JSON format with the entire result set and returned to the triggering function.

def __htpp_request(base_url):
    # supress the security warnings
    __supress_security()

    s = requests.Session()
    s.auth = ('username', 'password')
    s.headers.update({'Content-Type': 'application/json; charset=utf-8'})

    return json.dumps(s.get(base_url, verify=False).json(), sort_keys=True)

 

Here is an example of how you would create a function to return all hosts in a Nutanix cluster. The function require the Cluster IP as a parameter and the base_url is created inside the function. The return command will call __htpp_request with the base_url.

def __request_hosts(cluster_ip_address):
    base_url = "https://" + cluster_ip_address + API_PRISM + "/hosts/"
    return __htpp_request(base_url)

 

Here is another example of a function to request a list with all VMs in a cluster.

def __request_vms(cluster_ip_address):
    base_url = "https://" + cluster_ip_address + API_PRISM + "/vms/"
    return __htpp_request(base_url)

 

To avoid code repetition I am building the base_url variable using the CONSTANT API_PRISM. In my example you would need to set the the CONSTANT with the URL in advance. Currently there are two different Nutanix URIs to be known, one for PRISM that include all cluster management functionalities and another for managing AHV virtual machines.

# Nutanix API URI
API_AHV = ':9440/api/nutanix/v0.8'
API_PRISM = ':9440/PrismGateway/services/rest/v1'

 

Sometimes can be difficult to know the base_url for a specific Nutanix call, but with a little help from the REST API Explorer Live! it’s easy to identify the exact base_url for the calls you have in mind. REST API Explorer Live! is available in every cluster in PRISM.

 

Finally, you will need make sure you are installing and importing the requests and json libraries. You can do that manually or add as part of your code in the main Function. If you decide to add as part of your code  ‘pip install –user -U pip’  must be executed beforehand.

# install and import modules
# 'pip install --user -U pip' must be execute beforehand
__install_and_import_package('requests')
__install_and_import_package('json')

 

In my next post I will demonstrate how to parse Nutanix JSON results and how to call a http_post to execute and modify objects.

 

How to use Python with Nutanix REST API – Part 2 has already been published here.

 

This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.

Older posts «

» Newer posts