Nutanix REST API with python

After some very effective new hire training sessions and some first customer meetings I thought it could be a good idea to wipe off the dust and do some python stuff. Maybe you know that Nutanix has a beautiful REST API which you can use to develop great things like portal solutions or integrations. I thought it´s a good option to have a short way to see all protected and controller VM´s managed by PRISM.

Using the REST API Explorer in PRISM showed up some really easy GET calls to the VMs managed by Nutanix. It also gave me some attributes to get more informations (like controllerVm or protectedDomainName). With these I build this simple command-line application:

import json 
import requests

def main():
  requests.packages.urllib3.disable_warnings()
 
  base_url = "https://yourCVM:9440/PrismGateway/services/rest/v1/"
  s = requests.Session()
  s.auth = ('admin', 'admin')
  s.headers.update({'Content-Type': 'application/json; charset=utf-8'})

  print "\nPlease make your selection:\n"

  selection = raw_input("1) Protected VMs.\n2) Controller VMs:\n\nSelection: ")

  data = s.get(base_url + 'vms', verify=False).json()

  for e in data["entities"]:
      if (selection=="1"):
          if (e["protectionDomainName"] != None):
              print e["vmName"]
              print "This is a protected VM in group: ",e["protectionDomainName"]
      if (selection=="2"):
          if (e["controllerVm"] == True):
              print e["vmName"]
              print "This is a Controller VM."
  
if __name__ == "__main__":
  main()

When starting this from the command-line you can select the protected VMs (1) or the Controller VMs (2).

Selection_Screen

Just a quick example, more to come…

 

Leave a Reply