[PATCH conductor] pools api - create, show, and update endpoints - under RM 3542
Crag Wolfe
cwolfe at redhat.com
Tue Aug 14 21:29:53 UTC 2012
ACK. pushed. I tested manually with examples at
https://www.aeolusproject.org/redmine/projects/aeolus/wiki/Pools and the
checked in rspec tests passed.
On 08/14/2012 09:08 AM, Richard Su wrote:
> https://www.aeolusproject.org/redmine/issues/3542
>
> Updated controller to also render xml. Created specs to convert xml
> into hash params and then to post the hash params to the controller
> and verify xml responses.
> ---
> src/app/controllers/pools_controller.rb | 15 +++
> src/app/views/pools/_detail.xml.haml | 6 +
> src/app/views/pools/show.xml.haml | 2 +
> src/config/routes.rb | 1 +
> src/spec/controllers/pools_controller_spec.rb | 159 +++++++++++++++++++++++++
> 5 files changed, 183 insertions(+), 0 deletions(-)
> create mode 100644 src/app/views/pools/_detail.xml.haml
> create mode 100644 src/app/views/pools/show.xml.haml
>
> diff --git a/src/app/controllers/pools_controller.rb b/src/app/controllers/pools_controller.rb
> index 0668217..931d25d 100644
> --- a/src/app/controllers/pools_controller.rb
> +++ b/src/app/controllers/pools_controller.rb
> @@ -166,6 +166,7 @@ class PoolsController< ApplicationController
> map{ |deployment| view_context.deployment_for_mustache(deployment) }
> render :json => @pool.as_json.merge({:deployments => deployments})
> end
> + format.xml { render :show, :locals => { :pool => @pool } }
> end
> end
>
> @@ -186,6 +187,11 @@ class PoolsController< ApplicationController
>
> def create
> @title = t('pools.create_new_pool')
> + if !params.has_key? :quota and !params[:pool][:quota].nil?
> + params[:quota] = params[:pool][:quota]
> + params[:pool].delete(:quota)
> + end
> +
> @pool = Pool.new(params[:pool])
> require_privilege(Privilege::CREATE, Pool, @pool.pool_family)
> @pool.quota = @quota = Quota.new
> @@ -197,11 +203,14 @@ class PoolsController< ApplicationController
> flash[:notice] = t "pools.flash.notice.added"
> format.html { redirect_to pools_path }
> format.json { render :json => @pool, :status => :created }
> + format.xml {render :show, :locals => { :pool => @pool }
> + }
> else
> flash.now[:warning] = t "pools.flash.warning.creation_failed"
> format.html { render :new }
> format.js { render :partial => 'new' }
> format.json { render :json => @pool.errors, :status => :unprocessable_entity }
> + format.xml { render :template => 'api/validation_error', :locals => { :errors => @pool.errors }, :status => :bad_request}
> end
> end
> end
> @@ -219,6 +228,10 @@ class PoolsController< ApplicationController
> end
>
> def update
> + if !params.has_key? :quota and !params[:pool][:quota].nil?
> + params[:quota] = params[:pool][:quota]
> + params[:pool].delete(:quota)
> + end
> @pool = Pool.find(params[:id])
> require_privilege(Privilege::MODIFY, @pool)
> set_quota
> @@ -230,11 +243,13 @@ class PoolsController< ApplicationController
> format.html { redirect_to :action => 'show', :id => @pool.id }
> format.js { render :partial => 'show', :id => @pool.id }
> format.json { render :json => @pool }
> + format.xml {render :show, :locals => { :pool => @pool } }
> else
> flash[:error] = t "pools.flash.error.not_updated"
> format.html { render :action => :edit }
> format.js { render :partial => 'edit', :id => @pool.id }
> format.json { render :json => @pool.errors, :status => :unprocessable_entity }
> + format.xml { render :template => 'api/validation_error', :locals => { :errors => @pool.errors }, :status => :bad_request}
> end
> end
> end
> diff --git a/src/app/views/pools/_detail.xml.haml b/src/app/views/pools/_detail.xml.haml
> new file mode 100644
> index 0000000..9214ec0
> --- /dev/null
> +++ b/src/app/views/pools/_detail.xml.haml
> @@ -0,0 +1,6 @@
> +!!! XML
> +%pool{:id => pool.id, :url => api_pool_url(pool)}
> + %name= pool.name
> + %pool_family{:id => pool.pool_family.id, :url=> pool_family_url(pool.pool_family)}= pool.pool_family.name
> + %quota= pool.quota.maximum_running_instances
> + %enabled= pool.enabled
> diff --git a/src/app/views/pools/show.xml.haml b/src/app/views/pools/show.xml.haml
> new file mode 100644
> index 0000000..297a082
> --- /dev/null
> +++ b/src/app/views/pools/show.xml.haml
> @@ -0,0 +1,2 @@
> +!!! XML
> +=render 'detail', :pool => pool
> \ No newline at end of file
> diff --git a/src/config/routes.rb b/src/config/routes.rb
> index 119d740..8373342 100644
> --- a/src/config/routes.rb
> +++ b/src/config/routes.rb
> @@ -287,6 +287,7 @@ Conductor::Application.routes.draw do
> resources :provider_accounts, :only => [:index, :show, :destroy]
> resources :provider_types, :only => [:index, :show]
> resources :hardware_profiles, :only => [:index, :show, :destroy, :create]
> + resources :pools, :only => [:index, :show, :create, :update]
> end
>
> #match 'matching_profiles', :to => '/hardware_profiles/matching_profiles/:hardware_profile_id/provider/:provider_id', :controller => 'hardware_profiles', :action => 'matching_profiles', :conditions => { :method => :get }, :as =>'matching_profiles'
> diff --git a/src/spec/controllers/pools_controller_spec.rb b/src/spec/controllers/pools_controller_spec.rb
> index ba68755..5afc66c 100644
> --- a/src/spec/controllers/pools_controller_spec.rb
> +++ b/src/spec/controllers/pools_controller_spec.rb
> @@ -99,6 +99,165 @@ describe PoolsController do
> end
> end
>
> + context "API" do
> + render_views
> +
> + before do
> + accept_xml
> + mock_warden(@admin)
> + @test_pool_name = "testpool1"
> + @pool_family = FactoryGirl.create :pool_family
> + end
> +
> + def assert_pool_api_success_response(name, familyName, familyId, quota, enabled)
> + response.should be_success
> + response.should have_content_type("application/xml")
> + response.body.should be_xml
> + xml = Nokogiri::XML(response.body)
> + xml.xpath("/pool/name").text.should == name
> + xml.xpath("/pool/pool_family").text.should == familyName
> + xml.xpath("/pool/pool_family/@id").text.should == "#{familyId}"
> + xml.xpath("/pool/quota").text.should == quota
> + xml.xpath("/pool/enabled").text.should == enabled
> + end
> +
> + describe "#create" do
> +
> + it "post with all expected params" do
> + xmldata = "
> +<pool>
> +<name>#{@test_pool_name}</name>
> +<pool_family_id>#{@pool_family.id}</pool_family_id>
> +<enabled>true</enabled>
> +<quota>
> +<maximum_running_instances>1001</maximum_running_instances>
> +</quota>
> +</pool>"
> + post :create, Hash.from_xml(xmldata)
> +
> + assert_pool_api_success_response(@test_pool_name,
> + @pool_family.name,
> + @pool_family.id,
> + "1001",
> + "true")
> + end
> +
> + it "post missing pool family parameter should result in error message" do
> + xmldata = "
> +<pool>
> +<name>#{@test_pool_name}</name>
> +<!--<pool_family_id>#{@pool_family.id}</pool_family_id>-->
> +<enabled>true</enabled>
> +<quota>
> +<maximum_running_instances>1001</maximum_running_instances>
> +</quota>
> +</pool>"
> + post :create, Hash.from_xml(xmldata)
> +
> + response.status.should be_eql(400) # Bad Request
> + response.should have_content_type("application/xml")
> + response.body.should be_xml
> + xml = Nokogiri::XML(response.body)
> + xml.xpath("/errors/error/message").text.should == "Pool family can't be blank"
> + end
> +
> + end
> +
> + describe "#show" do
> +
> + it "show an existing pool" do
> + @pool = FactoryGirl.create :pool
> +
> + get :show, :id => @pool.id
> +
> + assert_pool_api_success_response(@pool.name,
> + @pool.pool_family.name,
> + @pool.pool_family.id,
> + "#{@pool.quota.maximum_running_instances}",
> + "#{@pool.enabled}")
> + end
> +
> + it "show a missing pool" do
> + get :show, :id => -1
> +
> + response.status.should be_eql(404)
> + response.should have_content_type("application/xml")
> + response.body.should be_xml
> + xml = Nokogiri::XML(response.body)
> + xml.xpath("/error/message").text.should == "Couldn't find Pool with id=-1"
> + end
> +
> + end
> +
> + describe "#update" do
> +
> + it "update with all expected params" do
> + # we will receive unlimited quota if
> + # maximum_running_instances is not specified
> + #<quota></quota> denotes unlmited quota
> + xmldata = "
> +<pool>
> +<name>#{@test_pool_name}</name>
> +<pool_family_id>#{@pool_family.id}</pool_family_id>
> +<enabled>true</enabled>
> +</pool>"
> + post :create, Hash.from_xml(xmldata)
> +
> + assert_pool_api_success_response(@test_pool_name,
> + @pool_family.name,
> + @pool_family.id,
> + "", #unlimited
> + "true")
> +
> + xml = Nokogiri::XML(response.body)
> + pool_id = xml.xpath("/pool/@id").text
> +
> + xmldata = "
> +<pool>
> +<name>pool-updated</name>
> +<pool_family_id>#{@pool_family.id}</pool_family_id>
> +<enabled>false</enabled>
> +<quota>
> +<maximum_running_instances>1002</maximum_running_instances>
> +</quota>
> +</pool>"
> + put :update, :id => pool_id, :pool => Hash.from_xml(xmldata)["pool"]
> +
> + assert_pool_api_success_response("pool-updated",
> + @pool_family.name,
> + @pool_family.id,
> + "1002",
> + "false")
> + end
> +
> + it "update missing pool" do
> + xmldata = "<pool><name>missing-pool</name></pool>"
> + put :update, :id => -1, :pool => Hash.from_xml(xmldata)["pool"]
> +
> + response.status.should be_eql(404)
> + response.should have_content_type("application/xml")
> + response.body.should be_xml
> + xml = Nokogiri::XML(response.body)
> + xml.xpath("/error/message").text.should == "Couldn't find Pool with id=-1"
> + end
> +
> + it "update with blank name" do
> + @pool = FactoryGirl.create :pool
> + xmldata = "<pool><name></name></pool>"
> + put :update, :id => @pool.id, :pool => Hash.from_xml(xmldata)["pool"]
> +
> + response.status.should be_eql(400)
> + response.should have_content_type("application/xml")
> + response.body.should be_xml
> + xml = Nokogiri::XML(response.body)
> + xml.xpath("/errors/error/message").text.should == "Name can't be blank"
> + end
> +
> + end
> +
> + end
> +
> +
> context "JSON format responses for " do
> before do
> accept_json
More information about the aeolus-devel
mailing list