From bb9a4e8ef723b5855b9a5088a5080deda42aafa1 Mon Sep 17 00:00:00 2001 From: Matthew Black Date: Wed, 16 Jan 2013 11:11:43 -0500 Subject: [PATCH] [vsphere] searching for VM improved to search whole cluster instead of current folder. --- .../requests/compute/get_virtual_machine.rb | 9 +++++-- .../requests/compute/list_virtual_machines.rb | 26 +++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/fog/vsphere/requests/compute/get_virtual_machine.rb b/lib/fog/vsphere/requests/compute/get_virtual_machine.rb index 63578b19c..0d57ed1d6 100644 --- a/lib/fog/vsphere/requests/compute/get_virtual_machine.rb +++ b/lib/fog/vsphere/requests/compute/get_virtual_machine.rb @@ -3,6 +3,7 @@ module Fog class Vsphere class Real def get_virtual_machine(id, datacenter_name = nil) + # The larger the VM list the longer it will take if not searching based on UUID. convert_vm_mob_ref_to_attr_hash(get_vm_ref(id, datacenter_name)) end @@ -16,13 +17,17 @@ module Fog else # try to find based on VM name if dc - get_raw_datacenter(dc).find_vm(id) + get_vm_by_name(id, dc) else - raw_datacenters.map { |d| d.find_vm(id) }.compact.first + raw_datacenters.map { |d| get_vm_by_name(id, d["name"])}.compact.first end end vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found") end + def get_vm_by_name(name, dc) + vms = raw_list_all_virtual_machines(dc) + vms.keep_if { |v| v["name"] == name }.first + end end class Mock diff --git a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb index c7377284c..a91ec6098 100644 --- a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb +++ b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb @@ -17,6 +17,7 @@ module Fog end end + private def list_all_virtual_machines_in_folder(path, datacenter_name) @@ -26,23 +27,26 @@ module Fog end def list_all_virtual_machines(options = { }) - datacenters = find_datacenters(options[:datacenter]) - - vms = datacenters.map do |dc| - @connection.serviceContent.viewManager.CreateContainerView({ - :container => dc.vmFolder, - :type => ["VirtualMachine"], - :recursive => true - }).view - end.flatten - # remove all template based virtual machines + vms = raw_list_all_virtual_machines(options[:datacenter]) vms.delete_if { |v| v.config.template } vms.map do |vm_mob| convert_vm_mob_ref_to_attr_hash(vm_mob) end end - + def raw_list_all_virtual_machines(datacenter_name = nil) + ## Moved this to its own function since trying to get a list of all virtual machines + ## to parse for a find function took way too long. The raw list returned will make it + ## much faster to interact for some functions. + datacenters = find_datacenters(datacenter_name) + datacenters.map do |dc| + @connection.serviceContent.viewManager.CreateContainerView({ + :container => dc.vmFolder, + :type => ["VirtualMachine"], + :recursive => true + }).view + end.flatten + end def get_folder_path(folder, root = nil) if (not folder.methods.include?('parent')) or (folder == root) return