1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge branch 'mjblack-vsphere_improved_vm_searching'

This commit is contained in:
Kevin Menard 2015-02-02 18:22:39 -05:00
commit 1f34b4452f
2 changed files with 35 additions and 24 deletions

View file

@ -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

View file

@ -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