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:
commit
1f34b4452f
2 changed files with 35 additions and 24 deletions
|
@ -3,6 +3,7 @@ module Fog
|
||||||
class Vsphere
|
class Vsphere
|
||||||
class Real
|
class Real
|
||||||
def get_virtual_machine(id, datacenter_name = nil)
|
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))
|
convert_vm_mob_ref_to_attr_hash(get_vm_ref(id, datacenter_name))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,13 +20,18 @@ module Fog
|
||||||
else
|
else
|
||||||
# try to find based on VM name
|
# try to find based on VM name
|
||||||
if dc
|
if dc
|
||||||
raw_datacenter.find_vm(id)
|
get_vm_by_name(id, dc)
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found")
|
vm ? vm : raise(Fog::Compute::Vsphere::NotFound, "#{id} was not found")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class Mock
|
class Mock
|
||||||
|
|
|
@ -17,6 +17,7 @@ module Fog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def list_all_virtual_machines_in_folder(path, datacenter_name)
|
def list_all_virtual_machines_in_folder(path, datacenter_name)
|
||||||
|
@ -29,23 +30,27 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_all_virtual_machines(options = { })
|
def list_all_virtual_machines(options = { })
|
||||||
datacenters = find_datacenters(options[:datacenter])
|
raw_vms = raw_list_all_virtual_machines(options[:datacenter])
|
||||||
|
vms = convert_vm_view_to_attr_hash(raw_vms)
|
||||||
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)
|
|
||||||
|
|
||||||
# remove all template based virtual machines
|
# remove all template based virtual machines
|
||||||
vms.delete_if { |v| v['template'] }
|
vms.delete_if { |v| v['template'] }
|
||||||
vms
|
vms
|
||||||
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)
|
def get_folder_path(folder, root = nil)
|
||||||
if (not folder.methods.include?('parent')) or (folder == root)
|
if (not folder.methods.include?('parent')) or (folder == root)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue