Class: HammerCLICsv::CsvCommand::PuppetFactsCommand

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

Constant Summary collapse

ORGANIZATION =
'Organization'
FACTS =
'Puppet Facts'
SEPARATOR =
' = '

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_puppetfacts_from_csv(line) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 45

def create_puppetfacts_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  count(line[COUNT]).times do |number|
    name = namify(line[NAME], number)
    print "Updating puppetfacts '#{name}'..." if option_verbose?
    facts = {}
    collect_column(line[FACTS]) do |fact|
      (fact_name, fact_value) = fact.split(SEPARATOR)
      facts[fact_name] = fact_value
    end

    # 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
end

#export(csv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 12

def export(csv)
  csv << [NAME, ORGANIZATION, FACTS]

  search_options = {:per_page => 999999}
  search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
  @api.resource(:hosts).call(:index, search_options)['results'].each do |host|
    facts = @api.resource(:fact_values).call(:index, {
                                                       'search' => "host = #{host['name']}",
                                                       'per_page' => 999999
                                                     })['results']
    facts = @api.resource(:fact_values).call(:index, {
                                                       'search' => "host = #{host['name']}",
                                                       'per_page' => 999999
                                                     })['results'][host['name']]
    facts ||= {}

    values = CSV.generate do |column|
      column << facts.collect do |fact_name, fact_value|
        "#{fact_name}#{SEPARATOR}#{fact_value}"
      end
    end
    values.delete!("\n")

    csv << [host['name'], host['organization_name'], values]
  end
end

#importObject



39
40
41
42
43
# File 'lib/hammer_cli_csv/puppet_facts.rb', line 39

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