Class: Api::V2::HostReportsController

Inherits:
V2::BaseController
  • Object
show all
Includes:
Api::Version2, Foreman::Controller::CsvResponder, Foreman::Controller::SmartProxyAuth, Foreman::TelemetryHelper, ForemanHostReports::Controller::Parameters::HostReport
Defined in:
app/controllers/api/v2/host_reports_controller.rb

Instance Method Summary collapse

Methods included from ForemanHostReports::Controller::Parameters::HostReport

#host_report_params

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/api/v2/host_reports_controller.rb', line 49

def create
  result = nil
  # version is unused at the moment (version = 1)
  params[:host_report].delete(:version)
  # check existing host and proxy
  raise("Unknown host: #{@hostname}") unless params[:host_report][:host_id]
  # fetch the body
  the_body = params[:host_report].delete(:body)
  if the_body && !the_body.is_a?(String)
    logger.warn "Report body not as a string, serializing JSON"
    the_body = JSON.pretty_generate(the_body)
  end
  # process keywords
  keywords = params[:host_report].delete(:keywords)
  report_keyword_ids = []
  telemetry_duration_histogram(:host_report_create_keywords, :ms) do
    if keywords.present?
      keywords_to_insert = keywords.each_with_object([]) do |n, ks|
        ks << { name: n }
      end
      ReportKeyword.upsert_all(keywords_to_insert, unique_by: :name)
      report_keyword_ids = ReportKeyword.where(name: keywords).distinct.pluck(:id)
    end
  end
  # create new record
  @host_report = HostReport.new(host_report_params.merge(body: the_body, report_keyword_ids: report_keyword_ids))
  telemetry_duration_histogram(:host_report_create, :ms) do
    result = @host_report.save
  end
  # refresh status and last_report flag
  telemetry_duration_histogram(:host_report_create_refresh, :ms) do
    time = Time.parse(params[:host_report][:reported_at]).utc
    @host.update_attribute(:last_report, time) if @host.last_report.nil? || @host.last_report.utc < time
    @host.refresh_statuses([HostStatus::HostReportStatus])
  end
  process_response result
rescue StandardError => e
  render_exception(e, :status => :unprocessable_entity)
end

#destroyObject



91
92
93
# File 'app/controllers/api/v2/host_reports_controller.rb', line 91

def destroy
  process_response @host_report.destroy
end

#exportObject



98
99
100
101
102
# File 'app/controllers/api/v2/host_reports_controller.rb', line 98

def export
  params[:per_page] = 'all'
  @host_reports = resource_scope_for_index.preload(:host, :proxy)
  csv_response(@host_reports)
end

#indexObject



21
22
23
24
25
# File 'app/controllers/api/v2/host_reports_controller.rb', line 21

def index
  options = {}
  options.update(host_id: params[:host_id]) if params[:host_id]
  @host_reports = resource_scope_for_index(options)
end

#showObject



29
30
31
# File 'app/controllers/api/v2/host_reports_controller.rb', line 29

def show
  @host_report = resource_scope.find(params[:id])
end