Search code examples
rubyvmwarefogvsphere

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


I made a script in ruby which retrieves informations from VM using the Vsphere API with the Fog gem.

I'd like to access to VirtualMachine:VirtualMachineRuntimeInfo:runtime in Vsphere API.

My problem is that to access this resource I need to retrieve the VM object. I have to use the methode "get_vm_ref" in get_virtual_machine.rb file but its a protected method.

I used a monkey patch to use this protected function but I'd like to know if there is another way (a proper way) to do it?

I havent find another way in the vsphere lib to retrieve runtimeInfo from a VM.

Here is my code:

#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

Solution

  • I find a way to bypass the "protected", I dont think this is good practice but it's working without using monkey patch:

    vm = vsphere.send :get_vm_ref, vm_id