lib/rbovirt.rb in rbovirt-0.0.35 vs lib/rbovirt.rb in rbovirt-0.0.36

- old
+ new

@@ -9,21 +9,25 @@ require "ovirt/volume" require "ovirt/interface" require "ovirt/network" require "ovirt/quota" require "ovirt/affinity_group" +require "ovirt/instance_type" require "ovirt/version" +require "ovirt/operating_system" require "client/vm_api" require "client/template_api" require "client/cluster_api" require "client/host_api" require "client/datacenter_api" require "client/storage_domain_api" require "client/quota_api" require "client/disk_api" require "client/affinity_group_api" +require "client/instance_type_api" +require "client/operating_system_api" require "nokogiri" require "rest_client" require "restclient_ext/request" require "restclient_ext/resource" @@ -42,11 +46,11 @@ end end class Client - attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id, :filtered_api, :ca_cert_file, :ca_cert_store, :ca_no_verify + attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id, :filtered_api, :ca_cert_file, :ca_cert_store, :ca_no_verify, :persistent_auth, :jsessionid # Construct a new ovirt client class. # mandatory parameters # username, password, api_entrypoint - for example 'me@internal', 'secret', 'https://example.com/api' # optional parameters @@ -64,18 +68,20 @@ # backward compatibility optional parameters options = {:datacenter_id => options, :cluster_id => backward_compatibility_cluster, :filtered_api => backward_compatibility_filtered} end - @api_entrypoint = api_entrypoint - @credentials = { :username => username, :password => password } - @datacenter_id = options[:datacenter_id] - @cluster_id = options[:cluster_id] - @filtered_api = options[:filtered_api] - @ca_cert_file = options[:ca_cert_file] - @ca_cert_store = options[:ca_cert_store] - @ca_no_verify = options[:ca_no_verify] + @api_entrypoint = api_entrypoint + @credentials = { :username => username, :password => password } + @datacenter_id = options[:datacenter_id] + @cluster_id = options[:cluster_id] + @filtered_api = options[:filtered_api] + @ca_cert_file = options[:ca_cert_file] + @ca_cert_store = options[:ca_cert_store] + @ca_no_verify = options[:ca_no_verify] + @persistent_auth = options[:persistent_auth] + @jsessionid = options[:jsessionid] end def api_version return @api_version unless @api_version.nil? xml = http_get("/")/'/api/product_info/version' @@ -90,13 +96,17 @@ xml = http_get("/capabilities") !(xml/"version/custom_properties/custom_property[@name='floppyinject']").empty? end private + def search_url opts - search = opts[:search] || ("datacenter=%s" % current_datacenter.name) - "?search=%s" % CGI.escape(search) + search = opts[:search] || '' + search += " datacenter=\"%s\"" % current_datacenter.name + search += " page #{opts[:page]}" if opts[:page] + max = opts[:max] ? ";max=#{opts[:max]}" : '' + "#{max}?search=#{CGI.escape(search)}" end def current_datacenter @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first end @@ -105,54 +115,51 @@ @current_cluster ||= self.cluster_id ? cluster(self.cluster_id) : clusters.first end def http_get(suburl, headers={}) begin - res = rest_client(suburl).get(http_headers(headers)) - puts "#{res}\n" if ENV['RBOVIRT_LOG_RESPONSE'] - Nokogiri::XML(res) + handle_success(rest_client(suburl).get(http_headers(headers))) rescue handle_fault $! end end def http_post(suburl, body, headers={}) begin - res = rest_client(suburl).post(body, http_headers(headers)) - puts "#{res}\n" if ENV['RBOVIRT_LOG_RESPONSE'] - Nokogiri::XML(res) + handle_success(rest_client(suburl).post(body, http_headers(headers))) rescue handle_fault $! end end def http_put(suburl, body, headers={}) begin - res = rest_client(suburl).put(body, http_headers(headers)) - puts "#{res}\n" if ENV['RBOVIRT_LOG_RESPONSE'] - Nokogiri::XML(res) + handle_success(rest_client(suburl).put(body, http_headers(headers))) rescue handle_fault $! end end def http_delete(suburl, body=nil, headers={}) begin headers = body ? http_headers(headers) : {:accept => 'application/xml'}.merge(auth_header).merge(filter_header) - res = rest_client(suburl).delete_with_payload(body, headers) - puts "#{res}\n" if ENV['RBOVIRT_LOG_RESPONSE'] - Nokogiri::XML(res) + handle_success(rest_client(suburl).delete_with_payload(body, headers)) rescue handle_fault $! end end def auth_header # This is the method for strict_encode64: encoded_credentials = ["#{@credentials[:username]}:#{@credentials[:password]}"].pack("m0").gsub(/\n/,'') - { :authorization => "Basic " + encoded_credentials } + headers = { :authorization => "Basic " + encoded_credentials } + if persistent_auth + headers[:prefer] = 'persistent-auth' + headers[:cookie] = "JSESSIONID=#{jsessionid}" if jsessionid + end + headers end def rest_client(suburl) if (URI.parse(@api_entrypoint)).scheme == 'https' options = {} @@ -184,9 +191,15 @@ def http_headers(headers ={}) filter_header.merge(auth_header).merge({ :content_type => 'application/xml', :accept => 'application/xml', }).merge(headers) + end + + def handle_success(response) + puts "#{response}\n" if ENV['RBOVIRT_LOG_RESPONSE'] + @jsessionid ||= response.cookies['JSESSIONID'] + Nokogiri::XML(response) end def handle_fault(f) if f.is_a?(RestClient::BadRequest) || f.is_a?(RestClient::Conflict) fault = (Nokogiri::XML(f.http_body)/'//fault/detail')