Class: VMware::Session
- Inherits:
-
Object
- Object
- VMware::Session
- Defined in:
- lib/vmware/session.rb
Overview
A session incorporates the API specific state of the server connection.
Instance Attribute Summary collapse
-
#sic ⇒ Object
readonly
Service content object provides our root to the object hierarchy.
Instance Method Summary collapse
- #create_prop_request(type, path_set, begin_entity, select_set = nil) ⇒ Object
- #find_entities(type, begin_entity = nil) ⇒ Object
-
#initialize(connection) ⇒ Session
constructor
Create a new session by connecting to the server, getting the service instance and logging in.
-
#login ⇒ Object
Login to the server.
-
#managed_object_wrapper_factory(type, reference) ⇒ Object
Based on the type of a ManagedObjectReference, generate the appropriate wrapper instance.
-
#method_missing(methodSymbol, *params) ⇒ Object
Catch any function call and send it over the soap connection.
-
#root_folder ⇒ Object
Return the root folder of the inventory hierarchy.
- #selection_spec(names) ⇒ Object
- #task_manager ⇒ Object
- #traversal_spec(name, type, path, select_set) ⇒ Object
- #traversal_specs ⇒ Object
- #wrap(ref) ⇒ Object
Constructor Details
#initialize(connection) ⇒ Session
Create a new session by connecting to the server, getting the service instance and logging in.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/vmware/session.rb', line 17 def initialize(connection) @connection = connection svcref = ManagedObjectReference.new("ServiceInstance") svcref.xmlattr_type = "ServiceInstance" retrieveServiceContent = RetrieveServiceContentRequestType.new(svcref) @sic = connection.retrieveServiceContent(retrieveServiceContent).returnval login end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methodSymbol, *params) ⇒ Object
Catch any function call and send it over the soap connection.
127 128 129 |
# File 'lib/vmware/session.rb', line 127 def method_missing(methodSymbol, *params) @connection.send(methodSymbol, *params) end |
Instance Attribute Details
#sic ⇒ Object (readonly)
Service content object provides our root to the object hierarchy.
11 12 13 |
# File 'lib/vmware/session.rb', line 11 def sic @sic end |
Instance Method Details
#create_prop_request(type, path_set, begin_entity, select_set = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vmware/session.rb', line 73 def create_prop_request type, path_set, begin_entity, select_set=nil # Which properties to return. prop_spec = PropertySpec.new prop_spec.type = type prop_spec.pathSet = path_set prop_spec.all = path_set.empty? # Object at which filter starts. object_spec = ObjectSpec.new object_spec.obj = begin_entity object_spec.skip = false object_spec.selectSet = select_set unless select_set.nil? # Create the filter. prop_filter_spec = PropertyFilterSpec.new prop_filter_spec.propSet = prop_spec prop_filter_spec.objectSet = [object_spec] # Create the request. RetrievePropertiesRequestType.new @sic.propertyCollector, prop_filter_spec end |
#find_entities(type, begin_entity = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vmware/session.rb', line 37 def find_entities type, begin_entity=nil begin_entity ||= @sic.rootFolder request = create_prop_request type, [], begin_entity, traversal_specs begin retrieveProperties(request).collect { |obj| obj.obj } rescue SOAP::FaultError => e puts e.inspect [] end end |
#login ⇒ Object
Login to the server.
98 99 100 101 102 103 |
# File 'lib/vmware/session.rb', line 98 def login loginRequest = LoginRequestType.new(@sic.sessionManager) loginRequest.userName = @connection.username loginRequest.password = @connection.password @connection.login(loginRequest); end |
#managed_object_wrapper_factory(type, reference) ⇒ Object
Based on the type of a ManagedObjectReference, generate the appropriate wrapper instance.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/vmware/session.rb', line 109 def managed_object_wrapper_factory(type, reference) # # If we know about the type try to instantiate it. # begin eval("VMware::#{type}.new(self, reference)") rescue STDERR.puts "WARNING: Unknown managed object type: #{type}" VMware::ManagedEntity.new(self, reference, type) end end |
#root_folder ⇒ Object
Return the root folder of the inventory hierarchy.
31 32 33 |
# File 'lib/vmware/session.rb', line 31 def root_folder managed_object_wrapper_factory("Folder", @sic.rootFolder) end |
#selection_spec(names) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/vmware/session.rb', line 66 def selection_spec names names.collect do |name| spec = SelectionSpec.new spec.name = name spec end end |
#task_manager ⇒ Object
34 35 36 |
# File 'lib/vmware/session.rb', line 34 def task_manager managed_object_wrapper_factory("TaskManager", @sic.taskManager) end |
#traversal_spec(name, type, path, select_set) ⇒ Object
63 64 65 |
# File 'lib/vmware/session.rb', line 63 def traversal_spec name, type, path, select_set TraversalSpec.new nil, [], name, type, path, false, select_set end |
#traversal_specs ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vmware/session.rb', line 48 def traversal_specs specs = [] spec = selection_spec ['folder_spec','host_folder_spec','vm_folder_spec','host_spec','resource_spec','cr_spec','hosts_spec'] specs.push(traversal_spec 'folder_spec', 'Folder', 'childEntity', spec) spec = selection_spec ['folder_spec'] specs.push(traversal_spec 'host_folder_spec', 'Datacenter', 'hostFolder', spec) specs.push(traversal_spec 'vm_folder_spec', 'Datacenter', 'vmFolder', spec) specs.push(traversal_spec 'host_spec', 'HostSystem', 'vm', spec) spec = selection_spec ['resource_spec'] specs.push(traversal_spec 'resource_spec', 'ResourcePool', 'resourcePool', spec) specs.push(traversal_spec 'cr_spec', 'ComputeResource', 'resourcePool', spec) spec = selection_spec ['host_spec'] specs.push(traversal_spec 'hosts_spec', 'ComputeResource', 'host', spec) specs end |
#wrap(ref) ⇒ Object
120 121 122 |
# File 'lib/vmware/session.rb', line 120 def wrap ref managed_object_wrapper_factory ref.xmlattr_type, ref end |