目录

前言

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'

最新文章

  1. 使用 google gson 转换Timestamp或Date类型为JSON字符串.
  2. 9x9乘法表输出[Java]
  3. swift语言的学习笔记
  4. JavaIO中的Reader和writer
  5. ADHelper C#域用户操作(转)
  6. 用java程序打印菱形
  7. 利用HTML5的canvas制作万花筒动画特效
  8. 【组队训练】2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest
  9. Tyvj P1729 文艺平衡树 Splay
  10. 微信、QQ这类IM App怎么做——谈谈Websocket
  11. DateTime字段控件值显示短格式的做法
  12. 0 and 1
  13. HDU 5066 Harry And Physical Teacher(物理题)
  14. Windows 8 卡在正在检查更新
  15. elasticsearch 5.0以上不支持consistency 和 quorum
  16. 【vue】vue +element 实现批量删除
  17. 用asp.net core 把用户访问记录优化到极致
  18. NEST 之旅 · 开启
  19. 汉语言处理工具pyhanlp的简繁转换
  20. MySQL - 用户变量

热门文章

  1. 下载 Eclipse 免安装版~
  2. c# task笔记
  3. info - 阅读 info 文档
  4. 021-Zabbix4.2对IIS监控摸索记录
  5. Kubernetes集群详细介绍及部署
  6. ajax 请求成功,但是后台feigin请求超时解决方案
  7. 【HDU5289】Assignment
  8. Excel去重操作
  9. HttpClient——ESP8266HTTPClient库
  10. springboot 在idea中实现热部署