VMware API Too many virtual machines. Add more filter criteria to reduce the number.

虚拟化系统 0 877 团子精英 收藏

环境

VMware :6.7

虚拟机数量:1000+

问题:使用API请求vm list的时候报错

{"type":"com.vmware.vapi.std.errors.unable_to_allocate_resource","value":
{"messages":[{"args":[],"default_message":"Too many virtual machines. Add 
more filter criteria to reduce the number.","id":"com.vmware.api.vcenter.vm.too_many_matches"}]}}

解决思路:

一、查看API限制

在开发人员中心-API管理器中查看vcenter-vm的API请求方式和说明

vmtoo1.jpg

看说明显示:

Returns information about at most 1000 visible (subject to permission checks) virtual machines in vCenter matching the VM.FilterSpec.

意思是最多显示1000条vm信息,如果超过1000条,则需要添加参数进行筛选。

二、找到/reset/vcenter/vm的API官方说明地址:

https://developer.vmware.com/apis/vsphere-automation/v7.0U2-deprecated/vcenter/rest/vcenter/vm/get/

但是官方却显示如下:

Returns information about at most 4000 visible (subject to permission checks) virtual machines in vCenter matching the VM.FilterSpec.

我觉得可能是我的vcenter的版本太低了导致。暂且先不管他

vmtoo2.jpg

查看filter的具体请求参数有哪些。

为了方便,我们这里采用resource_pools,前提是已经把所有的虚拟机都进行了资源池分类,资源池可以在虚拟机的页面查询到

vmtoo3.jpg

三、添加参数请求

添加已test_pool为参数开始请求API

curl -H "vmware-api-session-id: 3e828asdfqwer23423f934jh2" \
https://192.168.1.100/rest/vcenter/vm?filter.resource_pools=test_pool -k

结果返回:

{"value":[]}

肯定是有什么不对。。

查看具体说明:

Resource pools that must contain the virtual machine for the virtual machine to match the filter. 
If unset or empty, virtual machines in any resource pool match the filter. When clients pass a 
value of this structure as a parameter, the field must contain identifiers for the resource 
type: ResourcePool. When operations return a value of this structure as a result, the field 
will contain identifiers for the resource type: ResourcePool.

由此可见,使用filter参数来进行请求的时候,不能以显示的名字为具体参数内容,要以vm本身的唯一标识符为标准。

四、查找resource_pools的唯一标识符

官方地址:

https://developer.vmware.com/apis/vsphere-automation/v7.0U2-deprecated/vcenter/rest/vcenter/resource-pool/get/

查看请求方式,与vm相同

curl -H "vmware-api-session-id: 3e828asdfqwer23423f934jh2" \
https://192.168.1.100/rest/vcenter/resource_pools -k

返回如下:

{"value":[{"name":"test_pool","resource_pool":"resgroup-11111"}]}

由此可以确认,test_pool的唯一标识符为resgroup-11111

五、再次请求

curl -H "vmware-api-session-id: 3e828asdfqwer23423f934jh2" \
https://192.168.1.100/rest/vcenter/vm?filter.resource_pools=resgroup-11111 -k

返回成功(结果仅供参考):

{"value":[{"memory_size_MiB":8192,"vm":"vm-1011","name":"test_vm", \
"power_state":"POWERED_ON","cpu_count":4},{"memory_size_MiB":4096,"vm":"vm-1012", \
"name":"test_vm2","power_state":"POWERED_ON","cpu_count":4}]}

综上:

使用vcenter在进行任何请求的时候,都需要将显示名称转换为唯一标识符。切记


相关推荐:

网友留言:

您需要 登录账户 后才能发表评论

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
验证码