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, managed_type = nil) ⇒ ManagedEntity

Create a wrapper for managed object based on a ManagedObjectReference



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

def initialize(session, object_reference, managed_type=nil)
  @session, @object_reference = session, object_reference
  @managed_type = managed_type
  
  # 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.



63
64
65
66
67
68
69
70
# File 'lib/vmware/objects/managed_entity.rb', line 63

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.



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
# File 'lib/vmware/objects/managed_entity.rb', line 23

def properties(prop_list = [])
  if prop_list.empty? and @cached_properties
    return @cached_properties
  end
  
  # Create the request.
  type = @managed_type || self.class.to_s.gsub(/^.*::/, "")
  request = @session.create_prop_request type, prop_list, @object_reference

  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 unless prop_set.nil?
  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