Project

General

Profile

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

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

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(csv)
14
        csv << [NAME, ORGANIZATIONS, LOCATIONS, PATH, OSFAMILY, OPERATING_SYSTEMS]
15
        @api.resource(:media).call(:index, {
16
            :per_page => 999999,
17
            :search => option_search
18
        })['results'].each do |medium|
19
          medium = @api.resource(:media).call(:show, :id => medium['id'])
20
          name = medium['name']
21
          organizations = export_column(medium, 'organizations', 'name')
22
          locations = export_column(medium, 'locations', 'name')
23
          count = 1
24
          path = medium['path']
25
          os_family = medium['os_family']
26
          operating_systems = export_column(medium, 'operatingsystems', 'title')
27
          csv << [name, organizations, locations, path, os_family, operating_systems]
28
        end
29
      end
30

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

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

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

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

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