Get vm id from IP

Hi there,

I’m doing some refactoring of the opennebula-metadata module, and would like to know what’s the quickest way to get the vmid, if having the IP address of a virtual machine.

Currently I’m doing as follows:

flags = OpenNebula::Pool::INFO_ALL || OpenNebula::VirtualMachinePool::INFO_NOT_DONE
vmpool = VirtualMachinePool.new(@oneadmin_client, flags)
vmpool.info
vmpool.each do |vm|
   return vm.id if  vm["TEMPLATE/NIC/IP"] == ip
end

Transversing all the vmpool for every single attribute the metadata server returns is not something I consider a nice and efficient implementation.

Thanks,
Ricardo

I don’t think there is another way to do what you want. Pool info command does not allow you to filter by any column. You may make it faster filtering only VMs in ACTIVE state (3) as I suppose those are the only ones that will query the metadata server.

http://docs.opennebula.org/5.2/integration/system_interfaces/api.html#one-vmpool-info

Instead of each loop you can also use xpath but I’m not sure it will be faster:

http://docs.opennebula.org/doc/5.2/oca/ruby/OpenNebula/XMLElement.html#[]-instance_method