[PATCH conductor 3/4] BZ#773768 Verify the account connection on image push

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


From: Tomas Sedovic <tomas at sedovic.cz>

---
 src/app/controllers/images_controller.rb          |    9 ++++++++-
 src/app/controllers/provider_images_controller.rb |    7 +++++++
 src/app/models/provider_account.rb                |   21 ++++++++++++++++-----
 src/app/views/providers/_edit.html.haml           |    2 +-
 src/config/locales/en.yml                         |    1 +
 5 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/app/controllers/images_controller.rb b/src/app/controllers/images_controller.rb
index dcca913..282ca76 100644
--- a/src/app/controllers/images_controller.rb
+++ b/src/app/controllers/images_controller.rb
@@ -145,8 +145,10 @@ class ImagesController < ApplicationController
       return
     end
     accounts = @environment.provider_accounts
+    valid_accounts, invalid_accounts = accounts.partition { |acc| acc.connect }
     target_images = @build.target_images
-    accounts.each do |account|
+
+    valid_accounts.each do |account|
       if account.image_status(@image) == :not_pushed
         target = account.provider.provider_type.deltacloud_driver
         target_image = target_images.find { |ti| ti.target == target }
@@ -160,6 +162,11 @@ class ImagesController < ApplicationController
         provider_image.save!
       end
     end
+
+    unless invalid_accounts.empty?
+      flash[:warning] = t 'images.flash.warning.push_invalid_accounts'
+      flash[:error] = invalid_accounts.map{|a| a.errors[:provider]}.flatten
+    end
     redirect_to image_path(@image.id)
   end
 
diff --git a/src/app/controllers/provider_images_controller.rb b/src/app/controllers/provider_images_controller.rb
index 60fabe5..81b3c83 100644
--- a/src/app/controllers/provider_images_controller.rb
+++ b/src/app/controllers/provider_images_controller.rb
@@ -19,6 +19,13 @@ class ProviderImagesController < ApplicationController
 
   def create
     provider_account = ProviderAccount.find(params[:account_id])
+
+    if !provider_account.connect
+      flash[:error] = provider_account.errors[:provider]
+      redirect_to image_path(params[:image_id])
+      return
+    end
+
     @provider_image = Aeolus::Image::Factory::ProviderImage.new(
       :provider => provider_account.provider.name,
       :credentials => provider_account.to_xml(:with_credentials => true),
diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb
index 6548d80..8a4d891 100644
--- a/src/app/models/provider_account.rb
+++ b/src/app/models/provider_account.rb
@@ -144,14 +144,25 @@ class ProviderAccount < ActiveRecord::Base
       opts = {:username => credentials_hash['username'],
               :password => credentials_hash['password'],
               :driver => provider.provider_type.deltacloud_driver }
-      opts[:provider] = provider.deltacloud_provider if provider.deltacloud_provider
-      client = DeltaCloud.new(credentials_hash['username'],
-                              credentials_hash['password'],
-                              provider.url)
-      return client.with_config(opts)
+      client = DeltaCloud.new(nil, nil, provider.url)
+      client.use_config!(opts)
+
+      driver_connection = client.driver(provider.provider_type.deltacloud_driver)
+      if provider.deltacloud_provider.present?
+        if driver_connection.valid_provider?(provider.deltacloud_provider)
+          opts[:provider] = provider.deltacloud_provider
+          client.use_config!(opts)
+        else
+          errors.add :provider, I18n.t('providers.flash.warning.invalid_x_deltacloud_provider', :provider => provider.name)
+          return nil
+        end
+      end
+
+      return client
     rescue Exception => e
       logger.error("Error connecting to framework: #{e.message}")
       logger.error("Backtrace: #{e.backtrace.join("\n")}")
+      errors.add :provider, I18n.t('providers.flash.warning.connect_failed', :provider => provider.name)
       return nil
     end
   end
diff --git a/src/app/views/providers/_edit.html.haml b/src/app/views/providers/_edit.html.haml
index 6dd36bb..eca87fb 100644
--- a/src/app/views/providers/_edit.html.haml
+++ b/src/app/views/providers/_edit.html.haml
@@ -4,5 +4,5 @@
     %fieldset.options
       #obj_actions.button-group
         = f.submit "Save Changes", :class => "submit button pill", :id => 'save'
-        = link_to "Test Connection", edit_provider_path(@provider, :test_provider => true), :class => 'button pill'
+        = f.submit "Test Connection", :name => :test_provider, :class => 'button pill'
         = link_to 'Delete Provider', provider_path(@provider), :method => :delete, :confirm => t('providers.confirm_delete'), :class => "button pill danger", :id => "delete"
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index a443ef2..8f63cf5 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -794,6 +794,7 @@ en:
       warning:
         delete_failed: Unable to Delete Image
         not_found: Image not found
+        push_invalid_accounts: "Could not push to the following providers:"
       error:
         invalid_url: Could not load the provided URL
         no_file: You must specify the template XML file
-- 
1.7.6.5




More information about the aeolus-devel mailing list