Using the Nutanix Rest API v2 with Python to list cluster VM names

Leave a comment
Share

Nutanix Rest API has always interested me as it is a very powerful tool. I have only really used it via the explorer, which allows you to test various queries and outcomes.

Rest_explorer

I wanted to see how easy it would be to obtain information from the Nutanix cluster to use in other applications andĀ ‘Real {Men/Women} don’t click’ so I fancied trying my hand at utilising the API via Python.

To kick things off I’ll obtain a list of the VM’s in the cluster this should be as easy as callingĀ the GET VM’s API.

I’ll be using the Python 3.6 interpreter, so the syntax may differ slightly on earlier versions (ie 2.x)

First I need the request module to perform the REST actions, I’m using pip to install this…

I start by creating the base url, in this example I will utilise v2.0 of the Nutanix Rest API

where 192.168.0.157 is the cluster IP address. The base URL can differ between CE and retail versions of Acropolis so check your’s using the Explorer.

I can then construct the Request with authentication and header details :

now I can request the data as JSON from the path I wish to use which are listed using the explorer :

these are appended to the base url and stored in the data variable as JSON.

Using the rest api explorer we can see what the JSON will return by clicking the ‘Try it out’ button.

My test has returned 3 items labeled ‘entities’ which have different sub attributes.

I want to return a list of all the VM’s running on my cluster so I can use the following to loop through each entity and print the attribute of ‘name’

So in the above e becomes each entity and name references the sub value within each entity and returns

which are the 3 VM’s I have running in my test cluster.

Full source code below…

Summary

The above exercise is just to demonstrate basic REST API usage and should get you started. In later posts I’ll be looking at using the API to perform actions. Stay Tuned.

 

Leave a Reply

Your email address will not be published. Required fields are marked *