lib/rbovirt.rb in rbovirt-0.0.15 vs lib/rbovirt.rb in rbovirt-0.0.16
- old
+ new
@@ -33,17 +33,18 @@
end
end
class Client
- attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id
+ attr_reader :credentials, :api_entrypoint, :datacenter_id, :cluster_id, :filtered_api
- def initialize(username, password, api_entrypoint, datacenter_id=nil, cluster_id=nil)
+ def initialize(username, password, api_entrypoint, datacenter_id=nil, cluster_id=nil, filtered_api = false)
@credentials = { :username => username, :password => password }
@datacenter_id = datacenter_id
@cluster_id = cluster_id
@api_entrypoint = api_entrypoint
+ @filtered_api = filtered_api
end
def api_version
return @api_version unless @api_version.nil?
xml = http_get("/")/'/api/product_info/version'
@@ -58,10 +59,15 @@
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)
+ end
+
def current_datacenter
@current_datacenter ||= self.datacenter_id ? datacenter(self.datacenter_id) : datacenters.first
end
def current_cluster
@@ -92,11 +98,11 @@
end
end
def http_delete(suburl)
begin
- headers = {:accept => 'application/xml'}.merge(auth_header)
+ headers = {:accept => 'application/xml'}.merge(auth_header).merge(filter_header)
Nokogiri::XML(RestClient::Resource.new(@api_entrypoint)[suburl].delete(headers))
rescue
handle_fault $!
end
end
@@ -105,28 +111,31 @@
# This is the method for strict_encode64:
encoded_credentials = ["#{@credentials[:username]}:#{@credentials[:password]}"].pack("m0").gsub(/\n/,'')
{ :authorization => "Basic " + encoded_credentials }
end
+ def filter_header
+ filtered_api ? { :filter => "true" } : {}
+ 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
+ (vm/'data_center').any?
end
def http_headers(headers ={})
headers.merge({
:content_type => 'application/xml',
- :accept => 'application/xml'
- }).merge(auth_header)
+ :accept => 'application/xml',
+ }).merge(auth_header).merge(filter_header)
end
def handle_fault(f)
if f.is_a?(RestClient::BadRequest)
fault = (Nokogiri::XML(f.http_body)/'//fault/detail')