require "ovirt/base_object" require "ovirt/cluster" require "ovirt/datacenter" require "ovirt/host" require "ovirt/storage_domain" require "ovirt/template" require "ovirt/vm" require "ovirt/volume" require "ovirt/interface" require "ovirt/network" 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 "nokogiri" require "rest_client" module OVIRT class OvirtVersionUnsupportedException < StandardError; end class OvirtException < StandardError def initialize(message) @message = message super end def message @message end end class Client attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id def initialize(username, password, api_entrypoint, datacenter_id=nil, cluster_id=nil) @credentials = { :username => username, :password => password } @datacenter_id = datacenter_id @cluster_id = cluster_id @api_entrypoint = api_entrypoint end def api_version xml = http_get("/")/'/api/product_info/version' (xml/'version').first[:major] +"."+ (xml/'version').first[:minor] end def api_version?(major) api_version.split('.')[0] == major end private def current_datacenter @current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first end def current_cluster @current_cluster ||= self.cluster_id ? cluster(self.cluster_id) : clusters.first end def http_get(suburl, headers={}) begin Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].get(http_headers(headers))) rescue handle_fault $! end end def http_post(suburl, body, headers={}) begin Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].post(body, http_headers(headers))) rescue handle_fault $! end end def http_put(suburl, body, headers={}) begin Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].put(body, http_headers(headers))) rescue handle_fault $! end end def http_delete(suburl) begin headers = {:accept => 'application/xml'}.merge(auth_header) Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].delete(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 } end def base_url url = URI.parse(@api_entrypoint) "#{url.scheme}://#{url.host}:#{url.port}" end def self.parse_response(response) Nokogiri::XML(response) end def has_datacenter?(vm) value=!(vm/'data_center').empty? value end def http_headers(headers ={}) headers.merge({ :content_type => 'application/xml', :accept => 'application/xml' }).merge(auth_header) end def handle_fault(f) if f.is_a?(RestClient::BadRequest) fault = (Nokogiri::XML(f.http_body)/'//fault/detail') fault = fault.text.gsub(/\[|\]/, '') if fault end fault ||= f.message raise OvirtException::new(fault) end end class Link attr_accessor :id, :href, :client def initialize(client, id, href) @id, @href = id, href @client = client end def follow xml = Client::parse_response(OVIRT::client(@client.base_url)[@href].get(@client.auth_header)) object_class = ::OVIRT.const_get(xml.root.name.camelize) object_class.new(@client, (xml.root)) end end end