2013-03-28 11:52:27 -04:00
|
|
|
require 'fog/core'
|
|
|
|
|
2012-04-02 07:25:03 -04:00
|
|
|
module Fog
|
|
|
|
module XenServer
|
2012-04-02 12:10:56 -04:00
|
|
|
|
|
|
|
class InvalidLogin < Fog::Errors::Error; end
|
2012-04-04 03:01:55 -04:00
|
|
|
class NotFound < Fog::Errors::Error; end
|
|
|
|
class RequestFailed < Fog::Errors::Error; end
|
2012-04-02 12:10:56 -04:00
|
|
|
|
2012-04-02 07:25:03 -04:00
|
|
|
extend Fog::Provider
|
|
|
|
|
|
|
|
service(:compute, 'xenserver/compute', 'Compute')
|
|
|
|
|
|
|
|
class Connection
|
|
|
|
require 'xmlrpc/client'
|
|
|
|
|
2013-12-05 06:42:28 -05:00
|
|
|
def initialize(host, timeout)
|
2012-04-02 07:25:03 -04:00
|
|
|
@factory = XMLRPC::Client.new(host, '/')
|
2013-01-03 23:41:28 -05:00
|
|
|
@factory.set_parser(NokogiriStreamParser.new)
|
2013-12-05 06:42:28 -05:00
|
|
|
@factory.timeout = timeout
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate( username, password )
|
2012-12-05 18:45:39 -05:00
|
|
|
response = @factory.call('session.login_with_password', username.to_s, password.to_s)
|
2012-04-02 12:10:56 -04:00
|
|
|
raise Fog::XenServer::InvalidLogin.new unless response["Status"] =~ /Success/
|
2012-04-02 07:25:03 -04:00
|
|
|
@credentials = response["Value"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(options, *params)
|
|
|
|
begin
|
|
|
|
parser = options.delete(:parser)
|
|
|
|
method = options.delete(:method)
|
|
|
|
|
|
|
|
if params.empty?
|
|
|
|
response = @factory.call(method, @credentials)
|
|
|
|
else
|
|
|
|
if params.length.eql?(1) and params.first.is_a?(Hash)
|
|
|
|
response = @factory.call(method, @credentials, params.first)
|
2012-06-07 13:26:47 -04:00
|
|
|
elsif params.length.eql?(2) and params.last.is_a?(Array)
|
|
|
|
response = @factory.call(method, @credentials, params.first, params.last)
|
2012-04-02 07:25:03 -04:00
|
|
|
else
|
|
|
|
response = eval("@factory.call('#{method}', '#{@credentials}', #{params.map {|p| p.is_a?(String) ? "'#{p}'" : p}.join(',')})")
|
|
|
|
end
|
|
|
|
end
|
2012-04-04 13:48:21 -04:00
|
|
|
raise RequestFailed.new("#{method}: " + response["ErrorDescription"].to_s) unless response["Status"].eql? "Success"
|
2012-04-02 07:25:03 -04:00
|
|
|
if parser
|
|
|
|
parser.parse( response["Value"] )
|
|
|
|
response = parser.response
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-03 23:41:28 -05:00
|
|
|
class NokogiriStreamParser < XMLRPC::XMLParser::AbstractStreamParser
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
require 'nokogiri/xml/sax/document'
|
|
|
|
require 'nokogiri/xml/sax/parser'
|
|
|
|
|
|
|
|
@parser_class = Class.new(Nokogiri::XML::SAX::Document) do
|
|
|
|
|
|
|
|
include XMLRPC::XMLParser::StreamParserMixin
|
|
|
|
|
|
|
|
alias_method :start_element, :startElement
|
|
|
|
alias_method :end_element, :endElement
|
|
|
|
alias_method :characters, :character
|
|
|
|
alias_method :cdata_block, :character
|
|
|
|
|
|
|
|
def parse(str)
|
|
|
|
Nokogiri::XML::SAX::Parser.new(self).parse(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|