2015-05-12 22 views
0

我在ruby中制作了一个脚本,该脚本使用带有雾宝石的Vsphere API从VM中检索信息。从具有雾中Vsphere的VM中检索runtimeInfo(Ruby)

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

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

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

我还没有找到另一种方式在vsphere lib从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 

回答

0

我找到一种方法来绕过“保护”,我不认为这是很好的做法,但它的工作,而无需使用猴子补丁:

vm = vsphere.send :get_vm_ref, vm_id