Friday, July 12, 2013

Nutanix REST-API using python Requests - Get container info

From:
http://docs.python-requests.org/en/latest/

Installing Pip and Requests on Nutanix Controller VM/or any Linux system:
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
 sudo python get-pip.py
sudo pip install requests
sudo pip install requests --upgrade
 cd /usr/lib/python2.6/site-packages/;sudo chmod -R 755 requests-1.2.3-py2.6.egg/
PYTHONPATH=${PYTHONPATH}:/usr/lib/python2.6/site-packages/requests-1.2.3-py2.6.egg;export PYTHONPATH


Sample Script:
 cat test_resp.py - to print container info

#!/usr/bin/python
import json as json
import requests

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

  print s.get(base_url + 'vstores', verify=False).json()

if __name__ == "__main__":
  main()


Output

./test_resp.py
[{u'protectionDomain': None, u'name': u'ctr3', u'backedup': False, u'markedForRemoval': False, u'id': 2482, u'containerId': 2482}, {u'protectionDomain': None, u'name': u'ctr4', u'backedup': False, u'markedForRemoval': False, u'id': 2483, u'containerId': 2483}, {u'protectionDomain': u'ctr5_1372277619664', u'name': u'ctr5', u'backedup': True, u'markedForRemoval': False, u'id': 1898767, u'containerId': 1898767}, {u'protectionDomain': None, u'name': u'test', u'backedup': False, u'markedForRemoval': False, u'id': 2430343, u'containerId': 2430343}, {u'protectionDomain': None, u'name': u'testing', u'backedup': False, u'markedForRemoval': False, u'id': 2430528, u'containerId': 2430528}, {u'protectionDomain': None, u'name': u'testStats', u'backedup': False, u'markedForRemoval': False, u'id': 18973731, u'containerId': 18973731}, {u'protectionDomain': None, u'name': u'dummyCTR_to_delete1', u'backedup': False, u'markedForRemoval': False, u'id': 19113654, u'containerId': 19113654}]


Get the name and size of storage pool:

#!/usr/bin/python
import json as json
import requests

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

  data = s.get(base_url + 'storage_pools', verify=False).json()
  spname= data ["entities"][0]["name"]
  size= data ["entities"][0]["capacity"]
  print spname,size/(1024*1024*1024*1024)
if __name__ == "__main__":
  main()