Project

General

Profile

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

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

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, {
12
            :per_page => 999999,
13
            :search => option_search
14
        })['results'].each do |architecture|
15
          architecture = @api.resource(:architectures).call(:show, {:id => architecture['id']})
16
          name = architecture['name']
17
          operatingsystems = export_column(architecture, 'operatingsystems', 'title')
18
          csv << [name, operatingsystems]
19
        end
20
      end
21

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

    
28
        thread_import do |line|
29
          create_architectures_from_csv(line)
30
        end
31
      end
32

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