Project

General

Profile

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

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

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, {
19
              'organization_id' => organization['id'],
20
              'search' => option_search
21
          })['results'].each do |ptable|
22
            organizations_map[ptable['name']] ||= []
23
            organizations_map[ptable['name']] << organization['name']
24
          end
25
        end
26
        locations_map = {}
27
        @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
28
          ptables = @api.resource(:ptables).call(:index, {'location_id' => location['id']})['results'].each do |ptable|
29
            locations_map[ptable['name']] ||= []
30
            locations_map[ptable['name']] << location['name']
31
          end
32
        end
33

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

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

    
51
          csv << [name, organizations, locations, osfamily, operatingsystems, layout]
52
        end
53
      end
54

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

    
61
        thread_import do |line|
62
          create_ptables_from_csv(line)
63
        end
64
      end
65

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

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