[PATCH conductor 3/3] Adds XML format for Provider Accounts API - create

pblaho at redhat.com pblaho at redhat.com
Mon Jul 30 13:56:39 UTC 2012


From: Petr Blaho <pblaho at redhat.com>

https://www.aeolusproject.org/redmine/issues/3532

Adds block for XML format.
Adds handling of credentials param
Adds route to create action
---
 .../controllers/provider_accounts_controller.rb    |   42 +++++++++++++++----
 src/config/routes.rb                               |    2 +-
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/src/app/controllers/provider_accounts_controller.rb b/src/app/controllers/provider_accounts_controller.rb
index 7620e3d..61ca3e4 100644
--- a/src/app/controllers/provider_accounts_controller.rb
+++ b/src/app/controllers/provider_accounts_controller.rb
@@ -87,8 +87,18 @@ class ProviderAccountsController < ApplicationController
     require_privilege(Privilege::CREATE, ProviderAccount, @provider)
     params[:provider_account][:provider_id] = @provider.id
     @providers = Provider.all
+
+    # handles params from credentials and credentials_hash both the same way
+    # credentials get precedence
+    credentials_hash = if params[:provider_account][:credentials]
+                         params[:provider_account].delete( :credentials )
+                       elsif params[:provider_account][:credentials_hash]
+                         params[:provider_account].delete( :credentials_hash )
+                       else
+                         nil
+                       end
     @provider_account = ProviderAccount.new(params[:provider_account])
-    @provider_account.credentials_hash = params[:provider_account][:credentials_hash]
+    @provider_account.credentials_hash = credentials_hash
     @provider_account.quota = @quota = Quota.new
     limit = params[:quota][:maximum_running_instances] if params[:quota]
     @provider_account.quota.set_maximum_running_instances(limit)
@@ -96,17 +106,31 @@ class ProviderAccountsController < ApplicationController
     begin
       if @provider_account.save
         @provider_account.assign_owner_roles(current_user)
-        flash[:notice] = t('provider_accounts.flash.notice.account_added', :list => @provider_account.name, :count => 1)
-        redirect_to edit_provider_path(@provider, :details_tab => 'accounts')
+        respond_to do |format|
+          format.html do
+            flash[:notice] = t('provider_accounts.flash.notice.account_added', :list => @provider_account.name, :count => 1)
+            redirect_to edit_provider_path(@provider, :details_tab => 'accounts')
+          end
+          format.xml { render :partial => 'detail', :locals => { :provider_account => @provider_account, :with_credentials => true } }
+      end
       else
-        flash[:error] = t"provider_accounts.flash.error.not_added"
-        render :action => 'new' and return
+        respond_to do |format|
+          format.html do
+            flash[:error] = t"provider_accounts.flash.error.not_added"
+            render :action => 'new' and return
+          end
+          format.xml { render :template => 'api/validation_error', :locals => { :errors => @provider_account.errors }, :status => :bad_request }
+        end
       end
     rescue Exception => e
-      error = humanize_error(e.message, :context => :deltacloud)
-      flash[:error] = "#{t('provider_accounts.flash.error.account_not_added', :list => @provider_account.name,
-        :count => 1)}: #{error}"
-      render :action => 'new' and return
+      respond_to do |format|
+        format.html do
+          error = humanize_error(e.message, :context => :deltacloud)
+          flash[:error] = "#{t('provider_accounts.flash.error.account_not_added', :list => @provider_account.name,
+            :count => 1)}: #{error}"
+            render :action => 'new' and return
+        end
+      end
     end
   end
 
diff --git a/src/config/routes.rb b/src/config/routes.rb
index c13d827..d506e4c 100644
--- a/src/config/routes.rb
+++ b/src/config/routes.rb
@@ -282,7 +282,7 @@ Conductor::Application.routes.draw do
 
   scope "/api", :as => 'api' do
     resources :providers, :only => [:index, :show, :create, :update, :destroy] do
-      resources :provider_accounts, :only => [:index]
+      resources :provider_accounts, :only => [:index, :create]
     end
     resources :provider_accounts, :only => [:index, :show, :destroy]
     resources :provider_types, :only => [:index, :show]
-- 
1.7.7.6




More information about the aeolus-devel mailing list