Class: RbVmomi::VIM::Folder

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

Overview

Copyright © 2011-2017 VMware, Inc. All Rights Reserved. SPDX-License-Identifier: MIT

Instance Method Summary collapse

Instance Method Details

#childrenObject

Alias to childEntity.



114
115
116
# File 'lib/rbvmomi/vim/Folder.rb', line 114

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:



10
11
12
13
# File 'lib/rbvmomi/vim/Folder.rb', line 10

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

#findByDnsName(name, type = RbVmomi::VIM::VirtualMachine, dc = nil) ⇒ VIM::ManagedEntity

Retrieve a virtual machine or host by DNS name

Parameters:

  • name (String)

    The fully qualified domain name to find.

  • type (Class) (defaults to: RbVmomi::VIM::VirtualMachine)

    Return nil unless the found entity is_a? type.

  • dc (RbVmomi::VIM::Datacenter) (defaults to: nil)

    Restricts the query to entities in the given Datacenter.

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/rbvmomi/vim/Folder.rb', line 20

def findByDnsName name, type=RbVmomi::VIM::VirtualMachine, dc=nil
  propSpecs = {
    entity: self, dnsName: name,
    vmSearch: type == RbVmomi::VIM::VirtualMachine
  }
  propSpecs[:datacenter] = dc if dc
  x = _connection.searchIndex.FindByDnsName(propSpecs)
  x if x.is_a? type
end

#findByInventoryPath(path) ⇒ VIM::ManagedEntity

Retrieve a managed entity by inventory path.

Parameters:

  • path (String)

    A path of the form “My Folder/My Datacenter/vm/Discovered VM/VM1”

Returns:



67
68
69
70
71
72
# File 'lib/rbvmomi/vim/Folder.rb', line 67

def findByInventoryPath path
  propSpecs = {
    entity: self, inventoryPath: path
  }
  _connection.searchIndex.FindByInventoryPath(propSpecs)
end

#findByIp(ip, type = RbVmomi::VIM::VirtualMachine, dc = nil) ⇒ VIM::ManagedEntity

Retrieve a virtual machine or host by IP address

Parameters:

  • ip (String)

    The IP address is in dot-decimal notation.

  • type (Class) (defaults to: RbVmomi::VIM::VirtualMachine)

    Return nil unless the found entity is_a? type.

  • dc (RbVmomi::VIM::Datacenter) (defaults to: nil)

    Restricts the query to entities in the given Datacenter.

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/rbvmomi/vim/Folder.rb', line 35

def findByIp ip, type=RbVmomi::VIM::VirtualMachine, dc=nil
  propSpecs = {
    entity: self, ip: ip,
    vmSearch: type == RbVmomi::VIM::VirtualMachine
  }
  propSpecs[:datacenter] = dc if dc
  x = _connection.searchIndex.FindByIp(propSpecs)
  x if x.is_a? type
end

#findByUuid(uuid, type = RbVmomi::VIM::VirtualMachine, dc = nil, instance_uuid = false) ⇒ VIM::ManagedEntity

Finds a virtual machine or host by BIOS or instance UUID

Parameters:

  • uuid (String)

    UUID to find

  • type (Class) (defaults to: RbVmomi::VIM::VirtualMachine)

    return nil unless found entity is_a?(type)

  • dc (RbVmomi::VIM::Datacenter) (defaults to: nil)

    restricts query to specified datacenter

Returns:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rbvmomi/vim/Folder.rb', line 52

def findByUuid(uuid, type = RbVmomi::VIM::VirtualMachine, dc = nil, instance_uuid = false)
  prop_specs = {
    entity: self,
    instanceUuid: instance_uuid,
    uuid: uuid,
    vmSearch: type == RbVmomi::VIM::VirtualMachine
  }
  prop_specs[:datacenter] = dc if dc
  x = _connection.searchIndex.FindByUuid(prop_specs)
  x if x.is_a?(type)
end

#inventory(propSpecs = {}) ⇒ Hash

Deprecated.

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.



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rbvmomi/vim/Folder.rb', line 206

def inventory propSpecs={}
  inv = inventory_flat propSpecs
  tree = { self => {} }
  inv.each do |obj, x|
    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

#inventory_flat(propSpecs = {}) ⇒ 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)

    Hash of ManagedObjects to properties.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rbvmomi/vim/Folder.rb', line 127

def inventory_flat propSpecs={}
  propSet = [{ type: 'Folder', pathSet: ['name', 'parent', 'childEntity'] }]
  propSpecs.each do |k, v|
    case k
    when Class
      raise 'key must be a subclass of ManagedEntity' unless k < RbVmomi::VIM::ManagedEntity

      k = k.wsdl_name
    when Symbol, String
      k = k.to_s
    else
      raise 'invalid key'
    end

    h = { type: k }
    if v == :all
      h[:all] = true
    elsif v.is_a? Array
      h[:pathSet] = v + %w(parent)
    else
      raise '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 = _connection.propertyCollector.RetrieveProperties(specSet: [filterSpec])
  {}.tap do |h|
    result.each { |r| h[r.obj] = r }
  end
end

#inventory_tree(propSpecs = {}) ⇒ 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. Each node is a hash from VIM::ObjectContent to children.



186
187
188
189
190
191
# File 'lib/rbvmomi/vim/Folder.rb', line 186

def inventory_tree propSpecs={}
  inv = inventory_flat propSpecs
  children = inv.values.group_by { |v| v['parent'] }
  rec = lambda { |parent| Hash[(children[parent]||[]).map { |x| [x, rec[x.obj]] }] }
  rec[self]
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 ‘/’, or an array of path elements.

  • 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:



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 86

def traverse path, type=Object, create=false
  if path.is_a? String
    es = path.split('/').reject(&:empty?)
  elsif path.is_a? Enumerable
    es = path
  else
    raise "unexpected path class #{path.class}"
  end
  return self if es.empty?

  final = es.pop

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

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

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

Alias to traverse path, type, true

See Also:



76
77
78
# File 'lib/rbvmomi/vim/Folder.rb', line 76

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