[PATCH conductor] RM 3481 - delete provider account via api

Richard Su rwsu at redhat.com
Fri Jul 20 01:39:29 UTC 2012


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

Raises ProviderAccountNotFound exception and 404 when provider account
is not found.
---
 .../controllers/provider_accounts_controller.rb    |   26 ++++++++++++++----
 src/app/views/provider_accounts/destroy.xml.haml   |    3 ++
 src/config/locales/en.yml                          |    1 +
 src/config/routes.rb                               |    2 +-
 .../provider_accounts_controller_spec.rb           |   28 ++++++++++++++++++++
 5 files changed, 53 insertions(+), 7 deletions(-)
 create mode 100644 src/app/views/provider_accounts/destroy.xml.haml

diff --git a/src/app/controllers/provider_accounts_controller.rb b/src/app/controllers/provider_accounts_controller.rb
index ce0d2ab..9db4e39 100644
--- a/src/app/controllers/provider_accounts_controller.rb
+++ b/src/app/controllers/provider_accounts_controller.rb
@@ -144,13 +144,27 @@ class ProviderAccountsController < ApplicationController
   def destroy
     @provider_account = ProviderAccount.find(params[:id])
     require_privilege(Privilege::MODIFY, @provider_account)
-    @provider = Provider.find(params[:provider_id])
-    if @provider_account.destroy
-      flash[:notice] = t"provider_accounts.flash.notice.deleted"
-    else
-      flash[:error] = t"provider_accounts.flash.error.not_deleted"
+    is_deleted = @provider_account && @provider_account.destroy
+
+    respond_to do |format|
+      format.html {
+        if is_deleted
+          flash[:notice] = t"provider_accounts.flash.notice.deleted"
+        else
+          flash[:error] = t"provider_accounts.flash.error.not_deleted"
+        end
+
+        @provider = Provider.find(params[:provider_id])
+        redirect_to edit_provider_path(@provider, :details_tab => 'accounts')
+      }
+      format.xml {
+        if is_deleted
+          render 'destroy', :locals => { :provider_account_id => @provider_account.id }
+        else
+          raise(Aeolus::Conductor::API::ProviderAccountNotFound.new(404, t("api.error_messages.provider_account_not_found_for_id", :id => params[:id])))
+        end
+      }
     end
-    redirect_to edit_provider_path(@provider, :details_tab => 'accounts')
   end
 
   def multi_destroy
diff --git a/src/app/views/provider_accounts/destroy.xml.haml b/src/app/views/provider_accounts/destroy.xml.haml
new file mode 100644
index 0000000..1f62636
--- /dev/null
+++ b/src/app/views/provider_accounts/destroy.xml.haml
@@ -0,0 +1,3 @@
+!!! XML
+%provider_account{:id => provider_account_id}
+  %status="DELETED"
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index 9295373..ed03450 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -1437,6 +1437,7 @@ en:
       no_matching_target_image: Could not find any matching Target Images
       no_provider_account_given: No provider account given
       provider_account_not_found: Could not find provider account for name %{name}
+      provider_account_not_found_for_id: Could not find provider account for id %{id}
       provider_image_not_found: Could not find ProviderImage %{providerimage}
       provider_image_status_not_found: Could not find status for ProviderImage %{providerimage}
       push_error: "Could not push TargetImage %{targetimage} to %{account} and error %{error}"
diff --git a/src/config/routes.rb b/src/config/routes.rb
index e7f26c5..d03009f 100644
--- a/src/config/routes.rb
+++ b/src/config/routes.rb
@@ -283,7 +283,7 @@ Conductor::Application.routes.draw do
     resources :providers, :only => [:index, :show, :create, :update, :destroy] do
       resources :provider_accounts, :only => [:index]
     end
-    resources :provider_accounts, :only => [:index, :show]
+    resources :provider_accounts, :only => [:index, :show, :destroy]
     resources :provider_types, :only => [:index, :show]
   end
 
diff --git a/src/spec/controllers/provider_accounts_controller_spec.rb b/src/spec/controllers/provider_accounts_controller_spec.rb
index a88bd25..3ed4250 100644
--- a/src/spec/controllers/provider_accounts_controller_spec.rb
+++ b/src/spec/controllers/provider_accounts_controller_spec.rb
@@ -242,6 +242,34 @@ describe ProviderAccountsController do
 
           end
         end # #index
+
+        describe "#destroy" do
+          before(:each) do
+            @provider_account = FactoryGirl.create(:mock_provider_account)
+          end
+
+          it "when requested provider account exists" do
+            ProviderAccount.stub(:find).and_return(@provider_account)
+            get :destroy, :id => @provider_account.id
+            response.status.should be_eql(200)
+            response.should have_content_type("application/xml")
+            response.body.should be_xml
+            subject = Nokogiri::XML(response.body)
+            xml_provider_account = subject.xpath("//provider_account[@id=\"#{@provider_account.id}\"]")
+            xml_provider_account.xpath('@id').text.strip.should == "#{@provider_account.id}"
+            xml_provider_account.xpath('status').text.strip.should == "DELETED"
+          end
+
+          it "when requested provider account doesn't exists" do
+            ProviderAccount.stub(:find).and_return(nil)
+            get :destroy, :id => "id_that_does_not_exist"
+            response.status.should be_eql(404)
+            response.should have_content_type("application/xml")
+            response.body.should be_xml
+            subject = Nokogiri::XML(response.body)
+            subject.xpath('//error/code').text.strip.should == "ProviderAccountNotFound"
+          end
+        end #destroy
       end
 
     end
-- 
1.7.7.6




More information about the aeolus-devel mailing list