-
Notifications
You must be signed in to change notification settings - Fork 32
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
Foreman RH Cloud API Bindings to upload hits #933
Open
parthaa
wants to merge
2
commits into
theforeman:develop
Choose a base branch
from
parthaa:rh-cloud-host
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
app/controllers/api/v2/insights_advisor/insights_advisor_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module Api | ||
module V2 | ||
module InsightsAdvisor | ||
class InsightsAdvisorController < ::Api::V2::BaseController | ||
include ::Api::Version2 | ||
include Foreman::Controller::SmartProxyAuth | ||
add_smart_proxy_filters [:upload_hits] | ||
|
||
api :PATCH, "/insights_advisor/upload_hits", N_("Upload from insights advisor") | ||
param :host_name, String, required: true | ||
param :host_uuid, String, required: true | ||
|
||
param :payload, Hash, :desc => N_("On prem payload including resolutions, rules, hits") do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we configure that one not to be logged? it's massive |
||
param :resolutions, Array, :desc => N_("upload resolutions related to the hits") do | ||
param :rule_id, String, :desc => N_("rule id"), :required => true | ||
param :description, String, :desc => N_("resolution description") | ||
param :needs_reboot, :bool, :desc => N_("need reboot") | ||
param :resolution_risk, String, :desc => N_("resolution risk") | ||
param :resolution_type, String, :desc => N_("type") | ||
end | ||
|
||
param :rules, Array, :desc => N_("upload rules related to the hits") do | ||
param :rule_id, String, :desc => N_("rule id"), :required => true | ||
param :description, String, :desc => N_("rule description") | ||
param :category_name, String, :desc => N_("category name") | ||
param :impact_name, String, :desc => N_("impact name") | ||
param :summary, String, :desc => N_("summary") | ||
param :generic, String, :desc => N_("generic") | ||
param :reason, String, :desc => N_("reason") | ||
param :total_risk, :number, :desc => N_("total risk") | ||
param :reboot_required, :bool, :desc => N_("reboot required") | ||
param :more_info, String, :desc => N_("more info") | ||
param :rating, :number, :desc => N_("rating") | ||
end | ||
|
||
param :hits, Array, :desc => N_("upload hits information") do | ||
param :rule_id, String, :desc => N_("rule id"), :required => true | ||
param :title, String, :desc => N_("rule title") | ||
param :solution_url, String, :desc => N_("solution url") | ||
param :total_risk, :number, :desc => N_("total risk") | ||
param :likelihood, :number, :desc => N_("likelihood number") | ||
param :publish_date, String, :desc => N_("publish date (YYYY-MM-DD)") | ||
param :results_url, String, :desc => N_("result url") | ||
end | ||
param :details, String, :desc => N_("upload hits details json") | ||
end | ||
|
||
def upload_hits | ||
host = Host.find_by(name: params.require(:host_name)) | ||
payload = payload_params.to_h | ||
task = ForemanTasks.async_task(ForemanHits::Async::Upload, host, params.require(:host_uuid), payload) | ||
|
||
render json: { | ||
task: task, | ||
}, status: :ok | ||
end | ||
|
||
def payload_params | ||
params.require(:payload).permit( | ||
:details, | ||
{ | ||
resolutions: [ | ||
:rule_id, :description, :needs_reboot, :resolution_risk, :resolution_type | ||
], | ||
rules: [ | ||
:rule_id, :description, :category_name, :impact_name, :summary, :generic, | ||
:reason, :total_risk, :reboot_required, :more_info, :rating | ||
], | ||
hits: [ | ||
:rule_id, :title, :solution_url, :total_risk, :likelihood, :publish_date, :results_url | ||
], | ||
} | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20241217190624_add_unique_index_to_rule_id_and_host_id_in_insights_hits.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqueIndexToRuleIdAndHostIdInInsightsHits < ActiveRecord::Migration[7.0] | ||
def change | ||
add_index :insights_hits, [:rule_id, :host_id], unique: true, name: 'index_insight_hits_on_rule_id_and_host_id' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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] | ||
# rubocop:disable Rails/SkipsModelValidations | ||
facet.hits.insert_all(hits) | ||
facet.update(hits_count: facet.hits.count) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
end | ||
|
||
def update_rules_and_resolutions(payload) | ||
# rubocop:disable Rails/SkipsModelValidations | ||
::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]) | ||
# rubocop:enable Rails/SkipsModelValidations | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need both?