[PATCH conductor 1/4] BZ#773768 Fix the provider Test Connection

tsedovic at redhat.com tsedovic at redhat.com
Tue Feb 28 17:26:44 UTC 2012


From: Tomas Sedovic <tomas at sedovic.cz>

https://bugzilla.redhat.com/show_bug.cgi?id=773768

Now it uses the parameters that were actually entered in the form instead of
the ones in the database.
---
 src/app/controllers/providers_controller.rb |   32 ++++++++++++++++----------
 src/app/models/provider.rb                  |    8 ++++++
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/app/controllers/providers_controller.rb b/src/app/controllers/providers_controller.rb
index eb4c7b0..8c50a60 100644
--- a/src/app/controllers/providers_controller.rb
+++ b/src/app/controllers/providers_controller.rb
@@ -103,8 +103,11 @@ class ProvidersController < ApplicationController
       params[:provider][:provider_type_id] = provider_type.id
     end
     @provider = Provider.new(params[:provider])
-    if !@provider.connect
-      flash[:warning] = t"providers.flash.warning.connect_failed"
+
+    load_provider_tabs
+    load_providers_types
+
+    if !test_connection(@provider)
       render :action => "new"
     else
       if @provider.save
@@ -112,7 +115,6 @@ class ProvidersController < ApplicationController
         flash[:notice] = t"providers.flash.notice.added"
         redirect_to edit_provider_path(@provider)
       else
-        flash[:warning] = t"providers.flash.error.not_added"
         render :action => "new"
       end
     end
@@ -123,12 +125,21 @@ class ProvidersController < ApplicationController
     require_privilege(Privilege::MODIFY, @provider)
     @provider.attributes = params[:provider]
     provider_disabled = @provider.enabled_changed? && !@provider.enabled
+    @alerts = provider_alerts(@provider)
+    load_provider_tabs
+    load_providers_types
 
     if provider_disabled
       disable_provider
       return
     end
 
+    if params.delete :test_provider
+      test_connection(@provider)
+      render :action => 'edit'
+      return
+    end
+
     if @provider.save
       flash[:notice] = t"providers.flash.notice.updated"
       redirect_to edit_provider_path(@provider)
@@ -136,13 +147,9 @@ class ProvidersController < ApplicationController
       # we reset 'enabled' attribute to real state
       # if save failed
       @provider.reset_enabled!
-      unless @provider.connect
-        flash.now[:warning] = t"providers.flash.warning.connect_failed"
-      else
+      if @provider.connect
         flash[:error] = t"providers.flash.error.not_updated"
       end
-      load_provider_tabs
-      @alerts = provider_alerts(@provider)
       render :action => "edit"
     end
   end
@@ -166,12 +173,13 @@ class ProvidersController < ApplicationController
   end
 
   def test_connection(provider)
-    @provider.errors.clear
-    if @provider.connect
+    provider.errors.clear
+    provider.validate_connection
+    errors = provider.errors[:url]
+    if errors.empty?
       flash.now[:notice] = t"providers.flash.notice.connected"
     else
-      flash.now[:warning] = t"providers.flash.warning.connect_failed"
-      @provider.errors.add :url
+      flash.now[:error] = errors
     end
   end
 
diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb
index 9d5877c..24ce64b 100644
--- a/src/app/models/provider.rb
+++ b/src/app/models/provider.rb
@@ -98,6 +98,14 @@ class Provider < ActiveRecord::Base
     end
   end
 
+  def validate_connection
+    unless connect
+      errors.add :url, I18n.t('providers.flash.warning.connect_failed', :provider => name)
+      return false
+    end
+    true
+  end
+
   def pools
     cloud_accounts.collect {|account| account.pools}.flatten.uniq
   end
-- 
1.7.6.5




More information about the aeolus-devel mailing list