Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / architectures.rb @ c06f1783

1
module HammerCLICsv
2
  class CsvCommand
3
    class ArchitecturesCommand < BaseCommand
4
      command_name 'architectures'
5
      desc         'import or export architectures'
6

    
7
      OPERATINGSYSTEMS = 'Operating Systems'
8

    
9
      def export(csv)
10
        csv << [NAME, OPERATINGSYSTEMS]
11
        @api.resource(:architectures).call(:index, {:per_page => 999999})['results'].each do |architecture|
12
          architecture = @api.resource(:architectures).call(:show, {:id => architecture['id']})
13
          name = architecture['name']
14
          operatingsystems = export_column(architecture, 'operatingsystems', 'title')
15
          csv << [name, operatingsystems]
16
        end
17
      end
18

    
19
      def import
20
        @existing = {}
21
        @api.resource(:architectures).call(:index, {:per_page => 999999})['results'].each do |architecture|
22
          @existing[architecture['name']] = architecture['id'] if architecture
23
        end
24

    
25
        thread_import do |line|
26
          create_architectures_from_csv(line)
27
        end
28
      end
29

    
30
      def create_architectures_from_csv(line)
31
        count(line[COUNT]).times do |number|
32
          name = namify(line[NAME], number)
33
          architecture_id = @existing[name]
34
          operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name|
35
            foreman_operatingsystem(:name => operatingsystem_name)
36
          end
37
          if !architecture_id
38
            print "Creating architecture '#{name}'..." if option_verbose?
39
            architecture_id = @api.resource(:architectures).call(:create, {
40
                               'architecture' => {
41
                                 'name' => name,
42
                                 'operatingsystem_ids' => operatingsystem_ids
43
                               }
44
                             })
45
          else
46
            print "Updating architecture '#{name}'..." if option_verbose?
47
            @api.resource(:architectures).call(:update, {
48
                               'id' => architecture_id,
49
                               'architecture' => {
50
                                 'name' => name,
51
                                 'operatingsystem_ids' => operatingsystem_ids
52
                               }
53
                             })
54
          end
55
          print "done\n" if option_verbose?
56
        end
57
      rescue RuntimeError => e
58
        raise "#{e}\n       #{line}"
59
      end
60
    end
61
  end
62
end