ovirt-engine-sdk 4.2.0.alpha2 → 4.2.0.alpha3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40908e6b88b521aaf3a23e7c0580a1048f6c8ffd
4
- data.tar.gz: e8acc264e9fb9d97b492fbb9981b107296f336e1
3
+ metadata.gz: 80eb53f5a009746e0161ca6038d089370c372df8
4
+ data.tar.gz: 6285abc6584ca83aec8df2be1dfabca6d2485afa
5
5
  SHA512:
6
- metadata.gz: bcac4e7e89424f3ededf6541482a49f2f219597658c8d91faa6fe40d8e0f7c0aea0a97415c5cb8b039e835af9e4931ed1e56e2a394f95e833fb59b834e7728f7
7
- data.tar.gz: 1d844327f93a72ca2ecdd893cdefa40befb17f3e8d73bd396e46de8e1a103673d7fbf9884058adf6d4ee3362b5c2e7503e028fc87eecd2b7f00379da4b409d53
6
+ metadata.gz: 123a67164cf851403136b4af9362d8c3c73826b30a6573bae326827929c4f9df5f46642a02a8f10a16634ae1782f77f3e202ed10a9d2f3069b5d6fbcf95f4616
7
+ data.tar.gz: 9ca5ab17eba09f5a7f1ae1e73d66008a9ccfc76d0f77f589c6cbaa12c5a9f9e0e072f83ded518d68111c6db63fd7d3e4cc5cbeecb4defad008dc08318977504e
data/CHANGES.adoc CHANGED
@@ -2,6 +2,73 @@
2
2
 
3
3
  This document describes the relevant changes between releases of the SDK.
4
4
 
5
+ == 4.2.0-alpha3 / Sep 25 2017
6
+
7
+ Update to model 4.2.19:
8
+
9
+ * Add new `NicNetworkFilterParameter` type and related services.
10
+
11
+ * Add `StorageDomainDisks` and `AttachedStorageDomainDisks`.
12
+
13
+ * Add operation to register storage domain disk.
14
+
15
+ * Add `all_content` parameter to snapshots services.
16
+
17
+ * Add `default_route` value to the `NetworkUsage` enum.
18
+
19
+ * Add the `all_content` parameter to `Host.Get` and `Hosts.List`.
20
+
21
+ * Fix the documentation of the method that lists events
22
+ https://bugzilla.redhat.com/1447622[#1447622].
23
+
24
+ * Fix the direction of the main parameter of the method that adds a
25
+ CDROM to a virtual machine. It should be input and output.
26
+
27
+ * Add the `index` attribute to the `Event` type
28
+ https://bugzilla.redhat.com/1448511[#1448511].
29
+
30
+ * Add `volatile` parameter to the method that starts a virtual machine.
31
+
32
+ * Add `RefreshLun` method to the service that manages a disk.
33
+ https://bugzilla.redhat.com/1404389[#1404389].
34
+
35
+ * Add Link Layer Discovery Protocol (LLDP).
36
+
37
+ * Add a `refresh` parameter to `FilesService.list`.
38
+
39
+ * Add `firewal_type` attribute to the `Cluster` type.
40
+
41
+ * Add `has_illegal_images` attribute to the `Vm` type.
42
+
43
+ * Add support for creating image transfers using disks and snapshots.
44
+
45
+ * Add `size` and `type` properties to the `Image` type.
46
+
47
+ * Add `total_size` attribute to the `Disk` type.
48
+
49
+ * Add support for listing cluster level features, and enabling/disabing
50
+ them for clusters.
51
+
52
+ * Storage domain identifier isn't mandatory to create quota limit.
53
+
54
+ * Don't require deprecated affinity group attributes
55
+ https://bugzilla.redhat.com/1488729[#1488729].
56
+
57
+ * Fix optional fields in storage domain add and update
58
+ https://bugzilla.redhat.com/1488929[1488929].
59
+
60
+ * Add new `HIGH_PERFORMANCE` value to the `VmType` enum.
61
+
62
+ * Add new types and services for access to system configuration options.
63
+
64
+ New features:
65
+
66
+ * Improve log messages so that they contain the method and path of the
67
+ request.
68
+
69
+ * Add `AuthError`, `ConnectionError`, `NotFoundError` and
70
+ `TimeoutError`.
71
+
5
72
  == 4.2.0-alpha2 / Jul 21 2017
