Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scoped search and rabl nodes for IoP #934

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Api
module V2
module InsightsAdvisor
class InsightsAdvisorController < ::Api::V2::BaseController
include ::Api::Version2

api :GET, "insights_advisor/host_details", N_('Fetch Insights-related host details')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
api :GET, "insights_advisor/host_details", N_('Fetch Insights-related host details')
api :GET, "/insights_advisor/host_details", N_('Fetch Insights-related host details')

param :host_uuids, Array, required: true, desc: N_('List of host UUIDs')
def host_details
uuids = params.require(:host_uuids)
@hosts = ::Host.joins(:insights).where(:insights => { :uuid => uuids })
if @hosts.empty?
render json: { error: 'No hosts found for the given UUIDs' }, status: :not_found
else
respond_to do |format|
format.json { render 'api/v2/insights_advisor/host_details' }
end
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions app/models/concerns/rh_cloud_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ module RhCloudHost
scoped_search :relation => :inventory_sync_status_object, :on => :status, :rename => :insights_inventory_sync_status,
:complete_value => { :disconnect => ::InventorySync::InventoryStatus::DISCONNECT,
:sync => ::InventorySync::InventoryStatus::SYNC }
scoped_search :relation => :insights, :on => :uuid, :only_explicit => true, :rename => :insights_uuid

def insights_facet
insights
end
end
end
5 changes: 5 additions & 0 deletions app/views/api/v2/hosts/insights/base.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
attributes :uuid

node :insights_hit_details do |facet|
facet&.host&.facts('insights::hit_details')&.values&.first
end
3 changes: 3 additions & 0 deletions app/views/api/v2/hosts/insights/insights.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node :insights_attributes do
partial 'api/v2/hosts/insights/base', object: @object&.insights_facet
end
9 changes: 9 additions & 0 deletions app/views/api/v2/insights_advisor/host_details.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
collection @hosts

attributes :name
node :insights_uuid do |host|
host.insights_facet&.uuid
end
node :insights_hit_details do |host|
host&.facts('insights::hit_details')&.values&.first
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@

post 'cloud_request', to: 'cloud_request#update'
end

namespace 'insights_advisor' do
get 'host_details', to: 'insights_advisor#host_details'
# post 'upload_hits', to: 'insights_advisor#upload_hits'
end
end
end
end
7 changes: 7 additions & 0 deletions lib/foreman_rh_cloud/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def self.register_scheduled_task(task_class, cronline)

register_facet InsightsFacet, :insights do
configure_host do
api_view :list => 'api/v2/hosts/insights/insights'
set_dependent_action :destroy
end
end
Expand Down Expand Up @@ -151,6 +152,12 @@ def self.register_scheduled_task(task_class, cronline)
end
end

initializer "foreman_rh_cloud.add_rabl_view_path" do
jeremylenz marked this conversation as resolved.
Show resolved Hide resolved
Rabl.configure do |config|
config.view_paths << ForemanRhCloud::Engine.root.join('app', 'views')
end
end

initializer 'foreman_rh_cloud.register_scheduled_tasks', :before => :finisher_hook do |_app|
# skip database manipulations while tables do not exist, like in migrations
# skip object creation when admin user is not present, for example in test DB
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'test_plugin_helper'

module InsightsCloud
module Api
class InsightsAdvisorControllerTest < ActionController::TestCase
tests ::Api::V2::InsightsAdvisor::InsightsAdvisorController

setup do
@test_org = FactoryBot.create(:organization)
@host1 = FactoryBot.create(:host, :with_insights_hits, organization: @test_org, hostname: 'insightshost1')
@host2 = FactoryBot.create(:host, :with_insights_hits, organization: @test_org, hostname: 'insightshost2')
@host3 = FactoryBot.create(:host, organization: @test_org)
end

test 'shows hosts with uuids' do
uuids = [@host1.insights.uuid, @host2.insights.uuid]
get :host_details, params: { organization_id: @test_org.id, host_uuids: uuids }
assert_response :success
assert_template 'api/v2/insights_advisor/host_details'
assert_equal @test_org.hosts.joins(:insights).where(:insights => { :uuid => uuids }).count, assigns(:hosts).count
refute_equal @test_org.hosts.count, assigns(:hosts).count
end

test 'shows error when no hosts found' do
get :host_details, params: { organization_id: @test_org.id, host_uuids: ['nonexistentuuid'] }
assert_response :not_found
assert_equal 'No hosts found for the given UUIDs', JSON.parse(response.body)['error']
end
end
end
end
2 changes: 1 addition & 1 deletion test/factories/insights_factories.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :insights_facet do
# sequence(:uuid) { |n| "uuid-#{n}" }
sequence(:uuid) { |n| "uuid-#{n}" }

trait :with_hits do
hits do
Expand Down
Loading