从带有 Vsphere in Fog 的 VM 中检索 runtimeInfo (Ruby)

Retrieve runtimeInfo from a VM with Vsphere in Fog (Ruby)

我在 ruby 中制作了一个脚本,它使用 Vsphere API 和 Fog gem 从 VM 检索信息 gem。

我想访问 Vsphere API 中的 VirtualMachine:VirtualMachineRuntimeInfo:runtime。

我的问题是要访问此资源,我需要检索 VM 对象。我必须在 get_virtual_machine.rb 文件中使用方法 "get_vm_ref" 但它是一个受保护的方法。

我使用猴子补丁来使用这个受保护的功能,但我想知道是否有其他方法(正确的方法)来做到这一点?

我还没有在 vsphere 库中找到另一种从 VM 检索 runtimeInfo 的方法。

这是我的代码:

#Load credentials
config_par1 = YAML.load_file('config_par1.yml')
#Connexion
vsphere = Fog::Compute.new(config_par1.merge({:provider => 'Vsphere'}))
#Retrieve VM id
vm_id = vsphere.list_virtual_machines.first['id']
#Here is the problem, I use a protected method in Fog to access runtimeInfo
vm = vsphere.get_vm_ref(vm_id)
#Examples
maxCpu = vm.runtime.maxCpuUsage
maxMemory = vm.runtime.maxMemoryUsage

我找到了一种绕过 "protected" 的方法,我认为这不是一个好的做法,但它在不使用 monkey 补丁的情况下工作:

vm = vsphere.send :get_vm_ref, vm_id