Class: OvirtSDK4::Connection
- Inherits:
-
Object
- Object
- OvirtSDK4::Connection
- Defined in:
- lib/ovirtsdk4/http.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)
-
- (Object) close
Releases the resources used by this connection.
-
- (Object) follow_link(object)
Follows the
href
attribute of the given object, retrieves the target object and returns it. -
- (Connection) initialize(opts = {})
constructor
Creates a new connection to the API server.
-
- (Boolean) is_link?(object)
Indicates if the given object is a link.
-
- (Service) service(path)
Returns a reference to the service corresponding to the given path.
-
- (SystemService) system_service
Returns a reference to the root of the services tree.
-
- (Boolean) test(raise_exception = false)
Tests the connectivity with the server.
-
- (String) url
Returns the base URL of this connection.
Constructor Details
- (Connection) initialize(opts = {})
Creates a new connection to the API server.
Note that all the parameters with names starting with sso
are intended for use with external authentication
services, using the OAuth2 protocol. But the typical usage doesn’t require them, as they
are automatically calculated to use the authentication service that is part of the engine. A typical connection
can be created specifying just the url
, username
, password
and ca_file
parameters:
connection = OvirtSDK4::Connection.new(
:url => 'https://engine.example.com/ovirt-engine/api',
:username => 'admin@internal',
:password => '...',
:ca_file => '/etc/pki/ovirt-engine/ca.pem',
)
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/ovirtsdk4/http.rb', line 165 def initialize(opts = {}) # Get the values of the parameters and assign default values: url = opts[:url] username = opts[:username] password = opts[:password] insecure = opts[:insecure] || false ca_file = opts[:ca_file] debug = opts[:debug] || false log = opts[:log] kerberos = opts[:kerberos] || false timeout = opts[:timeout] || 0 compress = opts[:compress] || false sso_url = opts[:sso_url] sso_revoke_url = opts[:sso_revoke_url] sso_insecure = opts[:sso_insecure] || insecure sso_ca_file = opts[:sso_ca_file] || ca_file sso_debug = opts[:sso_debug] || debug sso_log = opts[:sso_log] || log sso_timeout = opts[:sso_timeout] || timeout sso_token_name = opts[:sso_token_name] || 'access_token' # Check mandatory parameters: if url.nil? raise ArgumentError.new("The \"url\" parameter is mandatory.") end # Save the URL: @url = URI(url) # Save SSO parameters: @sso_url = sso_url @sso_revoke_url = sso_revoke_url @username = username @password = password @kerberos = kerberos @sso_insecure = sso_insecure @sso_ca_file = sso_ca_file @sso_log_file = sso_log @sso_debug = sso_debug @sso_timeout = sso_timeout @log_file = log @sso_token_name = sso_token_name # Create the cURL handle: @curl = Curl::Easy.new # Configure TLS parameters: if @url.scheme == 'https' if insecure @curl.ssl_verify_peer = false @curl.ssl_verify_host = false elsif !ca_file.nil? raise ArgumentError.new("The CA file \"#{ca_file}\" doesn't exist.") unless ::File.file?(ca_file) @curl.cacert = ca_file end end # Configure the timeout: @curl.timeout = timeout # Configure compression of responses (setting the value to a zero length string means accepting all the # compression types that libcurl supports): if compress @curl.encoding = '' end # Configure debug mode: @close_log = false if debug if log.nil? @log = STDOUT elsif log.is_a?(String) @log = ::File.open(log, 'a') @close_log = true else @log = log end @curl.verbose = true @curl.on_debug do |type, data| case type when Curl::CURLINFO_DATA_IN prefix = '< ' when Curl::CURLINFO_DATA_OUT prefix = '> ' when Curl::CURLINFO_HEADER_IN prefix = '< ' when Curl::CURLINFO_HEADER_OUT prefix = '> ' else prefix = '* ' end lines = data.gsub("\r\n", "\n").strip.split("\n") lines.each do |line| @log.puts(prefix + line) end @log.flush end end end |
Instance Method Details
- (Object) close
Releases the resources used by this connection.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/ovirtsdk4/http.rb', line 609 def close # Send the last request to indicate the server that the session should be closed: request = Request.new({ :method => :HEAD, }) send(request) # Revoke the SSO access token: revoke_access_token # Close the log file, if we did open it: if @close_log @log.close end # Release resources used by the cURL handle: @curl.close end |
- (Object) follow_link(object)
Follows the href
attribute of the given object, retrieves the target object and returns it.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/ovirtsdk4/http.rb', line 579 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.new("Can't follow link because the 'href' attribute does't have a value") end # Check that the value of the "href" attribute is compatible with the base URL of the connection: prefix = @url.path if !prefix.end_with?('/') prefix += '/' end if !href.start_with?(prefix) raise Error.new("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 |
- (Boolean) is_link?(object)
Indicates if the given object is a link. An object is a link if it has an href
attribute.
569 570 571 |
# File 'lib/ovirtsdk4/http.rb', line 569 def is_link?(object) return !object.href.nil? end |
- (Service) service(path)
Returns a reference to the service corresponding to the given path. For example, if the path
parameter
is vms/123/disks
then it will return a reference to the service that manages the disks for the virtual
machine with identifier 123
.
294 295 296 |
# File 'lib/ovirtsdk4/http.rb', line 294 def service(path) return system_service.service(path) end |
- (SystemService) system_service
Returns a reference to the root of the services tree.
280 281 282 283 |
# File 'lib/ovirtsdk4/http.rb', line 280 def system_service @system_service ||= SystemService.new(self, "") return @system_service end |
- (Boolean) test(raise_exception = false)
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
.
554 555 556 557 558 559 560 561 562 |
# File 'lib/ovirtsdk4/http.rb', line 554 def test(raise_exception = false) begin system_service.get return true rescue Exception raise if raise_exception return false end end |
- (String) url
Returns the base URL of this connection.
271 272 273 |
# File 'lib/ovirtsdk4/http.rb', line 271 def url return @url end |