Project

General

Profile

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

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

1
module HammerCLICsv
2
  class CsvCommand
3
    class PartitionTablesCommand < BaseCommand
4
      command_name 'partition-tables'
5
      desc         'import or export partition tables'
6

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      OSFAMILY = 'OS Family'
10
      OPERATINGSYSTEMS = 'Operating Systems'
11
      LAYOUT = 'Layout'
12

    
13
      def export(csv)
14
        # TODO: partition-tables do not return their organizations or locations
15
        # http://projects.theforeman.org/issues/11175
16
        organizations_map = {}
17
        @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
18
          ptables = @api.resource(:ptables).call(:index, {'organization_id' => organization['id']})['results'].each do |ptable|
19
            organizations_map[ptable['name']] ||= []
20
            organizations_map[ptable['name']] << organization['name']
21
          end
22
        end
23
        locations_map = {}
24
        @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
25
          ptables = @api.resource(:ptables).call(:index, {'location_id' => location['id']})['results'].each do |ptable|
26
            locations_map[ptable['name']] ||= []
27
            locations_map[ptable['name']] << location['name']
28
          end
29
        end
30

    
31
        csv << [NAME, ORGANIZATIONS, LOCATIONS, OSFAMILY, OPERATINGSYSTEMS, LAYOUT]
32
        @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
33
          ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']})
34
          name = ptable['name']
35
          osfamily = ptable['os_family']
36
          layout = ptable['layout']
37
          operatingsystems = export_column(ptable, 'operatingsystems', 'title')
38

    
39
          organizations = CSV.generate do |column|
40
            column << organizations_map[name] if organizations_map[name]
41
          end
42
          organizations.delete!("\n")
43
          locations = CSV.generate do |column|
44
            column << locations_map[name] if locations_map[name]
45
          end
46
          locations.delete!("\n")
47

    
48
          csv << [name, organizations, locations, osfamily, operatingsystems, layout]
49
        end
50
      end
51

    
52
      def import
53
        @existing = {}
54
        @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
55
          @existing[ptable['name']] = ptable['id'] if ptable
56
        end
57

    
58
        thread_import do |line|
59
          create_ptables_from_csv(line)
60
        end
61
      end
62

    
63
      def create_ptables_from_csv(line)
64
        params = {
65
          'ptable' => {
66
            'os_family' => line[OSFAMILY],
67
            'layout' => line[LAYOUT]
68
          }
69
        }
70
        # Check for backwards compatibility
71
        if apipie_check_param(:ptable, :create, 'ptable[operatingsystem_ids]')
72
          operatingsystems = collect_column(line[OPERATINGSYSTEMS]) do |operatingsystem|
73
            foreman_operatingsystem(:name => operatingsystem)
74
          end
75
          params['ptable']['operatingsystem_ids'] = operatingsystems
76
        end
77
        if apipie_check_param(:ptable, :create, 'ptable[organization_ids]')
78
          organizations = collect_column(line[ORGANIZATIONS]) do |organization|
79
            foreman_organization(:name => organization)
80
          end
81
          params['ptable']['organization_ids'] = organizations
82
        end
83
        if apipie_check_param(:ptable, :create, 'ptable[location_ids]')
84
          locations = collect_column(line[LOCATIONS]) do |location|
85
            foreman_location(:name => location)
86
          end
87
          params['ptable']['location_ids'] = locations
88
        end
89

    
90
        count(line[COUNT]).times do |number|
91
          name = namify(line[NAME], number)
92
          params['ptable']['name'] = name
93
          if !@existing.include? name
94
            print "Creating partition-table '#{name}'... " if option_verbose?
95
            @api.resource(:ptables).call(:create, params)
96
          else
97
            print "Updating partition-table '#{name}'..." if option_verbose?
98
            params['id'] = @existing[name]
99
            @api.resource(:ptables).call(:update, params)
100
          end
101
          print "done\n" if option_verbose?
102
        end
103
      rescue RuntimeError => e
104
        raise "#{e}\n       #{line}"
105
      end
106
    end
107
  end
108
end