您现在的位置是:首页 > 文章详情

Python Module_oslo.vmware_连接 vCenter

日期:2016-08-21点击:555

目录

前言

oslo.vmware 是一个由 Python 实现的 vCenter 连接驱动 , 提供了连接并操作 vCenter 的 Method . 如果希望应用 Openstack 来接管 vCenter 的资源(调用 vSphere SDK) 的话,那么 olso.vmware 将会是一个不错的选择 .

注意: 本篇的代码需要一个测试用的 vCenter 环境

vSphere Web Service SDK

Install osls.vmware

cd /opt/stack git clone https://github.com/openstack/oslo.vmware --branch stable/liberty cd oslo.vmware sudo pip install -r requirements.txt -e .

测试连接 vCenter
vim vsdk.py

from oslo_vmware import api from oslo_vmware import vim_util # Get a handle to a vSphere API session session = api.VMwareAPISession( 'vCenter_IP', 'vCenter_username', 'vCenter_password', 1, 0.1) # Get MO of type "HostSystem" result1 = session.invoke_api( vim_util, 'get_objects', session.vim, 'HostSystem', 100) print result1 print "="*50 # Get information by properties of MO object rep2 = session.invoke_api(vim_util,'get_object_properties_dict',session.vim, result1.objects[0].obj,'vm') print "*"*50 print rep2

How to use the vSphere Web Service SDK

下面给出一些基本的使用方法(不需要 Openstack 环境) :

# 建立与 vCenter 的 Session 连接。 In [83]: session = api.VMwareAPISession( '200.X.102.7', 'root', 'vmware', 1, 0.1) # session.invoke_api() method of oslo_vmware.api.VMwareAPISession instance # :param module: module corresponding to the VIM API call # :param method: method in the module which corresponds to the VIM API call # :param args: arguments to the method # :param kwargs: keyword arguments to the method # :returns: response from the API call # :raises: VimException, VimFaultException, VimAttributeException, # VimSessionOverLoadException, VimConnectionException # 调用 API 获取一个 MO, 使用这个 MO 来进一步获取别的信息 # Return instance of suds.sudsobject.RetrieveResult In [83]: hostSystem = session.invoke_api(vim_util, # Module 'get_objects', # Method session.vim, 'HostSystem', # Managed Object(SDK) 100) # hostSystem.objects[0].obj == # (obj){ # value = "host-9" # _type = "HostSystem"  # } # 有 _type 属性,可以在 SDK 中找到 "HostSystem" 的 Description, # 并且根据 Description 提供的 Properties 进一步获取别的信息。 EG. vm In [83]: vmResponse = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim, hostSystem.objects[0].obj, 'vm') # vmResponse['vm'].ManagedObjectReference[0] == # (ManagedObjectReference){ # value = "vm-15" # _type = "VirtualMachine" # }, # 有 _type 属性,可以在 SDK 中找到 "VirtualMachine" 的 Description, # 并且根据 Description 提供的 Properties 进一步获取别的信息。 EG. summary In [83]: summaryResponse = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim, vmResponse['vm'].ManagedObjectReference[0], 'summary') # 当 Output 非常多时,可以使用 IPython 的特殊用法来实现过滤(Python 和 Shell 结合)。 In [118]: test = summaryResponse[0] In [117]: !echo '$test' | grep '_type' _type = "VirtualMachine" _type = "VirtualMachine" _type = "HostSystem" # summaryResponse[0].obj == # (obj){ # value = "vm-15" # _type = "VirtualMachine" # } # 有 _type 属性,可以在 SDK 中找到 "VirtualMachine" 的 Description, # 并且根据 Description 提供的 Properties 进一步获取别的信息。 EG. config In [126]: configResponse = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim, summaryResponse[0].obj, 'config') # 可以继续获取别的信息 In [205]: text = configResponse In [206]: !echo '$text' | grep _type _type = "Datastore" _type = "Datastore" _type = "Network" _type = "Network" _type = "Network" _type = "Network" 

使用小结:
1. session.invoke_api() 传递 oslo.vmware 提供的 method 到 vCenter
2. method: get_objects 获取 MO 对象
3. method: get_object_properties_dict 获取 MO 对象的属性信息的字典 EG. 获取 HostSystem 的 MO 对象
4. MO 对象中含有 _type 属性的对象, 其 _type 的值能在 SDK 中找到对应的 Properties
5. 将含有 _type 属性的对象和对应的 SDK 中的 Properties 值传递给 session.invoke_api() 方法就可以获取相应的值
EXAMPLE:

session.invoke_api(vim_util, 'get_object_properties_dict', session.vim, hostSystem.objects[0].obj, # hostSystem.objects[0].obj 对象中含有 _type 属性, 其值为 HostSystem 'vm') # 在 SDK 中找到 HostSystem MO type ,提供了Properties: 'vm'
原文链接:https://yq.aliyun.com/articles/237363
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章