6
73
 
7
74
  New features:
data/README.adoc CHANGED
@@ -68,10 +68,26 @@ The {reference}/Connection[Connection] class, as is the mechanism to
68
68
  connect to the server and to get the reference to the root of the
69
69
  services tree.
70
70
 
71
- Error::
71
+ Errors::
72
72
 
73
- The {reference}/Error[Error] class is the exception that the SDK will
74
- raise when it needs to report any error.
73
+ The {reference}/Error[Error] class is the base exception class that the
74
+ SDK will raise when it needs to report any error.
75
+ +
76
+ For certain kinds of errors there are specific error classes, extending
77
+ the base error class:
78
+ +
79
+ * {reference}/AuthError[AuthError] - Raised when authentication or
80
+ authorization fail.
81
+ +
82
+ * {reference}/ConnectionError[ConnectionError] - Raised when the name of
83
+ the server can't be resolved, and when the server is down or
84
+ unreachable.
85
+ +
86
+ * {reference}/NotFoundError[NotFoundError] - Raised when the requested
87
+ object doesn't exist.
88
+ +
89
+ * {reference}/TimeoutError[TimeoutError] - Raised when an operation times
90
+ out.
75
91
 
76
92
  Types::
77
93
 
@@ -106,7 +122,7 @@ locators_ and _service methods_ (described later) may change in the
106
122
  future.
107
123
 
108
124
  There are other classes, like HTTP client classes, readers and writers.
109
- These are used to implement the HTTP communication, and to for XML
125
+ These are used to implement the HTTP communication, and for XML
110
126
  parsing and rendering. Refrain from using them, as they are internal
111
127
  implementation details that may change in the future: backwards
112
128
  compatibility isn't guaranteed.
@@ -127,6 +143,9 @@ connection = OvirtSDK4::Connection.new(
127
143
  password: '...',
128
144
  ca_file: 'ca.pem',
129
145
  )
146
+
147
+ # Get the reference to the root of the tree of services:
148
+ system_service = connection.system_service
130
149
  ----
131
150
 
132
151
  The connection holds expensive resources, including a pool of HTTP
@@ -139,7 +158,8 @@ important to free these resources when they are no longer in use:
139
158
  connection.close
140
159
  ----
141
160
 
142
- Once a connection is closed it can't be reused.
161
+ The connection and all the services that have been obtained from it
162
+ can't be used after the connection has been closed.
143
163
 
144
164
  The `ca.pem` file is required when connecting to a server protected
145
165
  with TLS. In an usual oVirt installation it will be in
@@ -156,9 +176,9 @@ The type classes are pure data containers, they don't have any logic or
156
176
  operations. Instances can be created and modified at will.
157
177
 
158
178
  Creating or modifying one of this instances does *not* have any effect
159
- in the server side, unless one they are explicitly passed to a call to
160
- one of the service methods described below. Changes in the server side
161
- are *not* automatically reflected in the instances that already exist in
179
+ in the server side, unless they are explicitly passed to a call to one
180
+ of the service methods described below. Changes in the server side are
181
+ *not* automatically reflected in the instances that already exist in
162
182
  memory.
163
183
 
164
184
  The constructors of these classes have multiple optional arguments, one
@@ -181,6 +201,28 @@ vm = OvirtSDK4::Vm.new(
181
201
  )
182
202
  ----
183
203
 
204
+ The hashes passed to these constructors are processed recursively. For
205
+ example, in the above code instead of explicitly calling the constructor
206
+ for the `Cluster` and `Template` classes it is also possible to use
207
+ plain hashes:
208
+
209
+ [source,ruby]
210
+ ----
211
+ vm = OvirtSDK4::Vm.new(
212
+ name: 'myvm',
213
+ cluster: {
214
+ name: 'mycluster'
215
+ },
216
+ template: {
217
+ name: 'mytemplate'
218
+ },
219
+ memory: 1073741824
220
+ )
221
+ ----
222
+
223
+ The SDK will internally convert those hashes to the required classes, so
224
+ the result will be exactly the same.
225
+
184
226
  Using the constructors in this way is recommended, but not mandatory.
