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.
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'
)
109 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 |
# File 'lib/ovirtsdk4/connection.rb', line 109 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] || 1 @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.
246 247 248 249 250 |
# File 'lib/ovirtsdk4/connection.rb', line 246 def authenticate # rubocop:disable Naming/MemoizedInstanceVariableName @token ||= create_access_token # rubocop:enable Naming/MemoizedInstanceVariableName end |
#close ⇒ Object
Releases the resources used by this connection, making sure that multiple threads are coordinated correctly.
303 304 305 |
# File 'lib/ovirtsdk4/connection.rb', line 303 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.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/ovirtsdk4/connection.rb', line 277 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 raise Error, "Can't follow link because the 'href' attribute doesn't have a value" if href.nil? # 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.
395 396 397 |
# File 'lib/ovirtsdk4/connection.rb', line 395 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.
257 258 259 |
# File 'lib/ovirtsdk4/connection.rb', line 257 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
.
190 191 192 |
# File 'lib/ovirtsdk4/connection.rb', line 190 def service(path) system_service.service(path) end |
#system_service ⇒ SystemService
Returns a reference to the root of the services tree.
177 178 179 |
# File 'lib/ovirtsdk4/connection.rb', line 177 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
.
229 230 231 232 233 234 235 236 |
# File 'lib/ovirtsdk4/connection.rb', line 229 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.
404 405 406 |
# File 'lib/ovirtsdk4/connection.rb', line 404 def to_s inspect end |