From ef86c90bef42e20ba5db00cdeb70ccf2c684c3ef Mon Sep 17 00:00:00 2001 From: Partha Aji Date: Thu, 12 Dec 2024 19:29:58 -0500 Subject: [PATCH] Foreman RH Cloud API Bindings to upload hits --- app/models/concerns/rh_cloud_host.rb | 2 ++ config/routes.rb | 6 +++- lib/foreman_hits/async/upload.rb | 50 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/foreman_hits/async/upload.rb diff --git a/app/models/concerns/rh_cloud_host.rb b/app/models/concerns/rh_cloud_host.rb index e776ae84..f90cbd46 100644 --- a/app/models/concerns/rh_cloud_host.rb +++ b/app/models/concerns/rh_cloud_host.rb @@ -21,5 +21,7 @@ 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 end end diff --git a/config/routes.rb b/config/routes.rb index 1c7aaa77..0777dc49 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,7 @@ collection do get 'auto_complete_search' get 'resolutions', to: 'hits#resolutions' + post 'upload', to: 'hits#upload' end end match 'hits/:host_id', to: 'hits#show', via: :get @@ -60,9 +61,12 @@ end end + namespace 'insights_advisor' do + post 'upload_hits', to: 'insights_advisor#upload_hits' + end + namespace 'rh_cloud' do post 'enable_connector', to: 'inventory#enable_cloud_connector' - post 'cloud_request', to: 'cloud_request#update' end end diff --git a/lib/foreman_hits/async/upload.rb b/lib/foreman_hits/async/upload.rb new file mode 100644 index 00000000..d3704eef --- /dev/null +++ b/lib/foreman_hits/async/upload.rb @@ -0,0 +1,50 @@ +module ForemanHits + module Async + class Upload < ::Actions::EntryAction + def plan(host, uuid, payload = {}) + plan_self(host_id: host.id, uuid: uuid, payload: payload) + end + + + def run + host = Host.find(input[:host_id]) + payload = input[:payload] + update_facets(host, input[:uuid]) + update_hits(host, payload) + update_rules_and_resolutions(payload) + update_details(host, payload) + end + + def update_facets(host, uuid) + InsightsFacet.find_or_create_by(host_id: host.id) do |facet| + facet.uuid = uuid + end + host.reload + end + def update_hits(host, payload) + facet = host.insights + facet.hits.delete_all + hits = payload[:hits] + facet.hits.insert_all(hits) + facet.update(hits_count: facet.hits.count) + end + + def update_rules_and_resolutions(payload) + ::InsightsRule.upsert_all(payload[:rules], unique_by: :rule_id) + rules = payload[:rules].map {|rule| rule[:rule_id]} + ::InsightsResolution.where(rule_id: rules).delete_all + ::InsightsResolution.insert_all(payload[:resolutions]) + end + + def update_details(host, payload) + fact_name = FactName.where(name: "insights::hit_details", short_name: 'insights_details').first_or_create + fact_value = host.fact_values.where(fact_name: fact_name).first_or_create + fact_value.update(value: payload[:details]) + end + + def rescue_strategy_for_self + Dynflow::Action::Rescue::Fail + end + end + end +end