From b65ce12fa56f7d21962bb643cfb7ee719dde0f82 Mon Sep 17 00:00:00 2001 From: Richard Su Date: Tue, 11 Dec 2012 16:15:24 -0800 Subject: [PATCH 1/3] Initial frontend realms api index and show implementation --- .../controllers/frontend_realms_controller.rb | 2 + .../views/frontend_realms/_detail.xml.haml | 10 +++ src/app/views/frontend_realms/_list.xml.haml | 4 + src/app/views/frontend_realms/show.xml.haml | 2 + src/config/routes.rb | 1 + src/spec/requests/api/frontend_realms_spec.rb | 83 +++++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 src/app/views/frontend_realms/_detail.xml.haml create mode 100644 src/app/views/frontend_realms/_list.xml.haml create mode 100644 src/app/views/frontend_realms/show.xml.haml create mode 100644 src/spec/requests/api/frontend_realms_spec.rb diff --git a/src/app/controllers/frontend_realms_controller.rb b/src/app/controllers/frontend_realms_controller.rb index 173ef8ad..392e9275 100644 --- a/src/app/controllers/frontend_realms_controller.rb +++ b/src/app/controllers/frontend_realms_controller.rb @@ -26,6 +26,7 @@ def index respond_to do |format| format.html format.js { render :partial => 'list' } + format.xml { render :partial => 'list.xml' } end end @@ -126,6 +127,7 @@ def show render :partial => @details_tab end format.json { render :json => @realm } + format.xml { render :show, :locals => { :frontend_realm => @realm } } end end diff --git a/src/app/views/frontend_realms/_detail.xml.haml b/src/app/views/frontend_realms/_detail.xml.haml new file mode 100644 index 00000000..2af14f5d --- /dev/null +++ b/src/app/views/frontend_realms/_detail.xml.haml @@ -0,0 +1,10 @@ +!!! XML +%frontend_realm{:id => frontend_realm.id, :href => api_frontend_realm_url(frontend_realm)} + %name= frontend_realm.name + %description= frontend_realm.description + %provider_realms + - frontend_realm.backend_realms.each do |prealm| + %provider_realm{:id => prealm.id, :href => api_provider_realm_url(prealm)} + %providers + - frontend_realm.backend_providers.each do |provider| + %provider{:id => provider.id, :href=> api_provider_url(provider)} diff --git a/src/app/views/frontend_realms/_list.xml.haml b/src/app/views/frontend_realms/_list.xml.haml new file mode 100644 index 00000000..57d38d5b --- /dev/null +++ b/src/app/views/frontend_realms/_list.xml.haml @@ -0,0 +1,4 @@ +!!! XML +%frontend_realms + - @realms.each do |frealm| + %frontend_realm{:id => frealm.id, :href => api_frontend_realm_url(frealm)} diff --git a/src/app/views/frontend_realms/show.xml.haml b/src/app/views/frontend_realms/show.xml.haml new file mode 100644 index 00000000..d49a41dd --- /dev/null +++ b/src/app/views/frontend_realms/show.xml.haml @@ -0,0 +1,2 @@ +!!! XML +=render 'detail', :frontend_realm => frontend_realm \ No newline at end of file diff --git a/src/config/routes.rb b/src/config/routes.rb index a30bed90..15aa8d51 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -325,6 +325,7 @@ resources :deployables, :only => [:index, :show, :destroy] resources :provider_realms, :only => [:index, :show] resources :instances, :only => [:index, :show] + resources :frontend_realms, :only => [:index, :show] 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/requests/api/frontend_realms_spec.rb b/src/spec/requests/api/frontend_realms_spec.rb new file mode 100644 index 00000000..f85ee7c9 --- /dev/null +++ b/src/spec/requests/api/frontend_realms_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe "FrontendRealms" do + let(:headers) { { + 'HTTP_ACCEPT' => 'application/xml', + 'CONTENT_TYPE' => 'application/xml' + } } + + context "API" do + before(:each) do + user = FactoryGirl.create(:admin_permission).user + login_as(user) + @provider = FactoryGirl.create(:mock_provider) + @provider_account = FactoryGirl.create(:mock_provider_account) + @frontend_realm = FactoryGirl.create(:frontend_realm) + # add provider realm + @provider.provider_realms.each do |prealm| + realm_backend_target = FactoryGirl.create(:realm_backend_target, :frontend_realm => @frontend_realm, :provider_realm_or_provider => prealm) + @frontend_realm.realm_backend_targets << realm_backend_target + #@frontend_realm.backend_realms << prealm + end + # add provider + realm_backend_target = FactoryGirl.create(:realm_backend_target, :frontend_realm => @frontend_realm, :provider_realm_or_provider => @provider) + @frontend_realm.realm_backend_targets << realm_backend_target + end + + describe "#index" do + + it "get index" do + get "/api/frontend_realms", nil, headers + + response.should be_success + response.should have_content_type("application/xml") + response.body.should be_xml + xml = Nokogiri::XML(response.body) + + xml.xpath("/frontend_realms/frontend_realm").size.should > 0 + xml.xpath("/frontend_realms/frontend_realm").size.should eq FrontendRealm.all.size + FrontendRealm.all.each do |frontend_realm| + xml.xpath("/frontend_realms/frontend_realm[@id='#{frontend_realm.id}']/@href"). + text.should == api_frontend_realm_url(frontend_realm.id) + end + end + end + + describe "#show" do + + it "show frontend realm" do + @frontend_realm.backend_realms.size.should > 0 + @frontend_realm.backend_providers.size.should > 0 + + get "/api/frontend_realms/#{@frontend_realm.id}", nil, headers + response.should be_success + response.should have_content_type("application/xml") + response.body.should be_xml + xml = Nokogiri::XML(response.body) + + xml.xpath("/frontend_realm/@id").text.should == @frontend_realm.id.to_s + xml.xpath("/frontend_realm/@href").text.should == api_frontend_realm_url(@frontend_realm) + xml.xpath("/frontend_realm/name").text.should == @frontend_realm.name + xml.xpath("/frontend_realm/description").text.should == @frontend_realm.description.to_s + + @frontend_realm.backend_realms.each do |prealm| + xml.xpath("/frontend_realm/provider_realms/provider_realm[@id='#{prealm.id}']/@href"). + text.should == api_provider_realm_url(prealm) + end + + @frontend_realm.backend_providers.each do |provider| + xml.xpath("/frontend_realm/providers/provider[@id='#{provider.id}']/@href"). + text.should == api_provider_url(provider) + end + end + + it "show nonexistent frontend realm" do + get "/api/frontend_realms/-1", nil, headers + response.status.should == 404 + response.should have_content_type("application/xml") + response.body.should be_xml + end + + end + end +end From babee2611dbba0734864219198d8caaac587fd37 Mon Sep 17 00:00:00 2001 From: Richard Su Date: Thu, 13 Dec 2012 19:52:14 -0800 Subject: [PATCH 2/3] Frontend realms full representation shown in top level collections --- .../controllers/frontend_realms_controller.rb | 3 +- src/app/helpers/frontend_realms_helper.rb | 25 ++++++++++++++ .../views/frontend_realms/_detail.xml.haml | 17 +++++----- src/app/views/frontend_realms/_list.xml.haml | 6 ++-- src/app/views/frontend_realms/index.xml.haml | 3 ++ src/app/views/frontend_realms/show.xml.haml | 2 +- .../views/provider_realms/_detail.xml.haml | 7 ++-- src/config/routes.rb | 4 ++- src/spec/requests/api/frontend_realms_spec.rb | 34 +++++++++++-------- src/spec/requests/api/provider_realms_spec.rb | 6 ++++ 10 files changed, 76 insertions(+), 31 deletions(-) create mode 100644 src/app/helpers/frontend_realms_helper.rb create mode 100644 src/app/views/frontend_realms/index.xml.haml diff --git a/src/app/controllers/frontend_realms_controller.rb b/src/app/controllers/frontend_realms_controller.rb index 392e9275..6d8938e2 100644 --- a/src/app/controllers/frontend_realms_controller.rb +++ b/src/app/controllers/frontend_realms_controller.rb @@ -23,10 +23,11 @@ def index clear_breadcrumbs save_breadcrumb(frontend_realms_path) set_admin_content_tabs 'frontend_realms' + load_backend_realms respond_to do |format| format.html format.js { render :partial => 'list' } - format.xml { render :partial => 'list.xml' } + format.xml { @provider_realm = @backend_realms.first if params[:provider_realm_id] } end end diff --git a/src/app/helpers/frontend_realms_helper.rb b/src/app/helpers/frontend_realms_helper.rb new file mode 100644 index 00000000..c0a065ae --- /dev/null +++ b/src/app/helpers/frontend_realms_helper.rb @@ -0,0 +1,25 @@ +# +# Copyright 2012 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module FrontendRealmsHelper + def api_frontend_realms_collection_href(provider_realm = nil) + if provider_realm + api_provider_realm_frontend_realms_url(provider_realm) + else + api_frontend_realms_url + end + end +end diff --git a/src/app/views/frontend_realms/_detail.xml.haml b/src/app/views/frontend_realms/_detail.xml.haml index 2af14f5d..94cc76b2 100644 --- a/src/app/views/frontend_realms/_detail.xml.haml +++ b/src/app/views/frontend_realms/_detail.xml.haml @@ -1,10 +1,11 @@ !!! XML %frontend_realm{:id => frontend_realm.id, :href => api_frontend_realm_url(frontend_realm)} - %name= frontend_realm.name - %description= frontend_realm.description - %provider_realms - - frontend_realm.backend_realms.each do |prealm| - %provider_realm{:id => prealm.id, :href => api_provider_realm_url(prealm)} - %providers - - frontend_realm.backend_providers.each do |provider| - %provider{:id => provider.id, :href=> api_provider_url(provider)} + - unless shallow + %name= frontend_realm.name + %description= frontend_realm.description + %provider_realms + - frontend_realm.backend_realms.each do |prealm| + %provider_realm{:id => prealm.id, :href => api_provider_realm_url(prealm)} + %providers + - frontend_realm.backend_providers.each do |provider| + %provider{:id => provider.id, :href=> api_provider_url(provider)} \ No newline at end of file diff --git a/src/app/views/frontend_realms/_list.xml.haml b/src/app/views/frontend_realms/_list.xml.haml index 57d38d5b..89700ee3 100644 --- a/src/app/views/frontend_realms/_list.xml.haml +++ b/src/app/views/frontend_realms/_list.xml.haml @@ -1,4 +1,4 @@ !!! XML -%frontend_realms - - @realms.each do |frealm| - %frontend_realm{:id => frealm.id, :href => api_frontend_realm_url(frealm)} +%frontend_realms{ :href => frontend_realms_href } + - realms.each do |frealm| + =render '/frontend_realms/detail', :frontend_realm => frealm, :shallow => shallow \ No newline at end of file diff --git a/src/app/views/frontend_realms/index.xml.haml b/src/app/views/frontend_realms/index.xml.haml new file mode 100644 index 00000000..4d4af9bf --- /dev/null +++ b/src/app/views/frontend_realms/index.xml.haml @@ -0,0 +1,3 @@ +!!! XML +=render 'list', :realms => @realms, :shallow => false, + :frontend_realms_href => api_frontend_realms_collection_href(@provider_realm) \ No newline at end of file diff --git a/src/app/views/frontend_realms/show.xml.haml b/src/app/views/frontend_realms/show.xml.haml index d49a41dd..9dcd3801 100644 --- a/src/app/views/frontend_realms/show.xml.haml +++ b/src/app/views/frontend_realms/show.xml.haml @@ -1,2 +1,2 @@ !!! XML -=render 'detail', :frontend_realm => frontend_realm \ No newline at end of file +=render 'detail', :frontend_realm => frontend_realm, :shallow => false \ No newline at end of file diff --git a/src/app/views/provider_realms/_detail.xml.haml b/src/app/views/provider_realms/_detail.xml.haml index d10a769e..a2eda329 100644 --- a/src/app/views/provider_realms/_detail.xml.haml +++ b/src/app/views/provider_realms/_detail.xml.haml @@ -5,7 +5,8 @@ %provider_accounts - @provider_accounts.each do |paccount| %provider_account{:id => paccount.id, :href => api_provider_account_url(paccount)} - %frontend_realms - - @frontend_realms.each do |frontend_realm| - %frontend_realm{:id => frontend_realm.id} + =render '/frontend_realms/list', + :realms => provider_realm.frontend_realms, + :shallow => true, + :frontend_realms_href => api_provider_realm_frontend_realms_url(provider_realm) %provider{:id => provider_realm.provider.id, :href => api_provider_url(provider_realm.provider)} \ No newline at end of file diff --git a/src/config/routes.rb b/src/config/routes.rb index 15aa8d51..f7608caa 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -323,7 +323,9 @@ resources :instances, :only => [:index] end resources :deployables, :only => [:index, :show, :destroy] - resources :provider_realms, :only => [:index, :show] + resources :provider_realms, :only => [:index, :show] do + resources :frontend_realms, :only => [:index] + end resources :instances, :only => [:index, :show] resources :frontend_realms, :only => [:index, :show] end diff --git a/src/spec/requests/api/frontend_realms_spec.rb b/src/spec/requests/api/frontend_realms_spec.rb index f85ee7c9..0d29efc2 100644 --- a/src/spec/requests/api/frontend_realms_spec.rb +++ b/src/spec/requests/api/frontend_realms_spec.rb @@ -24,6 +24,23 @@ @frontend_realm.realm_backend_targets << realm_backend_target end + def check_frontend_realm_xml(xml_frontend_realm, frontend_realm) + xml_frontend_realm.xpath("@id").text.should == frontend_realm.id.to_s + xml_frontend_realm.xpath("@href").text.should == api_frontend_realm_url(frontend_realm) + xml_frontend_realm.xpath("name").text.should == frontend_realm.name + xml_frontend_realm.xpath("description").text.should == frontend_realm.description.to_s + + frontend_realm.backend_realms.each do |prealm| + xml_frontend_realm.xpath("provider_realms/provider_realm[@id='#{prealm.id}']/@href"). + text.should == api_provider_realm_url(prealm) + end + + frontend_realm.backend_providers.each do |provider| + xml_frontend_realm.xpath("providers/provider[@id='#{provider.id}']/@href"). + text.should == api_provider_url(provider) + end + end + describe "#index" do it "get index" do @@ -39,6 +56,8 @@ FrontendRealm.all.each do |frontend_realm| xml.xpath("/frontend_realms/frontend_realm[@id='#{frontend_realm.id}']/@href"). text.should == api_frontend_realm_url(frontend_realm.id) + frontend_realm_xml = xml.xpath("/frontend_realms/frontend_realm[@id='#{frontend_realm.id}']") + check_frontend_realm_xml(frontend_realm_xml, frontend_realm) end end end @@ -55,20 +74,7 @@ response.body.should be_xml xml = Nokogiri::XML(response.body) - xml.xpath("/frontend_realm/@id").text.should == @frontend_realm.id.to_s - xml.xpath("/frontend_realm/@href").text.should == api_frontend_realm_url(@frontend_realm) - xml.xpath("/frontend_realm/name").text.should == @frontend_realm.name - xml.xpath("/frontend_realm/description").text.should == @frontend_realm.description.to_s - - @frontend_realm.backend_realms.each do |prealm| - xml.xpath("/frontend_realm/provider_realms/provider_realm[@id='#{prealm.id}']/@href"). - text.should == api_provider_realm_url(prealm) - end - - @frontend_realm.backend_providers.each do |provider| - xml.xpath("/frontend_realm/providers/provider[@id='#{provider.id}']/@href"). - text.should == api_provider_url(provider) - end + check_frontend_realm_xml(xml.xpath("/frontend_realm"), @frontend_realm) end it "show nonexistent frontend realm" do diff --git a/src/spec/requests/api/provider_realms_spec.rb b/src/spec/requests/api/provider_realms_spec.rb index 90eb4d2c..78a2b7ae 100644 --- a/src/spec/requests/api/provider_realms_spec.rb +++ b/src/spec/requests/api/provider_realms_spec.rb @@ -55,6 +55,12 @@ text.should == api_provider_url(@provider) xml.xpath("/provider_realm/provider_accounts/provider_account[@id='#{@provider_account.id}']/@href"). text.should == api_provider_account_url(@provider_account) + + xml.xpath("/provider_realm/frontend_realms/@href").text.should == api_provider_realm_frontend_realms_url(provider_realm) + provider_realm.frontend_realms.size.should > 0 + provider_realm.frontend_realms.each do |frealm| + xml.xpath("/provider_realm/frontend_realms/frontend_realm[@id='#{frealm.id}']/@href").text.should == api_frontend_realm_url(frealm) + end end it "show nonexistent provider realm" do From 6a722fbcadfe72ff47923e5825954330fe22bbd0 Mon Sep 17 00:00:00 2001 From: Richard Su Date: Thu, 13 Dec 2012 18:55:37 -0800 Subject: [PATCH 3/3] Add frontend realms to api entrypoint --- src/app/views/api/entrypoint/index.xml.haml | 1 + src/spec/controllers/api/entrypoint_controller_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/views/api/entrypoint/index.xml.haml b/src/app/views/api/entrypoint/index.xml.haml index 3e48c4c6..32da7ec5 100644 --- a/src/app/views/api/entrypoint/index.xml.haml +++ b/src/app/views/api/entrypoint/index.xml.haml @@ -4,6 +4,7 @@ %catalogs{:href => api_catalogs_url} %deployables{:href => api_deployables_url} %deployments{:href => api_deployments_url} + %frontend_realms{:href => api_frontend_realms_url} %images{:href => api_images_url} %instances{:href => api_instances_url} %pools{:href => api_pools_url} diff --git a/src/spec/controllers/api/entrypoint_controller_spec.rb b/src/spec/controllers/api/entrypoint_controller_spec.rb index 44a5ebf7..6507ba92 100644 --- a/src/spec/controllers/api/entrypoint_controller_spec.rb +++ b/src/spec/controllers/api/entrypoint_controller_spec.rb @@ -42,8 +42,9 @@ api = resp['api'] api['builds']['href'].should == api_builds_url api['catalogs']['href'].should == api_catalogs_url - api['deployments']['href'].should == api_deployments_url api['deployables']['href'].should == api_deployables_url + api['deployments']['href'].should == api_deployments_url + api['frontend_realms']['href'].should == api_frontend_realms_url api['images']['href'].should == api_images_url api['instances']['href'].should == api_instances_url api['pools']['href'].should == api_pools_url