Project

General

Profile

Download (2.62 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / puppet_facts.rb @ f92e695a

1
module HammerCLICsv
2
  class CsvCommand
3
    class PuppetFactsCommand < BaseCommand
4
      command_name 'puppet-facts'
5
      desc         'import or export puppet facts'
6

    
7
      ORGANIZATION = 'Organization'
8
      FACTS = 'Puppet Facts'
9

    
10
      SEPARATOR = ' = '
11

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

    
15
        search_options = {:per_page => 999999}
16
        search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
17
        @api.resource(:hosts).call(:index, search_options)['results'].each do |host|
18
          facts = @api.resource(:fact_values).call(:index, {
19
                                                             'search' => "host = #{host['name']}",
20
                                                             'per_page' => 999999
21
                                                           })['results']
22
          facts = @api.resource(:fact_values).call(:index, {
23
                                                             'search' => "host = #{host['name']}",
24
                                                             'per_page' => 999999
25
                                                           })['results'][host['name']]
26
          facts ||= {}
27

    
28
          values = CSV.generate do |column|
29
            column << facts.collect do |fact_name, fact_value|
30
              "#{fact_name}#{SEPARATOR}#{fact_value}"
31
            end
32
          end
33
          values.delete!("\n")
34

    
35
          csv << [host['name'], host['organization_name'], values]
36
        end
37
      end
38

    
39
      def import
40
        thread_import(true) do |line|
41
          create_puppetfacts_from_csv(line)
42
        end
43
      end
44

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

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

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

    
64
          @api.resource(:hosts).call(:facts, {
65
                                               'name' => name,
66
                                               'facts' => facts
67
                                             })
68
          print "done\n" if option_verbose?
69
        end
70
      end
71
    end
72
  end
73
end