diff --git a/lib/fog/vsphere/requests/compute/get_virtual_machine.rb b/lib/fog/vsphere/requests/compute/get_virtual_machine.rb index 900901b73..85e781dea 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 @@ -11,21 +12,26 @@ module Fog def get_vm_ref(id, dc = nil) raw_datacenter = find_raw_datacenter(dc) if dc vm = case is_uuid?(id) - # UUID based - when true - params = {:uuid => id, :vmSearch => true, :instanceUuid => true} - params[:datacenter] = raw_datacenter if dc - @connection.searchIndex.FindByUuid(params) - else - # try to find based on VM name - if dc - raw_datacenter.find_vm(id) - else - raw_datacenters.map { |d| d.find_vm(id) }.compact.first - end - end + # UUID based + when true + params = {:uuid => id, :vmSearch => true, :instanceUuid => true} + params[:datacenter] = raw_datacenter if dc + @connection.searchIndex.FindByUuid(params) + else + # try to find based on VM name + if dc + get_vm_by_name(id, dc) + else + 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 0f7623e80..3510f0954 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) @@ -29,23 +30,27 @@ 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 - - vms = convert_vm_view_to_attr_hash(vms) + raw_vms = raw_list_all_virtual_machines(options[:datacenter]) + vms = convert_vm_view_to_attr_hash(raw_vms) # remove all template based virtual machines vms.delete_if { |v| v['template'] } vms 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