Skip to content

Commit

Permalink
Use AMQP fallback endpoints when available
Browse files Browse the repository at this point in the history
Qpid proton allows for automatic failover in case an error occurs for
the currently active connection. When the Nuage provider is associated
with endpoints with roles `amqp-fallback1` and `amqp-fallback2`, these
will be used to configure the message handler.

Signed-off-by: Gregor Berginc <[email protected]>
  • Loading branch information
gberginc committed Oct 27, 2017
1 parent 8894808 commit 3833be8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
23 changes: 12 additions & 11 deletions app/models/manageiq/providers/nuage/manager_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,27 @@ def verify_credentials(auth_type = nil, options = {})

def event_monitor_options
@event_monitor_options ||= begin
url = ''
amqp = connection_configuration_by_role('amqp')
if (endpoint = amqp.try(:endpoint))
url = "#{endpoint.hostname}:#{endpoint.port}"
end

if (authentication = amqp.try(:authentication))
url = "#{authentication.userid}:#{authentication.password}@#{url}"
end

{
:ems => self,
:url => url,
:urls => amqp_urls,
:sasl_allow_insecure_mechs => true, # Only plain (insecure) mechanism currently supported
}
end
end

private

def amqp_urls
amqp_endpoints = endpoints.select { |e| e.role == 'amqp' || e.role.start_with?('amqp_fallback') }
amqp_auth = authentications.detect { |a| a.authtype == 'amqp' }

amqp_endpoints.map do |e|
url = "#{e.hostname}:#{e.port}"
url = "#{amqp_auth.userid}:#{amqp_auth.password}@#{url}" if amqp_auth
url
end
end

def verify_api_credentials(options = {})
with_provider_connection(options) {}
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ def initialize(options = {})
super()
@options = options

@url = @options.delete(:url)
@topics = @options.delete(:topics)
@test_connection = @options.delete(:test_connection)
@message_handler_block = @options.delete(:message_handler_block)
end

def on_start(event)
@conn = event.container.connect(@url, @options)
@conn = event.container.connect(@options)
unless @test_connection
@topics.each { |topic| event.container.create_receiver(@conn, :source => "topic://#{topic}") }
end
Expand All @@ -28,7 +27,9 @@ def on_connection_error(_event)
end

def on_transport_error(_event)
raise MiqException::MiqHostError, "Transport error"
# Only raise error if single URL is used, as otherwise qpid will attempt
# to fallback to alternative URLs.
raise MiqException::MiqHostError, "Transport error" unless @options[:urls].length > 1
end

def on_message(event)
Expand Down
26 changes: 26 additions & 0 deletions spec/models/manageiq/providers/nuage/network_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,30 @@
end.to raise_error(MiqException::MiqInvalidCredentialsError)
end
end

context '#event_monitor_options' do
before(:each) do
@ems = FactoryGirl.build(:ems_nuage_network, :hostname => "host", :ipaddress => "::1")
@creds = {:amqp => {:userid => "amqp_user", :password => "amqp_pass"}}
@ems.endpoints << Endpoint.create(:role => 'amqp', :hostname => 'amqp_hostname', :port => '5672')
@ems.update_authentication(@creds, :save => false)
end

it 'returns options with a single endpoint' do
opts = @ems.event_monitor_options

expect(opts).to have_attributes(:urls => ['amqp_user:amqp_pass@amqp_hostname:5672'])
end

it 'returns options with a fallback URLs' do
@ems.endpoints << Endpoint.create(:role => 'amqp_fallback1', :hostname => 'amqp_hostname1', :port => '5672')
@ems.endpoints << Endpoint.create(:role => 'amqp_fallback2', :hostname => 'amqp_hostname2', :port => '5672')

opts = @ems.event_monitor_options

expect(opts[:urls]).to include('amqp_user:amqp_pass@amqp_hostname:5672',
'amqp_user:amqp_pass@amqp_hostname1:5672',
'amqp_user:amqp_pass@amqp_hostname2:5672')
end
end
end

0 comments on commit 3833be8

Please sign in to comment.