Skip to content
Permalink
Browse files

Merge pull request #76 from zozoh94/redirect_to_master

Support master and slave
  • Loading branch information
plribeiro3000 committed Nov 8, 2018
2 parents b6752f9 + 53a6f67 commit e43867d11c327720dd21bf9622db0f048f1c4036
Showing with 37 additions and 36 deletions.
  1. +8 −13 lib/fog/xenserver/compute/real.rb
  2. +29 −23 lib/fog/xenserver/connection.rb
@@ -5,23 +5,18 @@ class Real
attr_reader :host, :username

def initialize(options={})
@host = options[:xenserver_url]
@username = options[:xenserver_username]
@password = options[:xenserver_password]
@defaults = options[:xenserver_defaults] || {}
@timeout = options[:xenserver_timeout] || 30
@use_ssl = options[:xenserver_use_ssl] || false
@port = options[:xenserver_port] || 80
@verify_mode = options[:xenserver_verify_mode] || OpenSSL::SSL::VERIFY_PEER
@host = options[:xenserver_url]
@username = options[:xenserver_username]
@password = options[:xenserver_password]
@defaults = options[:xenserver_defaults] || {}
@timeout = options[:xenserver_timeout] || 30
@use_ssl = options[:xenserver_use_ssl] || false
@port = options[:xenserver_port] || 80
@verify_mode = options[:xenserver_verify_mode] || OpenSSL::SSL::VERIFY_PEER
@connection = Fog::XenServer::Connection.new(
@host, @port, @use_ssl, @verify_mode, @timeout)

@connection.authenticate(@username, @password)

if @connection.slave?
@connection = Fog::XenServer::Connection.new(@connection.master, @port, @use_ssl, @verify_mode, @timeout)
@connection.authenticate(@username, @password)
end
end

def reload
@@ -6,31 +6,43 @@ class Connection
attr_reader :credentials

def initialize(host, port, use_ssl, verify_mode, timeout)
@factory = XMLRPC::Client.new3(host: host, port: port, use_ssl: use_ssl, path: "/")
@host = host
@port = port
@use_ssl = use_ssl
@verify_mode = verify_mode
@timeout = timeout
connect
end

def connect
@factory = XMLRPC::Client.new3(host: @host, port: @port, use_ssl: @use_ssl, path: "/")
if @factory.respond_to?(:http)
@factory.http.verify_mode = verify_mode
@factory.http.verify_mode = @verify_mode
else
@factory.instance_variable_get(:@http).verify_mode = verify_mode
@factory.instance_variable_get(:@http).verify_mode = @verify_mode
end
@factory.set_parser(NokogiriStreamParser.new)
@factory.timeout = timeout
end

def slave?
master_slave_request
@ms_response["Status"] == "Failure"
end

def master
master_slave_request
return if @ms_response["ErrorDescription"].nil?
return unless @ms_response["ErrorDescription"][0] == "HOST_IS_SLAVE"
@ms_response["ErrorDescription"][1]
@factory.timeout = @timeout
end

def authenticate( username, password )
response = @factory.call("session.login_with_password", username.to_s, password.to_s)
raise Fog::XenServer::InvalidLogin.new unless response["Status"] =~ /Success/
unless response["Status"] =~ /Success/
if response.key?("ErrorDescription") &&
response["ErrorDescription"].is_a?(Array) &&
response["ErrorDescription"].length >= 2 &&
response["ErrorDescription"][0] == "HOST_IS_SLAVE"
@host = response["ErrorDescription"][1]
connect
response = @factory.call("session.login_with_password", username.to_s, password.to_s)
unless response["Status"] =~ /Success/
raise Fog::XenServer::InvalidLogin.new
end
@credentials = response["Value"]
return
end
raise Fog::XenServer::InvalidLogin.new
end
@credentials = response["Value"]
end

@@ -61,12 +73,6 @@ def request(options, *params)
response
end
end

private

def master_slave_request
@ms_response ||= @factory.call("host.get_all_records", @credentials)
end
end
end
end

0 comments on commit e43867d

Please sign in to comment.
You can’t perform that action at this time.