Skip to content

Commit

Permalink
Foreman RH Cloud API Bindings to upload hits
Browse files Browse the repository at this point in the history
  • Loading branch information
parthaa committed Dec 17, 2024
1 parent d8d9703 commit ef86c90
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 2 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,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
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions lib/foreman_hits/async/upload.rb
Original file line number Diff line number Diff line change
@@ -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


Check failure on line 8 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Layout/EmptyLines: Extra blank line detected.
def run

Check failure on line 9 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 2.
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)

Check failure on line 24 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Layout/EmptyLineBetweenDefs: Expected 1 empty line between method definitions; found 0.
facet = host.insights
facet.hits.delete_all
hits = payload[:hits]
facet.hits.insert_all(hits)

Check failure on line 28 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Rails/SkipsModelValidations: Avoid using `insert_all` because it skips validations.
facet.update(hits_count: facet.hits.count)
end

def update_rules_and_resolutions(payload)
::InsightsRule.upsert_all(payload[:rules], unique_by: :rule_id)

Check failure on line 33 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Rails/SkipsModelValidations: Avoid using `upsert_all` because it skips validations.
rules = payload[:rules].map {|rule| rule[:rule_id]}

Check failure on line 34 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Layout/SpaceInsideBlockBraces: Space between { and | missing.

Check failure on line 34 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Layout/SpaceInsideBlockBraces: Space missing inside }.
::InsightsResolution.where(rule_id: rules).delete_all
::InsightsResolution.insert_all(payload[:resolutions])

Check failure on line 36 in lib/foreman_hits/async/upload.rb

View workflow job for this annotation

GitHub Actions / Rubocop / Rubocop

Rails/SkipsModelValidations: Avoid using `insert_all` because it skips validations.
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

0 comments on commit ef86c90

Please sign in to comment.