Class: AnsibleProcessor

Inherits:
Processor show all
Defined in:
lib/smart_proxy_host_reports/ansible_processor.rb

Constant Summary collapse

KEYS_TO_COPY =
%w[status check_mode].freeze

Instance Attribute Summary

Attributes inherited from Processor

#errors, #telemetry

Instance Method Summary collapse

Methods inherited from Processor

#add_keywords, #build_report_root, #debug_payload, #debug_payload?, #errors?, #generated_report_id, #hostname_from_config, #keywords, #log_error, #measure, new_processor, #telemetry_as_string

Constructor Details

#initialize(data, json_body: true) ⇒ AnsibleProcessor

Returns a new instance of AnsibleProcessor.



6
7
8
9
10
11
12
13
14
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 6

def initialize(data, json_body: true)
  super(data, json_body: json_body)
  measure :parse do
    @data = JSON.parse(data)
  end
  @body = {}
  logger.debug "Processing report #{report_id}"
  debug_payload("Input", @data)
end

Instance Method Details

#build_reportObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 50

def build_report
  process
  if debug_payload?
    logger.debug { JSON.pretty_generate(@body) }
  end
  build_report_root(
    format: "ansible",
    version: 1,
    host: @body["host"],
    reported_at: @body["reported_at"],
    status: 0,
    proxy: @body["proxy"],
    body: @body,
    keywords: @body["keywords"],
  )
end

#processObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 33

def process
  measure :process do
    @body["format"] = "ansible"
    @body["id"] = report_id
    @body["host"] = hostname_from_config || @data["host"]
    @body["proxy"] = Proxy::HostReports::Plugin.settings.reported_proxy_hostname
    @body["reported_at"] = @data["reported_at"]
    @body["results"] = process_results
    @body["keywords"] = keywords
    @body["telemetry"] = telemetry
    @body["errors"] = errors if errors?
    KEYS_TO_COPY.each do |key|
      @body[key] = @data[key]
    end
  end
end

#process_resultsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 20

def process_results
  @data["results"]&.each do |result|
    process_facts(result)
    process_level(result)
    process_message(result)
    process_keywords(result)
  end
  @data["results"]
rescue StandardError => e
  logger.error "Unable to parse results", e
  @data["results"]
end

#report_idObject



16
17
18
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 16

def report_id
  @data["uuid"] || generated_report_id
end

#spool_reportObject



67
68
69
70
71
72
73
74
# File 'lib/smart_proxy_host_reports/ansible_processor.rb', line 67

def spool_report
  report_hash = build_report
  debug_payload("Output", report_hash)
  payload = measure :format do
    report_hash.to_json
  end
  SpooledHttpClient.instance.spool(report_id, payload)
end