From ae509d43d0d591f91845a4042d6b7125e9f68e85 Mon Sep 17 00:00:00 2001 From: ooVoo LLC Date: Sun, 21 Dec 2014 20:01:40 +0200 Subject: [PATCH] Fixed the issue according pool request #3356 --- lib/fog/xenserver/compute.rb | 31 +++++++++++++++++++------------ lib/fog/xenserver/core.rb | 23 ++++++++++++----------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/fog/xenserver/compute.rb b/lib/fog/xenserver/compute.rb index 8cd8de748..67af9a848 100644 --- a/lib/fog/xenserver/compute.rb +++ b/lib/fog/xenserver/compute.rb @@ -11,6 +11,7 @@ module Fog requires :xenserver_url recognizes :xenserver_defaults recognizes :xenserver_timeout + recognizes :xenserver_redirect_to_master model_path 'fog/xenserver/models/compute' model :blob @@ -117,18 +118,24 @@ module Fog attr_reader :connection def initialize(options={}) - @host = options[:xenserver_url] - @username = options[:xenserver_username] - @password = options[:xenserver_password] - @defaults = options[:xenserver_defaults] || {} - @timeout = options[:xenserver_timeout] || 30 - @connection = Fog::XenServer::Connection.new(@host, @timeout) - host_master = @connection.find_pool_master(@username, @password) - if host_master!= @host - @host = host_master; - @connection = Fog::XenServer::Connection.new(@host, @timeout) - end - @connection.authenticate(@username, @password) + @host = options[:xenserver_url] + @username = options[:xenserver_username] + @password = options[:xenserver_password] + @defaults = options[:xenserver_defaults] || {} + @timeout = options[:xenserver_timeout] || 30 + @redirect_to_master = options[:xenserver_redirect_to_master] || false + @connection = Fog::XenServer::Connection.new(@host, @timeout) + + if @redirect_to_master == false + @connection = Fog::XenServer::Connection.new(@host, @timeout) + elsif @redirect_to_master == true + host_master = @connection.find_pool_master(@username, @password) + if host_master && host_master!= @host + @host = host_master; + @connection = Fog::XenServer::Connection.new(@host, @timeout) + end + end + @connection.authenticate(@username, @password) end def reload diff --git a/lib/fog/xenserver/core.rb b/lib/fog/xenserver/core.rb index 8c7ace119..c7718dfd4 100644 --- a/lib/fog/xenserver/core.rb +++ b/lib/fog/xenserver/core.rb @@ -13,7 +13,6 @@ module Fog class Connection require 'xmlrpc/client' - attr_reader :credentials def initialize(host, timeout) @@ -23,18 +22,20 @@ module Fog end def find_pool_master( username, password ) - response = @factory.call('session.login_with_password', username.to_s, password.to_s) - raise Fog::XenServer::InvalidLogin.new unless response["Status"] =~ /Success/ - @credentials = response["Value"] - response = @factory.call('host.get_all_records', @credentials) - if response['Status'] == "Failure" - if response['ErrorDescription'][0] == "HOST_IS_SLAVE" - response['ErrorDescription'][1] - end - end + @credentials = authenticate( username, password ) + response = @factory.call('host.get_all_records', @credentials) + if response['Status'] == "Failure" + if response['ErrorDescription'][0] == "HOST_IS_SLAVE" + ip_address = response['ErrorDescription'][1] + ip_address = ip_address.chomp + valid = !(IPAddr.new(ip_address) rescue nil).nil? + if valid + response['ErrorDescription'][1] + end + end + end 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/