Class: OvirtSDK4::Connection
- Inherits:
-
Object
- Object
- OvirtSDK4::Connection
- Defined in:
- lib/ovirtsdk4/connection.rb
Overview
This class is responsible for managing an HTTP connection to the engine server. It is intended as the entry
point for the SDK, and it provides access to the system
service and, from there, to the rest of the services
provided by the API.
Instance Method Summary collapse
-
#authenticate ⇒ String
Performs the authentication process and returns the authentication token.
-
#close ⇒ Object
Releases the resources used by this connection, making sure that multiple threads are coordinated correctly.
-
#follow_link(object) ⇒ Object
Follows the
href
attribute of the given object, retrieves the target object and returns it. -
#initialize(opts = {}) ⇒ Connection
constructor
Creates a new connection to the API server.
-
#inspect ⇒ String
Returns a string representation of the connection.
-
#link?(object) ⇒ Boolean
(also: #is_link?)
Indicates if the given object is a link.
-
#service(path) ⇒ Service
Returns a reference to the service corresponding to the given path.
-
#system_service ⇒ SystemService
Returns a reference to the root of the services tree.
-
#test(raise_exception = false, timeout = nil) ⇒ Boolean
Tests the connectivity with the server.
-
#to_s ⇒ String
Returns a string representation of the connection.
-
#wait(request) ⇒ HttpResponse
Waits for the response to the given request, making sure that multiple threads are coordinated correctly.
Constructor Details
#initialize(opts = {}) ⇒ Connection
Creates a new connection to the API server.
connection = OvirtSDK4::Connection.new(
url: 'https://engine.example.com/ovirt-engine/api',
username: 'admin@internal',
password: '...',
ca_file:'/etc/pki/ovirt-engine/ca.pem'
)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ovirtsdk4/connection.rb', line 110 def initialize(opts = {}) # Get the values of the parameters and assign default values: @url = opts[:url] @username = opts[:username] @password = opts[:password] @token = opts[:token] @insecure = opts[:insecure] || false @ca_file = opts[:ca_file] @ca_certs = opts[:ca_certs] @debug = opts[:debug] || false @log = opts[:log] @kerberos = opts[:kerberos] || false @timeout = opts[:timeout] || 0 @connect_timeout = opts[:connect_timeout] || 0 @compress = opts[:compress] || true @proxy_url = opts[:proxy_url] @proxy_username = opts[:proxy_username] @proxy_password = opts[:proxy_password] @headers = opts[:headers] @connections = opts[:connections] || 0 @pipeline = opts[:pipeline] || 0 # Check that the URL has been provided: raise ArgumentError, "The 'url' option is mandatory" unless @url # Automatically disable compression when debug is enabled, as otherwise the debug output generated by # libcurl is also compressed, and that isn't useful for debugging: @compress = false if @debug # Create a temporary file to store the CA certificates, and populate it with the contents of the 'ca_file' and # 'ca_certs' options. The file will be removed when the connection is closed. @ca_store = nil if @ca_file || @ca_certs @ca_store = Tempfile.new('ca_store') @ca_store.write(::File.read(@ca_file)) if @ca_file if @ca_certs @ca_certs.each do |ca_cert| @ca_store.write(ca_cert) end end @ca_store.close end # Create the mutex that will be used to prevents simultaneous access to the same HTTP client by multiple threads: @mutex = Mutex.new # Create the HTTP client: @client = HttpClient.new( insecure: @insecure, ca_file: @ca_store ? @ca_store.path : nil, debug: @debug, log: @log, timeout: @timeout, connect_timeout: @connect_timeout, compress: @compress, proxy_url: @proxy_url, proxy_username: @proxy_username, proxy_password: @proxy_password, connections: @connections, pipeline: @pipeline ) end |
Instance Method Details
#authenticate ⇒ String
Performs the authentication process and returns the authentication token. Usually there is no need to
call this method, as authentication is performed automatically when needed. But in some situations it
may be useful to perform authentication explicitly, and then use the obtained token to create other
connections, using the token
parameter of the constructor instead of the user name and password.
382 383 384 |
# File 'lib/ovirtsdk4/connection.rb', line 382 def authenticate @token ||= create_access_token end |
#close ⇒ Object
Releases the resources used by this connection, making sure that multiple threads are coordinated correctly.
439 440 441 |
# File 'lib/ovirtsdk4/connection.rb', line 439 def close @mutex.synchronize { internal_close } end |
#follow_link(object) ⇒ Object
Follows the href
attribute of the given object, retrieves the target object and returns it.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/ovirtsdk4/connection.rb', line 411 def follow_link(object) # Check that the "href" has a value, as it is needed in order to retrieve the representation of the object: href = object.href if href.nil? raise Error, "Can't follow link because the 'href' attribute doesn't have a value" end # Check that the value of the "href" attribute is compatible with the base URL of the connection: prefix = URI(@url).path prefix += '/' unless prefix.end_with?('/') unless href.start_with?(prefix) raise Error, "The URL '#{href}' isn't compatible with the base URL of the connection" end # Remove the prefix from the URL, follow the path to the relevant service and invoke the "get" or "list" method # to retrieve its representation: path = href[prefix.length..-1] service = service(path) if object.is_a?(Array) service.list else service.get end end |
#inspect ⇒ String
Returns a string representation of the connection.
522 523 524 |
# File 'lib/ovirtsdk4/connection.rb', line 522 def inspect "#<#{self.class.name}:#{@url}>" end |
#link?(object) ⇒ Boolean Also known as: is_link?
Indicates if the given object is a link. An object is a link if it has an href
attribute.
391 392 393 |
# File 'lib/ovirtsdk4/connection.rb', line 391 def link?(object) !object.href.nil? end |
#service(path) ⇒ Service
Returns a reference to the service corresponding to the given path. For example, if the path
parameter
is vms/123/diskattachments
then it will return a reference to the service that manages the disk
attachments for the virtual machine with identifier 123
.
191 192 193 |
# File 'lib/ovirtsdk4/connection.rb', line 191 def service(path) system_service.service(path) end |
#system_service ⇒ SystemService
Returns a reference to the root of the services tree.
178 179 180 |
# File 'lib/ovirtsdk4/connection.rb', line 178 def system_service @system_service ||= SystemService.new(self, '') end |
#test(raise_exception = false, timeout = nil) ⇒ Boolean
Tests the connectivity with the server. If connectivity works correctly it returns true
. If there is any
connectivity problem it will either return false
or raise an exception if the raise_exception
parameter is
true
.
366 367 368 369 370 371 372 |
# File 'lib/ovirtsdk4/connection.rb', line 366 def test(raise_exception = false, timeout = nil) system_service.get(timeout: timeout) true rescue StandardError raise if raise_exception false end |
#to_s ⇒ String
Returns a string representation of the connection.
531 532 533 |
# File 'lib/ovirtsdk4/connection.rb', line 531 def to_s inspect end |
#wait(request) ⇒ HttpResponse
Waits for the response to the given request, making sure that multiple threads are coordinated correctly.
212 213 214 |
# File 'lib/ovirtsdk4/connection.rb', line 212 def wait(request) @mutex.synchronize { internal_wait(request) } end |