Skip to content

Commit

Permalink
Merge pull request #5553 from bbuckingham/issue-12315
Browse files Browse the repository at this point in the history
fixes #12315 - move rhsm routes to a separate resource list
  • Loading branch information
bbuckingham committed Nov 3, 2015
2 parents 0dd2396 + a54f1f8 commit 53cace6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
40 changes: 33 additions & 7 deletions app/controllers/katello/api/v2/root_controller.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes/api/rhsm.rb
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/controllers/api/v2/root_controller_spec.rb
Expand Up @@ -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

0 comments on commit 53cace6

Please sign in to comment.