Skip to content

Commit 783ba1e

Browse files
committed
address comments
1 parent 3b30531 commit 783ba1e

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

app/controllers/api/v2/insights_advisor/insights_advisor_controller.rb

+10-14
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ module InsightsAdvisor
44
class InsightsAdvisorController < ::Api::V2::BaseController
55
include ::Api::Version2
66

7-
before_action :find_organization
8-
7+
api :GET, "insights_advisor/host_details", N_('Fetch Insights-related host details')
8+
param :host_uuids, Array, required: true, desc: N_('List of host UUIDs')
99
def host_details
10-
@hosts = ::Host::Managed.search_for(params[:search] || "", :order => params[:order]).where(:organization_id => @organization.id).includes(:insights)
11-
respond_to do |format|
12-
format.json { render 'api/v2/insights_advisor/host_details' }
10+
uuids = params.require(:host_uuids)
11+
@hosts = ::Host.joins(:insights).where(:insights => { :uuid => uuids })
12+
if @hosts.empty?
13+
render json: { error: 'No hosts found for the given UUIDs' }, status: :not_found
14+
else
15+
respond_to do |format|
16+
format.json { render 'api/v2/insights_advisor/host_details' }
17+
end
1318
end
1419
end
15-
16-
private
17-
18-
def find_organization
19-
@organization ||= Organization.find_by(label: params[:organization_label]) if params[:organization_label]
20-
@organization ||= Organization.find_by(label: params[:organization]) if params[:organization]
21-
@organization ||= Organization.find(params[:organization_id]) if params[:organization_id]
22-
raise ::Foreman::Exception.new(N_("Organization not found")) unless @organization
23-
end
2420
end
2521
end
2622
end

test/controllers/insights_cloud/api/insights_advisor_controller_test.rb

+8-16
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,19 @@ class InsightsAdvisorControllerTest < ActionController::TestCase
1212
@host3 = FactoryBot.create(:host, organization: @test_org)
1313
end
1414

15-
test 'shows all hosts with no search param' do
16-
get :host_details, params: { organization_id: @test_org.id }
17-
18-
assert_response :success
19-
assert_template 'api/v2/insights_advisor/host_details'
20-
assert_equal @test_org.hosts.count, assigns(:hosts).count
21-
end
22-
23-
test 'shows hosts with search param' do
24-
search = @host1.name[0..4]
25-
get :host_details, params: { organization_id: @test_org.id, search: search }
15+
test 'shows hosts with uuids' do
16+
uuids = [@host1.insights.uuid, @host2.insights.uuid]
17+
get :host_details, params: { organization_id: @test_org.id, host_uuids: uuids }
2618
assert_response :success
2719
assert_template 'api/v2/insights_advisor/host_details'
28-
assert_equal @test_org.hosts.where('name LIKE ?', "%#{search}%").count, assigns(:hosts).count
20+
assert_equal @test_org.hosts.joins(:insights).where(:insights => { :uuid => uuids }).count, assigns(:hosts).count
2921
refute_equal @test_org.hosts.count, assigns(:hosts).count
3022
end
3123

32-
test 'fails without org id' do
33-
response = get :host_details
34-
35-
assert_includes response.body, 'Organization not found'
24+
test 'shows error when no hosts found' do
25+
get :host_details, params: { organization_id: @test_org.id, host_uuids: ['nonexistentuuid'] }
26+
assert_response :not_found
27+
assert_equal 'No hosts found for the given UUIDs', JSON.parse(response.body)['error']
3628
end
3729
end
3830
end

test/factories/insights_factories.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FactoryBot.define do
22
factory :insights_facet do
3-
# sequence(:uuid) { |n| "uuid-#{n}" }
3+
sequence(:uuid) { |n| "uuid-#{n}" }
44

55
trait :with_hits do
66
hits do

0 commit comments

Comments
 (0)