Nutainx接口的调用

Nutainx接口的调用

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值