Class: RbVmomi::VIM::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/rbvmomi/vim/Folder.rb

Instance Method Summary collapse

Instance Method Details

#childrenObject

Alias to childEntity.


42
43
44
# File 'lib/rbvmomi/vim/Folder.rb', line 42

def children
  childEntity
end

#find(name, type = Object) ⇒ VIM::ManagedEntity

Retrieve a child entity

Parameters:

  • name (String)

    Name of the child.

  • type (Class) (defaults to: Object)

    Return nil unless the found entity is_a? type.

Returns:


6
7
8
9
# File 'lib/rbvmomi/vim/Folder.rb', line 6

def find name, type=Object
  x = @soap.searchIndex.FindChild(:entity => self, :name => name)
  x if x.is_a? type
end

#inventory(propSpecs = {}) ⇒ Hash

TODO:

Return ObjectContent instead of the leaf hash.

Efficiently retrieve properties from descendants of this folder.

Parameters:

  • propSpecs (Hash) (defaults to: {})

    Specification of which properties to retrieve from which entities. Keys may be symbols, strings, or classes identifying ManagedEntity subtypes to be included in the results. Values are an array of property paths (strings) or the symbol :all.

Returns:

  • (Hash)

    Tree of inventory items. Folders are hashes from child name to child result. Objects are hashes from property path to value.


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbvmomi/vim/Folder.rb', line 59

def inventory propSpecs={}
  propSet = [{ :type => 'Folder', :pathSet => ['name', 'parent'] }]
  propSpecs.each do |k,v|
    case k
    when RbVmomi::VIM::ManagedEntity
      k = k.wsdl_name
    when Symbol, String
      k = k.to_s
    else
      fail "key must be a ManagedEntity"
    end

    h = { :type => k }
    if v == :all
      h[:all] = true
    elsif v.is_a? Array
      h[:pathSet] = v + %w(parent)
    else
      fail "value must be an array of property paths or :all"
    end
    propSet << h
  end

  filterSpec = RbVmomi::VIM.PropertyFilterSpec(
    :objectSet => [
      :obj => self,
      :selectSet => [
        RbVmomi::VIM.TraversalSpec(
          :name => 'tsFolder',
          :type => 'Folder',
          :path => 'childEntity',
          :skip => false,
          :selectSet => [
            RbVmomi::VIM.SelectionSpec(:name => 'tsFolder')
          ]
        )
      ]
    ],
    :propSet => propSet
  )

  result = @soap.propertyCollector.RetrieveProperties(:specSet => [filterSpec])

  tree = { self => {} }
  result.each do |x|
    obj = x.obj
    next if obj == self
    h = Hash[x.propSet.map { |y| [y.name, y.val] }]
    tree[h['parent']][h['name']] = [obj, h]
    tree[obj] = {} if obj.is_a? RbVmomi::VIM::Folder
  end
  tree
end

#traverse(path, type = Object, create = false) ⇒ VIM::ManagedEntity

TODO:

Move create functionality into another method.

Retrieve a descendant of this Folder.

Parameters:

  • path (String)

    Path delimited by '/'.

  • create (Boolean) (defaults to: false)

    If set, create folders that don't exist.

  • type (Class) (defaults to: Object)

    Return nil unless the found entity is_a? type.

Returns:


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbvmomi/vim/Folder.rb', line 23

def traverse path, type=Object, create=false
  es = path.split('/').reject(&:empty?)
  return self if es.empty?
  final = es.pop

  p = es.inject(self) do |f,e|
    f.find(e, Folder) || (create && f.CreateFolder(:name => e)) || return
  end

  if x = p.find(final, type)
    x
  elsif create and type == Folder
    p.CreateFolder(:name => final)
  else
    nil
  end
end

#traverse!(path, type = Object) ⇒ Object

Alias to traverse path, type, true

See Also:


13
14
15
# File 'lib/rbvmomi/vim/Folder.rb', line 13

def traverse! path, type=Object
  traverse path, type, true
end