Class: HammerCLICsv::CsvCommand::PuppetFactsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/puppet_facts.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_puppetfacts_from_csv(line) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 68

def create_puppetfacts_from_csv(line)
  if @headers.nil?
    @headers = line
    return
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    print "Updating puppetfacts '#{name}'..." if option_verbose?
    facts = line.to_hash
    facts.delete(NAME)
    facts.delete(COUNT)

    # Namify the values if the host name was namified
    if name != line[NAME]
      facts.each do |fact, value|
        facts[fact] = namify(value, number) unless value.nil? || value.empty?
      end
    end

    @api.resource(:hosts).call(:facts, {
                        'name' => name,
                        'facts' => facts
                      })
    print "done\n" if option_verbose?
  end
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 36

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    headers = [NAME, COUNT]
    # Extracted facts are always based upon the first host found, otherwise this would be an intensive
    # method to gather all the possible column names
    any_host = @api.resource(:hosts).call(:index, {:per_page => 1})['results'][0]
    headers += @api.resource(:puppetfactss).call(:index, {
                                                   'host_id' => any_host['name'],
                                                   'per_page' => 999999
                                                 })['results'][any_host['name']].keys
    csv << headers

    @api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
      line = [host['name'], 1]
      facts = @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})[host['name']]
      facts ||= {}
      headers[2..-1].each do |fact_name|
        line << facts[fact_name] || ''
      end
      csv << line
    end
  end
end

#importObject



60
61
62
63
64
65
66
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 60

def import
  @headers = nil

  thread_import(true) do |line|
    create_puppetfacts_from_csv(line)
  end
end