Class: XenApi::Client
- Inherits:
-
Object
- Object
- XenApi::Client
- Defined in:
- lib/xenapi/client.rb
Overview
This class permits the invocation of XMLRPC API calls through a ruby-like interface
client = XenApi::Client.new('http://xenapi.test')
client.login_with_password('root', 'password')
client.VM.get_all
Authenticating with the API
Authentication with the API takes place through the API session
class, usually using the login_with_password
method. The Client
handles this method specially to enable it to retain the session identifier to pass to invoked methods and perform reauthentication should the session become stale.
client = XenApi::Client.new('http://xenapi.test')
client.login_with_password('root', 'password')
It is worth noting that only login*
matching methods are specially passed through to the session
class.
Running code after API login
The Client
provides the ability for running code after the client has successfully authenticated with the API. This is useful for either logging authentication or for registering for certain information from the API.
The best example of this is when needing to make use of the Xen API event
class for asynchronous event handling. To use the API event
class you first have to register your interest in a specific set of event types.
client = XenApi::Client.new('http://xenapi.test')
client.after_login do |c|
c.event.register %w(vm) # register for 'vm' events
end
Asynchronous Methods
To call asynchronous methods on the Xen XMLRPC API you first call Async
on the Client
instance followed by the normal method name. For example:
client = XenApi::Client.new('http://xenapi.test')
client.login_with_password('root', 'password')
client.Async.VM.get_all
client.async.VM.get_all
Calling either Async
or async
will work as the capitalised form will always be sent when calling a method asynchronously.
Defined Under Namespace
Classes: LoginRequired, ResponseMissingErrorDescriptionField, ResponseMissingStatusField, ResponseMissingValueField, SessionInvalid
Instance Method Summary collapse
-
#after_login(&block) ⇒ Client
Receiver.
-
#initialize(uri, timeout = 10) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#method_missing(meth, *args) ⇒ true, ...
Handle API method calls.
-
#xenapi_session ⇒ String
Returns the current session identifier.
Constructor Details
#initialize(uri, timeout = 10) ⇒ Client
Returns a new instance of Client.
98 99 100 101 102 |
# File 'lib/xenapi/client.rb', line 98 def initialize(uri, timeout = 10) @timeout = timeout @uri = URI.parse(uri) @uri.path = '/' if @uri.path == '' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ true, ...
meth
names are not validated
Handle API method calls.
If the method called starts with login
then the method is assumed to be part of the session
namespace and will be called directly. For example login_with_password
client = XenApi::Client.new('http://xenapi.test/')
client.login_with_password('root', 'password)
If the method called is async
then an AsyncDispatcher
will be created to handle the asynchronous API method call.
client = XenApi::Client.new('http://xenapi.test/')
client.async.host.get_servertime(ref)
The final case will create a Dispatcher
to handle the subsequent method call such as.
client = XenApi::Client.new('http://xenapi.test/')
client.host.get_servertime(ref)
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/xenapi/client.rb', line 161 def method_missing(meth, *args) case meth.to_s when /^login/ _login(meth, *args) when /^async/i AsyncDispatcher.new(self, :_call) else Dispatcher.new(self, meth, :_call) end end |
Instance Method Details
#after_login {|client| ... } ⇒ Client #after_login ⇒ Client
Returns receiver.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/xenapi/client.rb', line 114 def after_login(&block) if block @after_login = block elsif @after_login case @after_login.arity when 1 @after_login.call(self) else @after_login.call end end self end |
#inspect ⇒ Object
92 93 94 |
# File 'lib/xenapi/client.rb', line 92 def inspect "#<#{self.class} #{@uri}>" end |
#xenapi_session ⇒ String
Returns the current session identifier.
131 132 133 |
# File 'lib/xenapi/client.rb', line 131 def xenapi_session @session end |