Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / installation_media.rb @ 6434439d

1
module HammerCLICsv
2
  class CsvCommand
3
    class InstallationMediaCommand < BaseCommand
4
      command_name 'installation-media'
5
      desc         'import or export media'
6

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9
      PATH = 'Path'
10
      OSFAMILY = 'OS Family'
11
      OPERATING_SYSTEMS = 'Operating Systems'
12

    
13
      def export
14
        CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
15
          csv << [NAME, ORGANIZATIONS, LOCATIONS, PATH, OSFAMILY, OPERATING_SYSTEMS]
16
          @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
17
            medium = @api.resource(:media).call(:show, :id => medium['id'])
18
            name = medium['name']
19
            organizations = export_column(medium, 'organizations', 'name')
20
            locations = export_column(medium, 'locations', 'name')
21
            count = 1
22
            path = medium['path']
23
            os_family = medium['os_family']
24
            operating_systems = export_column(medium, 'operatingsystems', 'title')
25
            csv << [name, organizations, locations, path, os_family, operating_systems]
26
          end
27
        end
28
      end
29

    
30
      def import
31
        @existing = {}
32
        @api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |medium|
33
          @existing[medium['name']] = medium['id'] if medium
34
        end
35

    
36
        thread_import do |line|
37
          create_from_csv(line)
38
        end
39
      end
40

    
41
      def create_from_csv(line)
42
        params = {
43
          'medium' => {
44
            'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
45
              foreman_organization(:name => organization)
46
            end,
47
            'location_ids' => collect_column(line[LOCATIONS]) do |location|
48
              foreman_location(:name => location)
49
            end,
50
            'path' => line[PATH],
51
            'os_family' => line[OSFAMILY],
52
            'operatingsystem_ids' => collect_column(line[OPERATING_SYSTEMS]) do |os|
53
              foreman_operatingsystem(:name => os)
54
            end
55
          }
56
        }
57

    
58
        count(line[COUNT]).times do |number|
59
          name = namify(line[NAME], number)
60
          params['medium']['name'] = name
61

    
62
          if !@existing.include? name
63
            print _("Creating installation medium '%{name}'... ") % {:name => name} if option_verbose?
64
            medium = @api.resource(:media).call(:create, params)
65
            @existing[name] = medium['id']
66
          else
67
            print _("Updating installation medium '%{name}'... ") % {:name => name} if option_verbose?
68
            params['id'] = @existing[name]
69
            medium = @api.resource(:media).call(:update, params)
70
          end
71
          puts _('done') if option_verbose?
72
        end
73
      rescue RuntimeError => e
74
        raise "#{e}\n       #{line}"
75
      end
76
    end
77
  end
78
end