diff --git a/app/controllers/katello/api/v2/root_controller.rb b/app/controllers/katello/api/v2/root_controller.rb index 6e8ecf7e3a6..b4ac3aa2de5 100644 --- a/app/controllers/katello/api/v2/root_controller.rb +++ b/app/controllers/katello/api/v2/root_controller.rb @@ -22,14 +22,40 @@ def resource_list api_root_routes.collect! { |r| { :rel => r["/katello/api/".size..-2], :href => r } } - # provide some fake paths that does not exist (but rhsm is checking it's existance) - api_root_routes << { :href => '/katello/api/packages/', :rel => 'packages' } - api_root_routes << { :href => '/katello/api/status/', :rel => 'status' } - api_root_routes << { :href => '/katello/api/guestids', :rel => 'guestids'} - api_root_routes << { :href => '/katello/api/content_overrides', :rel => 'content_overrides'} - api_root_routes << { :href => '/katello/api/available_releases', :rel => 'available_releases'} - respond_for_index :collection => api_root_routes end + + def rhsm_resource_list + # The RHSM resource list is required to interact with RHSM on the client. + # When requested, it will return a list of the resources (href & rel) defined by katello + # for the /rhsm namespace. The rel values are used by RHSM to determine if the server + # supports a particular resource (e.g. environments, guestids, organizations..etc) + + all_routes = Engine.routes.routes.collect { |r| r.path.spec.to_s } + + api_routes = all_routes.select do |path| + # obtain only the rhsm routes + path =~ %r{^/rhsm/.+$} + end + + api_routes = api_routes.collect do |path| + # drop the trailing :format + path = path.sub("(.:format)", "") + + # drop the trailing ids + path_elements = path.split("/") + if path_elements.last.start_with?(':') && path_elements.last.end_with?('id') + path_elements.delete_at(-1) + path_elements.join('/') + else + path + end + end + + api_routes.uniq! + api_routes.collect! { |r| { :rel => r.split('/').last, :href => r } } + + respond_for_index :collection => api_routes, :template => 'resource_list' + end end end diff --git a/config/routes/api/rhsm.rb b/config/routes/api/rhsm.rb index 31d11dc2151..8106f5e4215 100644 --- a/config/routes/api/rhsm.rb +++ b/config/routes/api/rhsm.rb @@ -7,7 +7,7 @@ class ActionDispatch::Routing::Mapper Katello::Engine.routes.draw do scope :api, :module => :api do - match '/rhsm' => 'v2/root#resource_list', :via => :get + match '/rhsm' => 'v2/root#rhsm_resource_list', :via => :get scope :path => :rhsm, :module => :rhsm, :as => :rhsm do # subscription-manager support diff --git a/test/controllers/api/v2/root_controller_spec.rb b/test/controllers/api/v2/root_controller_spec.rb index 67184b08a0a..3985b1be435 100644 --- a/test/controllers/api/v2/root_controller_spec.rb +++ b/test/controllers/api/v2/root_controller_spec.rb @@ -11,5 +11,30 @@ def test_resource_list assert_response :success end + + def test_rhsm_resource_list + results = JSON.parse(get(:rhsm_resource_list).body) + + rhsm_results = results.select { |r| r['href'].start_with?("/rhsm") } + katello_results = results.select { |r| r['href'].start_with?("/katello") } + + # results are only for rhsm resources + refute_empty rhsm_results + assert_empty katello_results + + # href & rel have values + assert_empty results.select { |r| r['href'].blank? } + assert_empty results.select { |r| r['rel'].blank? } + + # none hrefs end with an id + assert_empty rhsm_results.select { |r| r['href'].end_with?(:id) || r['href'].end_with?(:guest_id) } + + # check for a few of the expected routes + refute_empty rhsm_results.select { |r| r['href'] == "/rhsm/consumers" && r['rel'] == 'consumers' } + refute_empty rhsm_results.select { |r| r['href'] == "/rhsm/owners/:organization_id/environments" && r['rel'] == 'environments' } + refute_empty rhsm_results.select { |r| r['href'] == "/rhsm/owners/:organization_id/pools" && r['rel'] == 'pools' } + + assert_response :success + end end end