Class: VMware::ManagedEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/vmware/objects/managed_entity.rb

Overview

Wrapper for managed object references.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, object_reference) ⇒ ManagedEntity

Create a wrapper for managed object based on a ManagedObjectReference



11
12
13
14
15
16
# File 'lib/vmware/objects/managed_entity.rb', line 11

def initialize(session, object_reference)
  @session, @object_reference = session, object_reference
  
  # We cache properties for more efficient access.
  @cached_properties = nil
end

Instance Attribute Details

#object_referenceObject (readonly)

Returns the value of attribute object_reference.



6
7
8
# File 'lib/vmware/objects/managed_entity.rb', line 6

def object_reference
  @object_reference
end

Instance Method Details

#[](key) ⇒ Object

Return a specific property.



78
79
80
81
82
83
84
85
# File 'lib/vmware/objects/managed_entity.rb', line 78

def [](key)
  if @cached_properties
    p = properties
  else
    p = properties(key)
  end
  return p[key.to_s]
end

#properties(prop_list = []) ⇒ Object

Retrieve a set of properties from the object as a hash. Specify individual property names as a list, or leave empty to get all properties.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vmware/objects/managed_entity.rb', line 22

def properties(prop_list = [])
  if prop_list.empty? and @cached_properties
    return @cached_properties
  end
  
  #  Object at which filter starts.
  object_spec = ObjectSpec.new
  object_spec.obj = @object_reference
  object_spec.skip = false

  # Which properties to return.
  prop_spec = PropertySpec.new
  prop_spec.type = self.class.to_s.gsub(/^.*::/, "")
  prop_spec.pathSet = prop_list
  prop_spec.all = prop_list.empty?

  # Create the filter.
  prop_filter_spec = PropertyFilterSpec.new
  prop_filter_spec.objectSet = [object_spec]
  prop_filter_spec.propSet = prop_spec

  # Create the request.
  request = RetrievePropertiesRequestType.new(@session.sic.propertyCollector)
  request.specSet = prop_filter_spec

  prop_hash = {}
  begin
    object_list = @session.retrieveProperties(request)
    if object_list.length > 1
      raise "Unexpected number of objects returned when fetching properties for #{self.class}:\n#{object_list.inspect}\n"
    end
    if object_list.empty?
      puts "WARNING: #{self.class} has no properties"
      return {}
    end
    
    # Convert the properties into a hash and wrap data objects.
    prop_set = object_list[0].propSet
    prop_set.each do |prop|
      prop_hash[prop.name] = VMware::DataObject::soap_to_data_object(prop.val)
    end
  rescue SOAP::FaultError => e
    puts e.inspect
  end
  
  # Only cache the properties if we retrieved all of them.
  if prop_list.empty?
    @cached_properties = prop_hash
  end
  
  return prop_hash
end