Class: Actions::ForemanProbing::ImportHostFacts

Inherits:
Dynflow::Action
  • Object
show all
Defined in:
app/lib/actions/import_host_facts.rb

Instance Method Summary collapse

Instance Method Details

#runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/lib/actions/import_host_facts.rb', line 5

def run
  facts = input[:facts]
  # If we're not scanning an already existing host and it is down, we don't want to import it to Foreman
  unless (input[:options][:host_id].nil? && facts.fetch(:status, {})[:state] == 'down')
    host = determine_host(facts)
    facts = try_match_interface_names(host, facts)
    facts.delete(:hostnames)
    ::User.as :admin do
      state          = host.import_facts(facts)
      output[:state] = state
      output[:facts] = facts
    end
    try_set_subnet!(host)
    host.smart_proxy_ids << input[:proxy_id]
    output[:host_id] = host.id
    output[:hostname] = host.name
    scan = ::ForemanProbing::Scan.find(input[:scan_id])
    host.organization_id ||= scan.organization_ids.first
    host.location_id ||= scan.location_ids.first
    host.scans << scan
    host.save!
  end
rescue ::Foreman::Exception => e
  # This error is what is thrown by Host#ImportHostAndFacts when
  # the Host is in the build state. This can be refactored once
  # issue #3959 is fixed.
  raise e unless e.code == 'ERF51-9911'
end