From 769260b60b4e15c99151622236a78c16667d5ff3 Mon Sep 17 00:00:00 2001 From: Christian Dickmann Date: Wed, 9 Nov 2011 18:54:38 -0800 Subject: [PATCH] move PropertyCollector#pathsOfMany to ManagedEntity#paths and reuse that in ManagedEntity#path --- lib/rbvmomi/vim/ManagedEntity.rb | 63 +++++++++++++++++----------- lib/rbvmomi/vim/PropertyCollector.rb | 40 +----------------- 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/lib/rbvmomi/vim/ManagedEntity.rb b/lib/rbvmomi/vim/ManagedEntity.rb index 765d79cd..10a2bd8e 100644 --- a/lib/rbvmomi/vim/ManagedEntity.rb +++ b/lib/rbvmomi/vim/ManagedEntity.rb @@ -2,39 +2,52 @@ class RbVmomi::VIM::ManagedEntity # Retrieve the ancestors of the entity. # @return [Array] Ancestors of this entity, starting with the root. def path + self.class.paths([self])[self] + end + + # Retrieve the ancestors of a list of entries. + # @return [Hash] Object-indexed hash of ancestors of entities, starting with the root. + def self.paths objs + i = 0 filterSpec = RbVmomi::VIM.PropertyFilterSpec( - :objectSet => [{ - :obj => self, - :selectSet => [ - RbVmomi::VIM.TraversalSpec( - :name => 'tsME', - :type => 'ManagedEntity', - :path => 'parent', - :skip => false, - :selectSet => [ - RbVmomi::VIM.SelectionSpec(:name => 'tsME') - ] - ) - ] - }], + :objectSet => objs.map do |obj| + i += 1 + RbVmomi::VIM.ObjectSpec( + :obj => obj, + :selectSet => [ + RbVmomi::VIM.TraversalSpec( + :name => "tsME-#{i}", + :type => 'ManagedEntity', + :path => 'parent', + :skip => false, + :selectSet => [ + RbVmomi::VIM.SelectionSpec(:name => "tsME-#{i}") + ] + ) + ] + ) + end, :propSet => [{ :pathSet => %w(name parent), :type => 'ManagedEntity' }] ) - result = @soap.propertyCollector.RetrieveProperties(:specSet => [filterSpec]) + propCollector = objs.first.propertyCollector + result = propCollector.RetrieveProperties(:specSet => [filterSpec]) - tree = {} - result.each { |x| tree[x.obj] = [x['parent'], x['name']] } - a = [] - cur = self - while cur - parent, name = *tree[cur] - a << [cur, name] - cur = parent - end - a.reverse + Hash[objs.map do |obj| + tree = {} + result.each { |x| tree[x.obj] = [x['parent'], x['name']] } + a = [] + cur = obj + while cur + parent, name = *tree[cur] + a << [cur, name] + cur = parent + end + [obj, a.reverse] + end] end # Return a string representation of +path+ suitable for display. diff --git a/lib/rbvmomi/vim/PropertyCollector.rb b/lib/rbvmomi/vim/PropertyCollector.rb index 01c13b2f..31b5b395 100644 --- a/lib/rbvmomi/vim/PropertyCollector.rb +++ b/lib/rbvmomi/vim/PropertyCollector.rb @@ -24,44 +24,6 @@ def collectMultiple objs, *pathSet end def pathsOfMany objs - i = 0 - filterSpec = RbVmomi::VIM.PropertyFilterSpec( - :objectSet => objs.map do |obj| - i += 1 - RbVmomi::VIM.ObjectSpec( - :obj => obj, - :selectSet => [ - RbVmomi::VIM.TraversalSpec( - :name => "tsME-#{i}", - :type => 'ManagedEntity', - :path => 'parent', - :skip => false, - :selectSet => [ - RbVmomi::VIM.SelectionSpec(:name => "tsME-#{i}") - ] - ) - ] - ) - end, - :propSet => [{ - :pathSet => %w(name parent), - :type => 'ManagedEntity' - }] - ) - - result = self.RetrieveProperties(:specSet => [filterSpec]) - - Hash[objs.map do |obj| - tree = {} - result.each { |x| tree[x.obj] = [x['parent'], x['name']] } - a = [] - cur = obj - while cur - parent, name = *tree[cur] - a << [cur, name] - cur = parent - end - [obj, a.reverse] - end] + RbVmomi::VIM::ManagedEntity.paths objs end end