Skip to content

Commit

Permalink
feature #3327: search recursively for objects in vcenter
Browse files Browse the repository at this point in the history
It searches for both hosts and vms inside folders.

Based on the patch sent by Jens Grunert:

#39
(cherry picked from commit 4d90449)
  • Loading branch information
jfontan authored and rsmontero committed Nov 17, 2014
1 parent 6c7eef4 commit 56f2bab
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/vmm_mad/remotes/vcenter/vcenter_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ module VCenterDriver
class VIClient
attr_reader :vim, :one, :root, :cluster, :user, :pass, :host

def get_entities(entities=[], folder, type)
return nil if folder == []
folder.childEntity.each do |child|
name, junk = child.to_s.split('(')

case name
when "Folder"
get_entities(entities, child, type)
when type
entities.push(child)
end
end

return entities
end


############################################################################
# Initializr the VIClient, and creates an OpenNebula client. The parameters
# are obtained from the associated OpenNebula host
Expand All @@ -74,9 +91,10 @@ def initialize(hid)

initialize_vim(connection)

@root.childEntity.each {|dc|
ccrs = dc.hostFolder.childEntity.grep(
RbVmomi::VIM::ClusterComputeResource)
datacenters = get_entities(@root, 'Datacenter')

datacenters.each {|dc|
ccrs = get_entities(dc.hostFolder, 'ClusterComputeResource')

next if ccrs.nil?

Expand Down Expand Up @@ -118,7 +136,7 @@ def resource_pool
# @param uuid [String] the UUID of the VM or VM Template
########################################################################
def find_vm_template(uuid)
vms = @dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine)
vms = get_entities(@dc.vmFolder, 'VirtualMachine')

return vms.find do |v|
begin
Expand Down Expand Up @@ -157,9 +175,10 @@ def hierarchy
def vm_templates
vm_templates = {}

@root.childEntity.each { |dc|
datacenters = get_entities(@root, 'Datacenter')

vms = dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine)
datacenters.each { |dc|
vms = get_entities(dc.vmFolder, 'VirtualMachine')

tmp = vms.select { |v| v.config.template == true }

Expand Down

0 comments on commit 56f2bab

Please sign in to comment.