class RbVmomi::VIM::Folder

Public Instance Methods

children() click to toggle source

Alias to childEntity.

# File lib/rbvmomi/vim/Folder.rb, line 105
def children
  childEntity
end
find(name, type=Object) click to toggle source

Retrieve a child entity @param name [String] Name of the child. @param type [Class] Return nil unless the found entity is_a? type. @return [VIM::ManagedEntity]

# File lib/rbvmomi/vim/Folder.rb, line 6
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) click to toggle source

Retrieve a virtual machine or host by DNS name @param name [String] The fully qualified domain name to find. @param type [Class] Return nil unless the found entity is_a? type. @param dc [RbVmomi::VIM::Datacenter] Restricts the query to entities in the given Datacenter. @return [VIM::ManagedEntity]

# File lib/rbvmomi/vim/Folder.rb, line 16
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) click to toggle source

Retrieve a managed entity by inventory path. @param path [String] A path of the form “My Folder/My Datacenter/vm/Discovered VM/VM1” @return [VIM::ManagedEntity]

# File lib/rbvmomi/vim/Folder.rb, line 59
def findByInventoryPath path
  propSpecs = {
    :entity => self, :inventoryPath => path
  }
  x = _connection.searchIndex.FindByInventoryPath(propSpecs)
end
findByIp(ip, type=RbVmomi::VIM::VirtualMachine, dc=nil) click to toggle source

Retrieve a virtual machine or host by IP address @param ip [String] The IP address is in dot-decimal notation. @param type [Class] Return nil unless the found entity is_a? type. @param dc [RbVmomi::VIM::Datacenter] Restricts the query to entities in the given Datacenter. @return [VIM::ManagedEntity]

# File lib/rbvmomi/vim/Folder.rb, line 31
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) click to toggle source

Retrieve a virtual machine or host by BIOS UUID. @param uuid [String] The UUID to find. @param type [Class] Return nil unless the found entity is_a? type. @param dc [RbVmomi::VIM::Datacenter] Restricts the query to entities in the given Datacenter. @return [VIM::ManagedEntity]

# File lib/rbvmomi/vim/Folder.rb, line 46
def findByUuid uuid, type=RbVmomi::VIM::VirtualMachine, dc=nil
  propSpecs = {
    :entity => self, :uuid => uuid, :instanceUuid => false,
    :vmSearch => type == RbVmomi::VIM::VirtualMachine
  }
  propSpecs[:datacenter] = dc if dc
  x = _connection.searchIndex.FindByUuid(propSpecs)
  x if x.is_a? type
end
inventory(propSpecs={}) click to toggle source

Efficiently retrieve properties from descendants of this folder.

@param propSpecs [Hash] 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.

@return [Hash] Tree of inventory items. Folders are hashes from child name

to child result. Objects are hashes from property path to
value.

@deprecated

# File lib/rbvmomi/vim/Folder.rb, line 196
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={}) click to toggle source

Efficiently retrieve properties from descendants of this folder.

@param propSpecs [Hash] 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.

@return [Hash] Hash of ManagedObjects to properties.

# File lib/rbvmomi/vim/Folder.rb, line 118
def inventory_flat propSpecs={}
  propSet = [{ :type => 'Folder', :pathSet => ['name', 'parent', 'childEntity'] }]
  propSpecs.each do |k,v|
    case k
    when Class
      fail "key must be a subclass of ManagedEntity" unless k < RbVmomi::VIM::ManagedEntity
      k = k.wsdl_name
    when Symbol, String
      k = k.to_s
    else
      fail "invalid key"
    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 = _connection.propertyCollector.RetrieveProperties(:specSet => [filterSpec])
  {}.tap do |h|
    result.each { |r| h[r.obj] = r }
  end
end
inventory_tree(propSpecs={}) click to toggle source

Efficiently retrieve properties from descendants of this folder.

@param propSpecs [Hash] 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.

@return [Hash] Tree of inventory items. Each node is a hash from

VIM::ObjectContent to children.
# File lib/rbvmomi/vim/Folder.rb, line 176
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) click to toggle source

Retrieve a descendant of this Folder. @param path [String] Path delimited by '/', or an array of path elements. @param type (see #find) @param create [Boolean] If set, create folders that don't exist. @return (see #find) @todo Move create functionality into another method.

# File lib/rbvmomi/vim/Folder.rb, line 78
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
    fail "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) click to toggle source

Alias to traverse path, type, true @see traverse

# File lib/rbvmomi/vim/Folder.rb, line 68
def traverse! path, type=Object
  traverse path, type, true
end