Nutainx接口的调用
该文档引用的GitHub上的一个nutianx的自动化脚本,有兴趣的可以直接查看GitHub脚本。GitHub脚本链接
此处以获取集群信息为例
#!/usr/bin/env python
#
# Copyright (c) 2014 Nutanix Inc. All rights reserved.
#
# Nutanix REST API v2
#
# This script uses Python 2.7.
# This script shows how to authenticate, create protection domains,
# and add unprotected VMs to a protection domain provided by the Prism Gateway.
# This script is ready to copy and paste for execution, but assign the
# variables within the script.
# WARNING: Be sure that when adding VMs to a protection domain, they are first
# unprotected.
# NOTE: You need a Python library called "requests" which is available from
# the url: http://docs.python-requests.org/en/latest/user/install/#install
import json
import requests
import sys
from requests.packages.urllib3.exceptions import InsecureRequestWarning
class test_rest_api():
def __init__(self):
# Initializes the options and the logfile from GFLAGS.
self.ip_addr = "cluster_ip_address"
self.username = "cluster_username"
self.password = "cluster_password"
# Base URL at which REST services are hosted in Prism Gateway.
base_url = 'https://%s:9440/PrismGateway/services/rest/v2.0/'
self.base_url = base_url % self.ip_addr
self.session = self.get_server_session(self.username, self.password)
def get_server_session(self, username, password):
# Creating REST client session for server connection, after globally
# setting authorization, content type, and character set.
session = requests.Session()
session.auth = (username, password)
session.verify = False
session.headers.update({'Content-Type': 'application/json; charset=utf-8'})
return session
# Prints the cluster information and loads JSON objects to be formatted.
def get_cluster_information(self):
cluster_url = self.base_url + "/cluster"
print("Getting cluster information for cluster %s" % self.ip_addr)
server_response = self.session.get(cluster_url)
print("Response code: %s" % server_response.status_code)
return server_response.status_code ,json.loads(server_response.text)
if __name__ == "__main__":
try:
# Start the execution of test cases.
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
ntnx_test_rest_api = test_rest_api()
#print ("hello world!")
status, cluster = ntnx_test_rest_api.get_cluster_information()
# Displays cluster authentication response and information.
print("Status code: %s" % status)
print("Text: ")
print(json.dumps(cluster,indent=2))
print("=")
# Get specific cluster elements.
print ("Name: %s" % cluster.get('name'))
print ("ID: %s" % cluster.get('id'))
print ("Cluster Ext IP Address:%s" % cluster.get('cluster_external_i_p_address'))
print ("Number of Nodes: %s" % cluster.get('num_nodes'))
print ("Version: %s" % cluster.get('version'))
#Print hypervisor type.
hypervisors = ""
for hypervisor in cluster.get('hypervisor_types'):
if( hypervisor == "kKvm" ):
hypervisor_human_name = "AHV"
elif( hypervisor == "kVMware" ):
hypervisor_human_name = "ESXi"
elif( hypervisor == "kHyperv" ):
hypervisor_human_name = "Hyper-V"
else:
hypervisor_human_name = "Unknown"
hypervisors = hypervisors + hypervisor_human_name + ','
print ("Hypervisor Types: %s" % hypervisors.rstrip(','))
except Exception as ex:
print (ex)
sys.exit(1)