185
227
  You can also create the instance with no arguments in the call to the
186
228
  constructor, and then populate the object step by step, using the
@@ -192,7 +234,7 @@ vm = OvirtSDK4::Vm.new
192
234
  vm.name = 'myvm'
193
235
  vm.cluster = OvirtSDK4::Cluster.new(name: 'mycluster')
194
236
  vm.template = OvirtSDK4::Template.new(name: 'mytemplate')
195
- vm.memory=1073741824
237
+ vm.memory = 1073741824
196
238
  ----
197
239
 
198
240
  Attributes that are defined as lists of objects in the specification of
@@ -299,7 +341,7 @@ manages the collection of virtual machines of the system lives in
299
341
 
300
342
  In the SDK the root of that tree of services is implemented by the
301
343
  _system service_. It is obtained calling the
302
- {rererence}/Connection#system_service-instance_method[system_service]
344
+ {reference}/Connection#system_service-instance_method[system_service]
303
345
  method of the connection:
304
346
 
305
347
  [source,ruby]
@@ -352,7 +394,8 @@ The services that manage a single object usually have the `get`,
352
394
 
353
395
  Both kinds of services can also have additional _action methods_, which
354
396
  perform actions other than retrieving, creating, updating or removing.
355
- Most frequently they available in services that manage a single object.
397
+ Most frequently they are available in services that manage a single
398
+ object.
356
399
 
357
400
  ==== Using the _get_ methods
358
401
 
@@ -427,7 +470,7 @@ vms_service = system_service.vms_service
427
470
  vms = vms_service.list
428
471
  ----
429
472
 
430
- The result will be a Ruby array containing the instances of
473
+ The result will be a Ruby array containing the instances of the
431
474
  corresponding types. For example, in this case, the result will be a
432
475
  list of instances of the Ruby class {reference}/Vm[Vm].
433
476
 
@@ -456,8 +499,8 @@ of the failure.
456
499
 
457
500
  ==== Using the _add_ methods
458
501
 
459
- These service methods add new elements to the collection. They receive
460
- an instance of the relevant type describing the object to add, send the
502
+ These service methods add new elements to collections. They receive an
503
+ instance of the relevant type describing the object to add, send the
461
504
  request to add it, and return an instance of the type describing the
462
505
  added object.
463
506
 
@@ -469,12 +512,12 @@ For example, to add a new virtual machine named `myvm`:
469
512
  vm = vms_service.add(
470
513
  OvirtSDK4::Vm.new(
471
514
  name: 'myvm',
472
- cluster: OvirtSDK4::Cluster.new(
515
+ cluster: {
473
516
  name: 'mycluster'
474
- ),
475
- template: OvirtSDK4::Template.new(
517
+ },
518
+ template: {
476
519
  name: 'mytemplate'
477
- )
520
+ }
478
521
  )
479
522
  )
480
523
  ----
@@ -539,10 +582,10 @@ updated.
539
582
 
540
583
  ==== Using the _update_ methods
541
584
 
542
- These service methods update existing objects. They receive
543
- an instance of the relevant type describing the update to perform, send
544
- the request to update it, and return an instance of the type describing
545
- the updated object.
585
+ These service methods update existing objects. They receive an instance
586
+ of the relevant type describing the update to perform, send the request
587
+ to update it, and return an instance of the type describing the updated
588
+ object.
546
589
 
547
590
  For example, to update the name of a virtual machine from `myvm` to
548
591
  `newvm`:
@@ -569,7 +612,7 @@ update. For example, try to *avoid* this:
569
612
  [source,ruby]
570
613
  ----
571
614
  # Retrieve the current representation:
572
- vm = vm_service.get()
615
+ vm = vm_service.get
573
616
 
574
617
  # Update the representation, in memory, no request sent
575
618
  # to the server:
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright (c) 2015-2016 Red Hat, Inc.
2
+ Copyright (c) 2015-2017 Red Hat, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -19,9 +19,16 @@ limitations under the License.
19
19
  #include "ov_module.h"
20
20
  #include "ov_error.h"
21
21
 
22
- /* Class: */
22
+ /* Classes: */
23
23
  VALUE ov_error_class;
24
+ VALUE ov_connection_error_class;
25
+ VALUE ov_timeout_error_class;
24
26
 
25
27
  void ov_error_define(void) {
28
+ /* Define the base error class: */
26
29
  ov_error_class = rb_define_class_under(ov_module, "Error", rb_eStandardError);
30
+
31
+ /* Define the specialized error classes: */
32
+ ov_connection_error_class = rb_define_class_under(ov_module, "ConnectionError", ov_error_class);
33
+ ov_timeout_error_class = rb_define_class_under(ov_module, "TimeoutError", ov_error_class);
27
34
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright (c) 2015-2016 Red Hat, Inc.
2
+ Copyright (c) 2015-2017 Red Hat, Inc.
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ limitations under the License.
19
19
 
20
20
  // Classes:
21
21
  extern VALUE ov_error_class;
22
+ extern VALUE ov_connection_error_class;
23
+ extern VALUE ov_timeout_error_class;
22
24
 
23
25
  // Initialization function:
24
26
  extern void ov_error_define(void);
@@ -583,10 +583,12 @@ static int ov_http_client_add_header(VALUE name, VALUE value, struct curl_slist*
583
583
  static void* ov_http_client_complete_task(void* data) {
584
584
  CURLM* handle;
585
585
  CURLMsg* message;
586
- VALUE error;
586
+ VALUE error_class;
587
+ VALUE error_instance;
587
588
  VALUE transfer;
588
589
  long code;
589
590
  ov_http_client_object* client_ptr;
591
+ ov_http_request_object* request_ptr;
590
592
  ov_http_response_object* response_ptr;
591
593
  ov_http_transfer_object* transfer_ptr;
592
594
 
@@ -600,6 +602,7 @@ static void* ov_http_client_complete_task(void* data) {
600
602
  /* Get the pointers to the transfer, client and response: */
601
603
  ov_http_transfer_ptr(transfer, transfer_ptr);
602
604
  ov_http_client_ptr(transfer_ptr->client, client_ptr);
605
+ ov_http_request_ptr(transfer_ptr->request, request_ptr);
603
606
  ov_http_response_ptr(transfer_ptr->response, response_ptr);
604
607
 
605
608
  /* Remove the transfer from the pending hash: */
@@ -617,15 +620,31 @@ static void* ov_http_client_complete_task(void* data) {
617
620
  /* Send a summary of the response to the log: */
618
621
  ov_http_client_log_info(
619
622
  client_ptr->log,
620
- "Received response code '%"PRIsVALUE"'.",
621
- response_ptr->code
623
+ "Received response code %"PRIsVALUE" for %"PRIsVALUE" request to URL '%"PRIsVALUE"'.",
624
+ response_ptr->code,
625
+ request_ptr->method,
626
+ request_ptr->url
622
627
  );
623
628
  }
624
629
  else {
630
+ /* Select the error class according to the kind of error returned by libcurl: */
631
+ switch (message->data.result) {
632
+ case CURLE_COULDNT_CONNECT:
633
+ case CURLE_COULDNT_RESOLVE_HOST:
634
+ case CURLE_COULDNT_RESOLVE_PROXY:
635
+ error_class = ov_connection_error_class;
636
+ break;
637
+ case CURLE_OPERATION_TIMEDOUT:
638
+ error_class = ov_timeout_error_class;
639
+ break;
640
+ default:
641
+ error_class = ov_error_class;
642
+ }
643
+
625
644
  /* Put the request and error in the completed transfers hash: */
626
- error = rb_sprintf("Can't send request: %s", curl_easy_strerror(message->data.result));
627
- error = rb_class_new_instance(1, &error, ov_error_class);
628
- rb_hash_aset(client_ptr->completed, transfer_ptr->request, error);
645
+ error_instance = rb_sprintf("Can't send request: %s", curl_easy_strerror(message->data.result));
646
+ error_instance = rb_class_new_instance(1, &error_instance, error_class);
647
+ rb_hash_aset(client_ptr->completed, transfer_ptr->request, error_instance);
629
648
  }
630
649
 
631
650
  /* Now that the libcurl easy handle is released, we can release the headers as well: */
@@ -806,7 +825,7 @@ static void ov_http_client_prepare_handle(ov_http_client_object* client_ptr, ov_
806
825
  /* Send a summary of the request to the log: */
807
826
  ov_http_client_log_info(
808
827
  client_ptr->log,
809
- "Sending '%"PRIsVALUE"' request to URL '%"PRIsVALUE"'.",
828
+ "Sending %"PRIsVALUE" request to URL '%"PRIsVALUE"'.",
810
829
  request_ptr->method,
811
830
  url
812
831
  );
data/lib/ovirtsdk4.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright (c) 2015-2016 Red Hat, Inc.
2
+ # Copyright (c) 2015-2017 Red Hat, Inc.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ require 'ovirtsdk4c'
28
28
  # Own requirements.
29
29
  #
30
30
  require 'ovirtsdk4/version.rb'
31
- require 'ovirtsdk4/error.rb'
31
+ require 'ovirtsdk4/errors.rb'
32
32
  require 'ovirtsdk4/connection.rb'
33
33
  require 'ovirtsdk4/type.rb'
34
34
  require 'ovirtsdk4/types.rb'
@@ -192,7 +192,7 @@ module OvirtSDK4
192
192
  #
193
193
  def send(request)
194
194
  # Add the base URL to the request:
195
- request.url = request.url.nil? ? request.url = @url : "#{@url}#{request.url}"
195
+ request.url = request.url.nil? ? request.url = @url : "#{@url}/#{request.url}"
196
196
 
197
197
  # Set the headers common to all requests:
198
198
  request.headers.merge!(
@@ -227,6 +227,8 @@ module OvirtSDK4
227
227
  # @param request [HttpRequest] The request object whose corresponding response you want to wait for.
228
228
  # @return [Response] A request object containing the details of the HTTP response received.
229
229
  #
230
+ # @api private
231
+ #
230
232
  def wait(request)
231
233
  # Wait for the response:
232
234
  response = @client.wait(request)
@@ -245,144 +247,6 @@ module OvirtSDK4
245
247
  response
246
248
  end
247
249
 
248
- #
249
- # Obtains the access token from SSO to be used for bearer authentication.
250
- #
251
- # @return [String] The access token.
252
- #
253
- # @api private
254
- #
255
- def create_access_token
256
- # Build the URL and parameters required for the request:
257
- url, parameters = build_sso_auth_request
258
-
259
- # Send the request and wait for the request:
260
- response = get_sso_response(url, parameters)
261
- response = response[0] if response.is_a?(Array)
262
-
263
- # Check the response and raise an error if it contains an error code:
264
- code = response['error_code']
265
- error = response['error']
266
- raise Error, "Error during SSO authentication: #{code}: #{error}" if error
267
-
268
- response['access_token']
269
- end
270
-
271
- #
272
- # Revoke the SSO access token.
273
- #
274
- # @api private
275
- #
276
- def revoke_access_token
277
- # Build the URL and parameters required for the request:
278
- url, parameters = build_sso_revoke_request
279
-
280
- # Send the request and wait for the response:
281
- response = get_sso_response(url, parameters)
282
- response = response[0] if response.is_a?(Array)
283
-
284
- # Check the response and raise an error if it contains an error code:
285
- code = response['error_code']
286
- error = response['error']
287
- raise Error, "Error during SSO revoke: #{code}: #{error}" if error
288
- end
289
-
290
- #
291
- # Execute a get request to the SSO server and return the response.
292
- #
293
- # @param url [String] The URL of the SSO server.
294
- #
295
- # @param parameters [Hash] The parameters to send to the SSO server.
296
- #
297
- # @return [Hash] The JSON response.
298
- #
299
- # @api private
300
- #
301
- def get_sso_response(url, parameters)
302
- # Create the request:
303
- request = HttpRequest.new
304
- request.method = :POST
305
- request.url = url
306
- request.headers = {
307
- 'User-Agent' => "RubySDK/#{VERSION}",
308
- 'Content-Type' => 'application/x-www-form-urlencoded',
309
- 'Accept' => 'application/json'
310
- }
311
- request.body = URI.encode_www_form(parameters)
312
-
313
- # Add the global headers:
314
- request.headers.merge!(@headers) if @headers
315
-
316
- # Send the request and wait for the response:
317
- @client.send(request)
318
- response = @client.wait(request)
319
- raise response if response.is_a?(Exception)
320
-
321
- # Check the returned content type:
322
- check_json_content_type(response)
323
-
324
- # Parse and return the JSON response:
325
- JSON.parse(response.body)
326
- end
327
-
328
- #
329
- # Builds a the URL and parameters to acquire the access token from SSO.
330
- #
331
- # @return [Array] An array containing two elements, the first is the URL of the SSO service and the second is a hash
332
- # containing the parameters required to perform authentication.
333
- #
334
- # @api private
335
- #
336
- def build_sso_auth_request
337
- # Compute the entry point and the parameters:
338
- parameters = {
339
- scope: 'ovirt-app-api'
340
- }
341
- if @kerberos
342
- entry_point = 'token-http-auth'
343
- parameters[:grant_type] = 'urn:ovirt:params:oauth:grant-type:http'
344
- else
345
- entry_point = 'token'
346
- parameters.merge!(
347
- grant_type: 'password',
348
- username: @username,
349
- password: @password
350
- )
351
- end
352
-
353
- # Compute the URL:
354
- url = URI(@url.to_s)
355
- url.path = "/ovirt-engine/sso/oauth/#{entry_point}"
356
- url = url.to_s
357
-
358
- # Return the pair containing the URL and the parameters:
359
- [url, parameters]
360
- end
361
-
362
- #
363
- # Builds a the URL and parameters to revoke the SSO access token
364
- #
365
- # @return [Array] An array containing two elements, the first is the URL of the SSO service and the second is a hash
366
- # containing the parameters required to perform the revoke.
367
- #
368
- # @api private
369
- #
370
- def build_sso_revoke_request
371
- # Compute the parameters:
372
- parameters = {
373
- scope: '',
374
- token: @token
375
- }
376
-
377
- # Compute the URL:
378
- url = URI(@url.to_s)
379
- url.path = '/ovirt-engine/services/sso-logout'
380
- url = url.to_s
381
-
382
- # Return the pair containing the URL and the parameters:
383
- [url, parameters]
384
- end
385
-
386
250
  #
387
251
  # Tests the connectivity with the server. If connectivity works correctly it returns `true`. If there is any
388
252
  # connectivity problem it will either return `false` or raise an exception if the `raise_exception` parameter is
@@ -546,7 +410,16 @@ module OvirtSDK4
546
410
  end
547
411
 
548
412
  # Create and populate the error:
549
- error = Error.new(message)
413
+ klass = Error
414
+ unless response.nil?
415
+ case response.code
416
+ when 401, 403
417
+ klass = AuthError
418
+ when 404
419
+ klass = NotFoundError
420
+ end
421
+ end
422
+ error = klass.new(message)
550
423
  error.code = response.code if response
551
424
  error.fault = fault
552
425
 
@@ -596,5 +469,161 @@ module OvirtSDK4
596
469
  end
597
470
  raise_error(response, detail)
598
471
  end
472
+
473
+ #
474
+ # Obtains the access token from SSO to be used for bearer authentication.
475
+ #
476
+ # @return [String] The access token.
477
+ #
478
+ # @api private
479
+ #
480
+ def create_access_token
481
+ # Build the URL and parameters required for the request:
482
+ url, parameters = build_sso_auth_request
483
+
484
+ # Send the request and wait for the request:
485
+ response = get_sso_response(url, parameters)
486
+ response = response[0] if response.is_a?(Array)
487
+
488
+ # Check the response and raise an error if it contains an error code:
489
+ error = get_sso_error_message(response)
490
+ raise AuthError, "Error during SSO authentication: #{error}" if error
491
+
492
+ response['access_token']
493
+ end
494
+
495
+ #
496
+ # Revoke the SSO access token.
497
+ #
498
+ # @api private
499
+ #
500
+ def revoke_access_token
501
+ # Build the URL and parameters required for the request:
502
+ url, parameters = build_sso_revoke_request
503
+
504
+ # Send the request and wait for the response:
505
+ response = get_sso_response(url, parameters)
506
+ response = response[0] if response.is_a?(Array)
507
+
508
+ # Check the response and raise an error if it contains an error code:
509
+ error = get_sso_error_message(response)
510
+ raise AuthError, "Error during SSO revoke: #{error}" if error
511
+ end
512
+
513
+ #
514
+ # Execute a get request to the SSO server and return the response.
515
+ #
516
+ # @param url [String] The URL of the SSO server.
517
+ #
518
+ # @param parameters [Hash] The parameters to send to the SSO server.
519
+ #
520
+ # @return [Hash] The JSON response.
521
+ #
522
+ # @api private
523
+ #
524
+ def get_sso_response(url, parameters)
525
+ # Create the request:
526
+ request = HttpRequest.new
527
+ request.method = :POST
528
+ request.url = url
529
+ request.headers = {
530
+ 'User-Agent' => "RubySDK/#{VERSION}",
531
+ 'Content-Type' => 'application/x-www-form-urlencoded',
532
+ 'Accept' => 'application/json'
533
+ }
534
+ request.body = URI.encode_www_form(parameters)
535
+
536
+ # Add the global headers:
537
+ request.headers.merge!(@headers) if @headers
538
+
539
+ # Send the request and wait for the response:
540
+ @client.send(request)
541
+ response = @client.wait(request)
542
+ raise response if response.is_a?(Exception)
543
+
544
+ # Check the returned content type:
545
+ check_json_content_type(response)
546
+
547
+ # Parse and return the JSON response:
548
+ JSON.parse(response.body)
549
+ end
550
+
551
+ #
552
+ # Builds a the URL and parameters to acquire the access token from SSO.
553
+ #
554
+ # @return [Array] An array containing two elements, the first is the URL of the SSO service and the second is a hash
555
+ # containing the parameters required to perform authentication.
556
+ #
557
+ # @api private
558
+ #
559
+ def build_sso_auth_request
560
+ # Compute the entry point and the parameters:
561
+ parameters = {
562
+ scope: 'ovirt-app-api'
563
+ }
564
+ if @kerberos
565
+ entry_point = 'token-http-auth'
566
+ parameters[:grant_type] = 'urn:ovirt:params:oauth:grant-type:http'
567
+ else
568
+ entry_point = 'token'
569
+ parameters.merge!(
570
+ grant_type: 'password',
571
+ username: @username,
572
+ password: @password
573
+ )
574
+ end
575
+
576
+ # Compute the URL:
577
+ url = URI(@url.to_s)
578
+ url.path = "/ovirt-engine/sso/oauth/#{entry_point}"
579
+ url = url.to_s
580
+
581
+ # Return the pair containing the URL and the parameters:
582
+ [url, parameters]
583
+ end
584
+
585
+ #
586
+ # Builds a the URL and parameters to revoke the SSO access token
587
+ #
588
+ # @return [Array] An array containing two elements, the first is the URL of the SSO service and the second is a hash
589
+ # containing the parameters required to perform the revoke.
590
+ #
591
+ # @api private
592
+ #
593
+ def build_sso_revoke_request
594
+ # Compute the parameters:
595
+ parameters = {
596
+ scope: '',
597
+ token: @token
598
+ }
599
+
600
+ # Compute the URL:
601
+ url = URI(@url.to_s)
602
+ url.path = '/ovirt-engine/services/sso-logout'
603
+ url = url.to_s
604
+
605
+ # Return the pair containing the URL and the parameters:
606
+ [url, parameters]
607
+ end
608
+
609
+ #
610
+ # Extrats the error message from the given SSO response.
611
+ #
612
+ # @param response [Hash] The result of parsing the JSON document returned by the SSO server.
613
+ # @return [String] The error message, or `nil` if there was no error.
614
+ #
615
+ def get_sso_error_message(response)
616
+ # OAuth uses the 'error_code' attribute for the error code, and 'error' for the error description. But OpenID uses
617
+ # 'error' for the error code and 'error_description' for the description. So we need to check if the
618
+ # 'error_description' attribute is present, and extract the code and description accordingly.
619
+ description = response['error_description']
620
+ if description.nil?
621
+ code = response['error_code']
622
+ description = response['error']
623
+ else
624
+ code = response['error']
625
+ end
626
+ "#{code}: #{description}" if code
627
+ end
599
628
  end
600
629
  end